AbstractSyslogHandler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Logger;
  12. use Monolog\Formatter\FormatterInterface;
  13. use Monolog\Formatter\LineFormatter;
  14. /**
  15. * Common syslog functionality
  16. */
  17. abstract class AbstractSyslogHandler extends AbstractProcessingHandler
  18. {
  19. protected $facility;
  20. /**
  21. * Translates Monolog log levels to syslog log priorities.
  22. */
  23. protected $logLevels = [
  24. Logger::DEBUG => LOG_DEBUG,
  25. Logger::INFO => LOG_INFO,
  26. Logger::NOTICE => LOG_NOTICE,
  27. Logger::WARNING => LOG_WARNING,
  28. Logger::ERROR => LOG_ERR,
  29. Logger::CRITICAL => LOG_CRIT,
  30. Logger::ALERT => LOG_ALERT,
  31. Logger::EMERGENCY => LOG_EMERG,
  32. ];
  33. /**
  34. * List of valid log facility names.
  35. */
  36. protected $facilities = [
  37. 'auth' => LOG_AUTH,
  38. 'authpriv' => LOG_AUTHPRIV,
  39. 'cron' => LOG_CRON,
  40. 'daemon' => LOG_DAEMON,
  41. 'kern' => LOG_KERN,
  42. 'lpr' => LOG_LPR,
  43. 'mail' => LOG_MAIL,
  44. 'news' => LOG_NEWS,
  45. 'syslog' => LOG_SYSLOG,
  46. 'user' => LOG_USER,
  47. 'uucp' => LOG_UUCP,
  48. ];
  49. /**
  50. * @param mixed $facility
  51. * @param int $level The minimum logging level at which this handler will be triggered
  52. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  53. */
  54. public function __construct($facility = LOG_USER, $level = Logger::DEBUG, bool $bubble = true)
  55. {
  56. parent::__construct($level, $bubble);
  57. if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
  58. $this->facilities['local0'] = LOG_LOCAL0;
  59. $this->facilities['local1'] = LOG_LOCAL1;
  60. $this->facilities['local2'] = LOG_LOCAL2;
  61. $this->facilities['local3'] = LOG_LOCAL3;
  62. $this->facilities['local4'] = LOG_LOCAL4;
  63. $this->facilities['local5'] = LOG_LOCAL5;
  64. $this->facilities['local6'] = LOG_LOCAL6;
  65. $this->facilities['local7'] = LOG_LOCAL7;
  66. } else {
  67. $this->facilities['local0'] = 128; // LOG_LOCAL0
  68. $this->facilities['local1'] = 136; // LOG_LOCAL1
  69. $this->facilities['local2'] = 144; // LOG_LOCAL2
  70. $this->facilities['local3'] = 152; // LOG_LOCAL3
  71. $this->facilities['local4'] = 160; // LOG_LOCAL4
  72. $this->facilities['local5'] = 168; // LOG_LOCAL5
  73. $this->facilities['local6'] = 176; // LOG_LOCAL6
  74. $this->facilities['local7'] = 184; // LOG_LOCAL7
  75. }
  76. // convert textual description of facility to syslog constant
  77. if (is_string($facility) && array_key_exists(strtolower($facility), $this->facilities)) {
  78. $facility = $this->facilities[strtolower($facility)];
  79. } elseif (!in_array($facility, array_values($this->facilities), true)) {
  80. throw new \UnexpectedValueException('Unknown facility value "'.$facility.'" given');
  81. }
  82. $this->facility = $facility;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. protected function getDefaultFormatter(): FormatterInterface
  88. {
  89. return new LineFormatter('%channel%.%level_name%: %message% %context% %extra%');
  90. }
  91. }