AmfphpLogger.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * This file is part of amfPHP
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the license that is bundled
  8. * with this package in the file license.txt.
  9. */
  10. /**
  11. * logs requests and responses in their serialized and deserialized forms.
  12. * deactivated by default.
  13. *
  14. * Note that this a crude logging system, with no levels, targets etc. like Log4j for example.
  15. * It is as such to be used for development purposes, but not for production
  16. *
  17. * @package Amfphp_Plugins_Logger
  18. * @author Ariel Sommeria-klein
  19. */
  20. class AmfphpLogger {
  21. const LOG_FILE_PATH = 'amfphplog.log';
  22. /**
  23. * constructor.
  24. * @param array $config optional key/value pairs in an associative array. Used to override default configuration values.
  25. */
  26. public function __construct(array $config = null) {
  27. $filterManager = Amfphp_Core_FilterManager::getInstance();
  28. $filterManager->addFilter(Amfphp_Core_Gateway::FILTER_SERIALIZED_REQUEST, $this, 'filterSerializedRequest');
  29. $filterManager->addFilter(Amfphp_Core_Gateway::FILTER_DESERIALIZED_REQUEST, $this, 'filterDeserializedRequest');
  30. $filterManager->addFilter(Amfphp_Core_Gateway::FILTER_DESERIALIZED_RESPONSE, $this, 'filterDeserializedResponse');
  31. $filterManager->addFilter(Amfphp_Core_Gateway::FILTER_SERIALIZED_RESPONSE, $this, 'filterSerializedResponse');
  32. }
  33. /**
  34. * write message to log file at LOG_FILE_PATH
  35. * @see LOG_FILE_PATH
  36. * @param String $message
  37. * @throws Amfphp_Core_Exception
  38. */
  39. public static function logMessage($message) {
  40. $fh = fopen(self::LOG_FILE_PATH, 'a');
  41. if (!$fh) {
  42. throw new Amfphp_Core_Exception("couldn't open log file for writing");
  43. }
  44. fwrite($fh, $message . "\n");
  45. fclose($fh);
  46. }
  47. /**
  48. * logs the serialized incoming packet
  49. * @param String $rawData
  50. */
  51. public function filterSerializedRequest($rawData) {
  52. self::logMessage("serialized request : \n$rawData");
  53. }
  54. /**
  55. * logs the deserialized request
  56. * @param mixed $deserializedRequest
  57. */
  58. public function filterDeserializedRequest($deserializedRequest) {
  59. self::logMessage("deserialized request : \n " . print_r($deserializedRequest, true));
  60. }
  61. /**
  62. * logs the deserialized response
  63. * @param packet $deserializedResponse
  64. */
  65. public function filterDeserializedResponse($deserializedResponse) {
  66. self::logMessage("deserialized response : \n " . print_r($deserializedResponse, true));
  67. }
  68. /**
  69. * logs the deserialized incoming packet
  70. * @param mixed $rawData
  71. */
  72. public function filterSerializedResponse($rawData) {
  73. self::logMessage("serialized response : \n$rawData");
  74. }
  75. }
  76. ?>