SyslogLog.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  12. * @since 2.4.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Log\Engine;
  16. use Cake\Log\Engine\BaseLog;
  17. /**
  18. * Syslog stream for Logging. Writes logs to the system logger
  19. */
  20. class SyslogLog extends BaseLog {
  21. /**
  22. * Default config for this class
  23. *
  24. * By default messages are formatted as:
  25. * level: message
  26. *
  27. * To override the log format (e.g. to add your own info) define the format key when configuring
  28. * this logger
  29. *
  30. * If you wish to include a prefix to all messages, for instance to identify the
  31. * application or the web server, then use the prefix option. Please keep in mind
  32. * the prefix is shared by all streams using syslog, as it is dependent of
  33. * the running process. For a local prefix, to be used only by one stream, you
  34. * can use the format key.
  35. *
  36. * ## Example:
  37. *
  38. * {{{
  39. * Log::config('error', ]
  40. * 'engine' => 'Syslog',
  41. * 'levels' => ['emergency', 'alert', 'critical', 'error'],
  42. * 'format' => "%s: My-App - %s",
  43. * 'prefix' => 'Web Server 01'
  44. * ]);
  45. * }}}
  46. *
  47. * @var array
  48. */
  49. protected $_defaultConfig = [
  50. 'levels' => [],
  51. 'scopes' => [],
  52. 'format' => '%s: %s',
  53. 'flag' => LOG_ODELAY,
  54. 'prefix' => '',
  55. 'facility' => LOG_USER
  56. ];
  57. /**
  58. * Used to map the string names back to their LOG_* constants
  59. *
  60. * @var array
  61. */
  62. protected $_levelMap = array(
  63. 'emergency' => LOG_EMERG,
  64. 'alert' => LOG_ALERT,
  65. 'critical' => LOG_CRIT,
  66. 'error' => LOG_ERR,
  67. 'warning' => LOG_WARNING,
  68. 'notice' => LOG_NOTICE,
  69. 'info' => LOG_INFO,
  70. 'debug' => LOG_DEBUG
  71. );
  72. /**
  73. * Whether the logger connection is open or not
  74. *
  75. * @var bool
  76. */
  77. protected $_open = false;
  78. /**
  79. * Writes a message to syslog
  80. *
  81. * Map the $level back to a LOG_ constant value, split multi-line messages into multiple
  82. * log messages, pass all messages through the format defined in the configuration
  83. *
  84. * @param string $level The severity level of log you are making.
  85. * @param string $message The message you want to log.
  86. * @param array $context Additional information about the logged message
  87. * @return bool success of write.
  88. */
  89. public function log($level, $message, array $context = []) {
  90. if (!$this->_open) {
  91. $config = $this->_config;
  92. $this->_open($config['prefix'], $config['flag'], $config['facility']);
  93. $this->_open = true;
  94. }
  95. $priority = LOG_DEBUG;
  96. if (isset($this->_levelMap[$level])) {
  97. $priority = $this->_levelMap[$level];
  98. }
  99. $messages = explode("\n", $this->_format($message, $context));
  100. foreach ($messages as $message) {
  101. $message = sprintf($this->_config['format'], $level, $message);
  102. $this->_write($priority, $message);
  103. }
  104. return true;
  105. }
  106. /**
  107. * Extracts the call to openlog() in order to run unit tests on it. This function
  108. * will initialize the connection to the system logger
  109. *
  110. * @param string $ident the prefix to add to all messages logged
  111. * @param int $options the options flags to be used for logged messages
  112. * @param int $facility the stream or facility to log to
  113. * @return void
  114. */
  115. protected function _open($ident, $options, $facility) {
  116. openlog($ident, $options, $facility);
  117. }
  118. /**
  119. * Extracts the call to syslog() in order to run unit tests on it. This function
  120. * will perform the actual write in the system logger
  121. *
  122. * @param int $priority Message priority.
  123. * @param string $message Message to log.
  124. * @return bool
  125. */
  126. protected function _write($priority, $message) {
  127. return syslog($priority, $message);
  128. }
  129. /**
  130. * Closes the logger connection
  131. */
  132. public function __destruct() {
  133. closelog();
  134. }
  135. }