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.

82 lines
2.4KB

  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. return $logger;
  27. }
  28. /**
  29. * Creates a new PSR-3 compliant develop logger
  30. *
  31. * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
  32. * you'll use this logger instance for the duration of your work.
  33. *
  34. * It should never get filled during normal usage of Friendica
  35. *
  36. * @param string $channel The channel of the logger instance
  37. * @param string $developerIp The IP of the developer who wants to use the logger
  38. *
  39. * @return LoggerInterface The PSR-3 compliant logger instance
  40. */
  41. public static function createDev($channel, $developerIp)
  42. {
  43. $logger = new Monolog\Logger($channel);
  44. $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
  45. $logger->pushHandler(new FriendicaDevelopHandler($developerIp));
  46. return $logger;
  47. }
  48. /**
  49. * Adding a handler to a given logger instance
  50. *
  51. * @param LoggerInterface $logger The logger instance
  52. * @param mixed $stream The stream which handles the logger output
  53. * @param string $level The level, for which this handler at least should handle logging
  54. *
  55. * @return void
  56. *
  57. * @throws InternalServerErrorException if the logger is incompatible to the logger factory
  58. * @throws \Exception in case of general failures
  59. */
  60. public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
  61. {
  62. if ($logger instanceof Monolog\Logger) {
  63. $fileHandler = new Monolog\Handler\StreamHandler($stream . ".1", Monolog\Logger::toMonologLevel($level));
  64. $formatter = new Monolog\Formatter\LineFormatter("%channel% [%level_name%]: %message% %context% %extra%");
  65. $fileHandler->setFormatter($formatter);
  66. $logger->pushHandler($fileHandler);
  67. } else {
  68. throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
  69. }
  70. }
  71. }