AbstractSyslogHandler.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. use Monolog\Formatter\LineFormatter;
  13. /**
  14. * Common syslog functionality
  15. */
  16. abstract class AbstractSyslogHandler extends AbstractProcessingHandler
  17. {
  18. protected $facility;
  19. /**
  20. * Translates Monolog log levels to syslog log priorities.
  21. */
  22. protected $logLevels = array(
  23. Logger::DEBUG => LOG_DEBUG,
  24. Logger::INFO => LOG_INFO,
  25. Logger::NOTICE => LOG_NOTICE,
  26. Logger::WARNING => LOG_WARNING,
  27. Logger::ERROR => LOG_ERR,
  28. Logger::CRITICAL => LOG_CRIT,
  29. Logger::ALERT => LOG_ALERT,
  30. Logger::EMERGENCY => LOG_EMERG,
  31. );
  32. /**
  33. * List of valid log facility names.
  34. */
  35. protected $facilities = array(
  36. 'auth' => LOG_AUTH,
  37. 'authpriv' => LOG_AUTHPRIV,
  38. 'cron' => LOG_CRON,
  39. 'daemon' => LOG_DAEMON,
  40. 'kern' => LOG_KERN,
  41. 'lpr' => LOG_LPR,
  42. 'mail' => LOG_MAIL,
  43. 'news' => LOG_NEWS,
  44. 'syslog' => LOG_SYSLOG,
  45. 'user' => LOG_USER,
  46. 'uucp' => LOG_UUCP,
  47. );
  48. /**
  49. * @param mixed $facility
  50. * @param integer $level The minimum logging level at which this handler will be triggered
  51. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  52. */
  53. public function __construct($facility = LOG_USER, $level = Logger::DEBUG, $bubble = true)
  54. {
  55. parent::__construct($level, $bubble);
  56. if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
  57. $this->facilities['local0'] = LOG_LOCAL0;
  58. $this->facilities['local1'] = LOG_LOCAL1;
  59. $this->facilities['local2'] = LOG_LOCAL2;
  60. $this->facilities['local3'] = LOG_LOCAL3;
  61. $this->facilities['local4'] = LOG_LOCAL4;
  62. $this->facilities['local5'] = LOG_LOCAL5;
  63. $this->facilities['local6'] = LOG_LOCAL6;
  64. $this->facilities['local7'] = LOG_LOCAL7;
  65. }
  66. // convert textual description of facility to syslog constant
  67. if (array_key_exists(strtolower($facility), $this->facilities)) {
  68. $facility = $this->facilities[strtolower($facility)];
  69. } elseif (!in_array($facility, array_values($this->facilities), true)) {
  70. throw new \UnexpectedValueException('Unknown facility value "'.$facility.'" given');
  71. }
  72. $this->facility = $facility;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function getDefaultFormatter()
  78. {
  79. return new LineFormatter('%channel%.%level_name%: %message% %context% %extra%');
  80. }
  81. }