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