SyslogHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  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. /**
  13. * Logs to syslog service.
  14. *
  15. * usage example:
  16. *
  17. * $log = new Logger('application');
  18. * $syslog = new SyslogHandler('myfacility', 'local6');
  19. * $formatter = new LineFormatter("%channel%.%level_name%: %message% %extra%");
  20. * $syslog->setFormatter($formatter);
  21. * $log->pushHandler($syslog);
  22. *
  23. * @author Sven Paulus <sven@karlsruhe.org>
  24. */
  25. class SyslogHandler extends AbstractProcessingHandler
  26. {
  27. /**
  28. * Translates Monolog log levels to syslog log priorities.
  29. */
  30. private $logLevels = array(
  31. Logger::DEBUG => LOG_DEBUG,
  32. Logger::INFO => LOG_INFO,
  33. Logger::NOTICE => LOG_NOTICE,
  34. Logger::WARNING => LOG_WARNING,
  35. Logger::ERROR => LOG_ERR,
  36. Logger::CRITICAL => LOG_CRIT,
  37. Logger::ALERT => LOG_ALERT,
  38. Logger::EMERGENCY => LOG_EMERG,
  39. );
  40. /**
  41. * List of valid log facility names.
  42. */
  43. private $facilities = array(
  44. 'auth' => LOG_AUTH,
  45. 'authpriv' => LOG_AUTHPRIV,
  46. 'cron' => LOG_CRON,
  47. 'daemon' => LOG_DAEMON,
  48. 'kern' => LOG_KERN,
  49. 'lpr' => LOG_LPR,
  50. 'mail' => LOG_MAIL,
  51. 'news' => LOG_NEWS,
  52. 'syslog' => LOG_SYSLOG,
  53. 'user' => LOG_USER,
  54. 'uucp' => LOG_UUCP,
  55. );
  56. /**
  57. * @param string $ident
  58. * @param mixed $facility
  59. * @param integer $level The minimum logging level at which this handler will be triggered
  60. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  61. */
  62. public function __construct($ident, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true)
  63. {
  64. parent::__construct($level, $bubble);
  65. if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
  66. $this->facilities['local0'] = LOG_LOCAL0;
  67. $this->facilities['local1'] = LOG_LOCAL1;
  68. $this->facilities['local2'] = LOG_LOCAL2;
  69. $this->facilities['local3'] = LOG_LOCAL3;
  70. $this->facilities['local4'] = LOG_LOCAL4;
  71. $this->facilities['local5'] = LOG_LOCAL5;
  72. $this->facilities['local6'] = LOG_LOCAL6;
  73. $this->facilities['local7'] = LOG_LOCAL7;
  74. }
  75. // convert textual description of facility to syslog constant
  76. if (array_key_exists(strtolower($facility), $this->facilities)) {
  77. $facility = $this->facilities[strtolower($facility)];
  78. } else if (!in_array($facility, array_values($this->facilities), true)) {
  79. throw new \UnexpectedValueException('Unknown facility value "'.$facility.'" given');
  80. }
  81. if (!openlog($ident, LOG_PID, $facility)) {
  82. throw new \LogicException('Can\'t open syslog for ident "'.$ident.'" and facility "'.$facility.'"');
  83. }
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function close()
  89. {
  90. closelog();
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. protected function write(array $record)
  96. {
  97. syslog($this->logLevels[$record['level']], (string) $record['formatted']);
  98. }
  99. }