Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.7KB

  1. <?php
  2. namespace Friendica\Util;
  3. use Friendica\Network\HTTPException\InternalServerErrorException;
  4. use Friendica\Util\Logger\FriendicaDevelopHandler;
  5. use Monolog;
  6. use Psr\Log\LoggerInterface;
  7. use Psr\Log\LogLevel;
  8. /**
  9. * A logger factory
  10. *
  11. * Currently only Monolog is supported
  12. */
  13. class LoggerFactory
  14. {
  15. /**
  16. * Creates a new PSR-3 compliant logger instances
  17. *
  18. * @param string $channel The channel of the logger instance
  19. *
  20. * @return LoggerInterface The PSR-3 compliant logger instance
  21. */
  22. public static function create($channel)
  23. {
  24. $logger = new Monolog\Logger($channel);
  25. $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
  26. $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
  27. // Add more information in case of a warning and more
  28. $logger->pushProcessor(new Monolog\Processor\IntrospectionProcessor(LogLevel::WARNING, [], 1));
  29. return $logger;
  30. }
  31. /**
  32. * Creates a new PSR-3 compliant develop logger
  33. *
  34. * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
  35. * you'll use this logger instance for the duration of your work.
  36. *
  37. * It should never get filled during normal usage of Friendica
  38. *
  39. * @param string $channel The channel of the logger instance
  40. * @param string $developerIp The IP of the developer who wants to use the logger
  41. *
  42. * @return LoggerInterface The PSR-3 compliant logger instance
  43. */
  44. public static function createDev($channel, $developerIp)
  45. {
  46. $logger = new Monolog\Logger($channel);
  47. $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
  48. $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
  49. $logger->pushProcessor(new Monolog\Processor\IntrospectionProcessor(Loglevel::DEBUG, [], 1));
  50. $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
  51. return $logger;
  52. }
  53. /**
  54. * Adding a handler to a given logger instance
  55. *
  56. * @param LoggerInterface $logger The logger instance
  57. * @param mixed $stream The stream which handles the logger output
  58. * @param string $level The level, for which this handler at least should handle logging
  59. *
  60. * @return void
  61. *
  62. * @throws InternalServerErrorException if the logger is incompatible to the logger factory
  63. * @throws \Exception in case of general failures
  64. */
  65. public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
  66. {
  67. if ($logger instanceof Monolog\Logger) {
  68. $fileHandler = new Monolog\Handler\StreamHandler($stream, Monolog\Logger::toMonologLevel($level));
  69. $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
  70. $fileHandler->setFormatter($formatter);
  71. $logger->pushHandler($fileHandler);
  72. } else {
  73. throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
  74. }
  75. }
  76. /**
  77. * This method enables the test mode of a given logger
  78. *
  79. * @param LoggerInterface $logger The logger
  80. *
  81. * @return Monolog\Handler\TestHandler the Handling for tests
  82. *
  83. * @throws InternalServerErrorException if the logger is incompatible to the logger factory
  84. */
  85. public static function enableTest($logger)
  86. {
  87. if ($logger instanceof Monolog\Logger) {
  88. // disable every handler so far
  89. $logger->pushHandler(new Monolog\Handler\NullHandler());
  90. // enable the test handler
  91. $fileHandler = new Monolog\Handler\TestHandler();
  92. $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
  93. $fileHandler->setFormatter($formatter);
  94. $logger->pushHandler($fileHandler);
  95. return $fileHandler;
  96. } else {
  97. throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
  98. }
  99. }
  100. }