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: *
30: * By default messages are formatted as:
31: * type: message
32: *
33: * To override the log format (e.g. to add your own info) define the format key when configuring
34: * this logger
35: *
36: * If you wish to include a prefix to all messages, for instance to identify the
37: * application or the web server, then use the prefix option. Please keep in mind
38: * the prefix is shared by all streams using syslog, as it is dependent of
39: * the running process. For a local prefix, to be used only by one stream, you
40: * can use the format key.
41: *
42: * ## Example:
43: *
44: * {{{
45: * CakeLog::config('error', array(
46: * 'engine' => 'Syslog',
47: * 'types' => array('emergency', 'alert', 'critical', 'error'),
48: * 'format' => "%s: My-App - %s",
49: * 'prefix' => 'Web Server 01'
50: * ));
51: * }}}
52: *
53: * @var array
54: */
55: protected $_defaults = array(
56: 'format' => '%s: %s',
57: 'flag' => LOG_ODELAY,
58: 'prefix' => '',
59: 'facility' => LOG_USER
60: );
61:
62: /**
63: *
64: * Used to map the string names back to their LOG_* constants
65: *
66: * @var array
67: */
68: protected $_priorityMap = array(
69: 'emergency' => LOG_EMERG,
70: 'alert' => LOG_ALERT,
71: 'critical' => LOG_CRIT,
72: 'error' => LOG_ERR,
73: 'warning' => LOG_WARNING,
74: 'notice' => LOG_NOTICE,
75: 'info' => LOG_INFO,
76: 'debug' => LOG_DEBUG
77: );
78:
79: /**
80: * Whether the logger connection is open or not
81: *
82: * @var boolean
83: */
84: protected $_open = false;
85:
86: /**
87: * Make sure the configuration contains the format parameter, by default it uses
88: * the error number and the type as a prefix to the message
89: *
90: * @param array $config
91: */
92: public function __construct($config = array()) {
93: $config += $this->_defaults;
94: parent::__construct($config);
95: }
96:
97: /**
98: * Writes a message to syslog
99: *
100: * Map the $type back to a LOG_ constant value, split multi-line messages into multiple
101: * log messages, pass all messages through the format defined in the configuration
102: *
103: * @param string $type The type of log you are making.
104: * @param string $message The message you want to log.
105: * @return boolean success of write.
106: */
107: public function write($type, $message) {
108: if (!$this->_open) {
109: $config = $this->_config;
110: $this->_open($config['prefix'], $config['flag'], $config['facility']);
111: $this->_open = true;
112: }
113:
114: $priority = LOG_DEBUG;
115: if (isset($this->_priorityMap[$type])) {
116: $priority = $this->_priorityMap[$type];
117: }
118:
119: $messages = explode("\n", $message);
120: foreach ($messages as $message) {
121: $message = sprintf($this->_config['format'], $type, $message);
122: $this->_write($priority, $message);
123: }
124:
125: return true;
126: }
127:
128: /**
129: * Extracts the call to openlog() in order to run unit tests on it. This function
130: * will initialize the connection to the system logger
131: *
132: * @param string $ident the prefix to add to all messages logged
133: * @param integer $options the options flags to be used for logged messages
134: * @param integer $facility the stream or facility to log to
135: * @return void
136: */
137: protected function _open($ident, $options, $facility) {
138: openlog($ident, $options, $facility);
139: }
140:
141: /**
142: * Extracts the call to syslog() in order to run unit tests on it. This function
143: * will perform the actual write in the system logger
144: *
145: * @param integer $priority
146: * @param string $message
147: * @return boolean
148: */
149: protected function _write($priority, $message) {
150: return syslog($priority, $message);
151: }
152:
153: /**
154: * Closes the logger connection
155: */
156: public function __destruct() {
157: closelog();
158: }
159:
160: }
161: