SyslogLog.php 4.2 KB

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