pictcode / lib / Cake / Log / Engine / SyslogLog.php @ 48ae03cf
履歴 | 表示 | アノテート | ダウンロード (4.195 KB)
| 1 |
<?php
|
|---|---|
| 2 |
/**
|
| 3 |
* Syslog logger engine for CakePHP
|
| 4 |
*
|
| 5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
| 6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 7 |
*
|
| 8 |
* Licensed under The MIT License
|
| 9 |
* For full copyright and license information, please see the LICENSE.txt
|
| 10 |
* Redistributions of files must retain the above copyright notice.
|
| 11 |
*
|
| 12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 13 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
| 14 |
* @package Cake.Log.Engine
|
| 15 |
* @since CakePHP(tm) v 2.4
|
| 16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
| 17 |
*/
|
| 18 |
|
| 19 |
App::uses('BaseLog', 'Log/Engine'); |
| 20 |
|
| 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 |
/**
|
| 78 |
* Whether the logger connection is open or not
|
| 79 |
*
|
| 80 |
* @var bool
|
| 81 |
*/
|
| 82 |
protected $_open = false; |
| 83 |
|
| 84 |
/**
|
| 85 |
* Make sure the configuration contains the format parameter, by default it uses
|
| 86 |
* the error number and the type as a prefix to the message
|
| 87 |
*
|
| 88 |
* @param array $config Options list.
|
| 89 |
*/
|
| 90 |
public function __construct($config = array()) { |
| 91 |
$config += $this->_defaults; |
| 92 |
parent::__construct($config); |
| 93 |
} |
| 94 |
|
| 95 |
/**
|
| 96 |
* Writes a message to syslog
|
| 97 |
*
|
| 98 |
* Map the $type back to a LOG_ constant value, split multi-line messages into multiple
|
| 99 |
* log messages, pass all messages through the format defined in the configuration
|
| 100 |
*
|
| 101 |
* @param string $type The type of log you are making.
|
| 102 |
* @param string $message The message you want to log.
|
| 103 |
* @return bool success of write.
|
| 104 |
*/
|
| 105 |
public function write($type, $message) { |
| 106 |
if (!$this->_open) { |
| 107 |
$config = $this->_config; |
| 108 |
$this->_open($config['prefix'], $config['flag'], $config['facility']); |
| 109 |
$this->_open = true; |
| 110 |
} |
| 111 |
|
| 112 |
$priority = LOG_DEBUG; |
| 113 |
if (isset($this->_priorityMap[$type])) { |
| 114 |
$priority = $this->_priorityMap[$type]; |
| 115 |
} |
| 116 |
|
| 117 |
$messages = explode("\n", $message); |
| 118 |
foreach ($messages as $message) { |
| 119 |
$message = sprintf($this->_config['format'], $type, $message); |
| 120 |
$this->_write($priority, $message); |
| 121 |
} |
| 122 |
|
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
/**
|
| 127 |
* Extracts the call to openlog() in order to run unit tests on it. This function
|
| 128 |
* will initialize the connection to the system logger
|
| 129 |
*
|
| 130 |
* @param string $ident the prefix to add to all messages logged
|
| 131 |
* @param int $options the options flags to be used for logged messages
|
| 132 |
* @param int $facility the stream or facility to log to
|
| 133 |
* @return void
|
| 134 |
*/
|
| 135 |
protected function _open($ident, $options, $facility) { |
| 136 |
openlog($ident, $options, $facility); |
| 137 |
} |
| 138 |
|
| 139 |
/**
|
| 140 |
* Extracts the call to syslog() in order to run unit tests on it. This function
|
| 141 |
* will perform the actual write in the system logger
|
| 142 |
*
|
| 143 |
* @param int $priority Message priority.
|
| 144 |
* @param string $message Message to log.
|
| 145 |
* @return bool
|
| 146 |
*/
|
| 147 |
protected function _write($priority, $message) { |
| 148 |
return syslog($priority, $message); |
| 149 |
} |
| 150 |
|
| 151 |
/**
|
| 152 |
* Closes the logger connection
|
| 153 |
*/
|
| 154 |
public function __destruct() { |
| 155 |
closelog();
|
| 156 |
} |
| 157 |
|
| 158 |
} |