SyslogLog.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakefoundation.org CakePHP(tm) Project
  12. * @since 2.4.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Log\Engine;
  16. /**
  17. * Syslog stream for Logging. Writes logs to the system logger
  18. */
  19. class SyslogLog extends BaseLog
  20. {
  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 int[]
  61. */
  62. protected $_levelMap = [
  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. {
  91. if (!$this->_open) {
  92. $config = $this->_config;
  93. $this->_open($config['prefix'], $config['flag'], $config['facility']);
  94. $this->_open = true;
  95. }
  96. $priority = LOG_DEBUG;
  97. if (isset($this->_levelMap[$level])) {
  98. $priority = $this->_levelMap[$level];
  99. }
  100. $messages = explode("\n", $this->_format($message, $context));
  101. foreach ($messages as $message) {
  102. $message = sprintf($this->_config['format'], $level, $message);
  103. $this->_write($priority, $message);
  104. }
  105. return true;
  106. }
  107. /**
  108. * Extracts the call to openlog() in order to run unit tests on it. This function
  109. * will initialize the connection to the system logger
  110. *
  111. * @param string $ident the prefix to add to all messages logged
  112. * @param int $options the options flags to be used for logged messages
  113. * @param int $facility the stream or facility to log to
  114. * @return void
  115. */
  116. protected function _open($ident, $options, $facility)
  117. {
  118. openlog($ident, $options, $facility);
  119. }
  120. /**
  121. * Extracts the call to syslog() in order to run unit tests on it. This function
  122. * will perform the actual write in the system logger
  123. *
  124. * @param int $priority Message priority.
  125. * @param string $message Message to log.
  126. * @return bool
  127. */
  128. protected function _write($priority, $message)
  129. {
  130. return syslog($priority, $message);
  131. }
  132. /**
  133. * Closes the logger connection
  134. */
  135. public function __destruct()
  136. {
  137. closelog();
  138. }
  139. }