SyslogHandler.php 3.2KB

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