cakephp2-php8/lib/Cake/Log/Engine/SyslogLog.php

159 lines
4.2 KiB
PHP
Raw Normal View History

2013-05-14 21:21:38 +00:00
<?php
/**
* Syslog logger engine for CakePHP
*
2017-06-10 21:33:55 +00:00
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
2013-05-14 21:21:38 +00:00
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package Cake.Log.Engine
* @since CakePHP(tm) v 2.4
* @license http://www.opensource.org/licenses/mit-license.php MIT License
2013-05-14 21:21:38 +00:00
*/
App::uses('BaseLog', 'Log/Engine');
/**
* Syslog stream for Logging. Writes logs to the system logger
*
* @package Cake.Log.Engine
*/
class SyslogLog extends BaseLog {
/**
* By default messages are formatted as:
* type: message
*
* To override the log format (e.g. to add your own info) define the format key when configuring
* this logger
*
* If you wish to include a prefix to all messages, for instance to identify the
2013-05-15 07:52:25 +00:00
* application or the web server, then use the prefix option. Please keep in mind
* the prefix is shared by all streams using syslog, as it is dependent of
* the running process. For a local prefix, to be used only by one stream, you
* can use the format key.
2013-05-14 21:21:38 +00:00
*
* ## Example:
*
* ```
2013-05-14 21:21:38 +00:00
* CakeLog::config('error', array(
2013-06-21 14:43:01 +00:00
* 'engine' => 'Syslog',
2013-05-14 21:21:38 +00:00
* 'types' => array('emergency', 'alert', 'critical', 'error'),
* 'format' => "%s: My-App - %s",
* 'prefix' => 'Web Server 01'
* ));
* ```
2013-05-14 21:21:38 +00:00
*
* @var array
*/
protected $_defaults = array(
'format' => '%s: %s',
'flag' => LOG_ODELAY,
'prefix' => '',
'facility' => LOG_USER
);
/**
* Used to map the string names back to their LOG_* constants
*
* @var array
*/
protected $_priorityMap = array(
'emergency' => LOG_EMERG,
'alert' => LOG_ALERT,
'critical' => LOG_CRIT,
'error' => LOG_ERR,
'warning' => LOG_WARNING,
'notice' => LOG_NOTICE,
'info' => LOG_INFO,
'debug' => LOG_DEBUG
);
/**
* Whether the logger connection is open or not
*
* @var bool
2013-05-14 21:21:38 +00:00
*/
protected $_open = false;
/**
* Make sure the configuration contains the format parameter, by default it uses
* the error number and the type as a prefix to the message
*
2014-05-31 21:36:05 +00:00
* @param array $config Options list.
2013-05-14 21:21:38 +00:00
*/
public function __construct($config = array()) {
$config += $this->_defaults;
parent::__construct($config);
}
/**
* Writes a message to syslog
*
* Map the $type back to a LOG_ constant value, split multi-line messages into multiple
* log messages, pass all messages through the format defined in the configuration
*
* @param string $type The type of log you are making.
* @param string $message The message you want to log.
* @return bool success of write.
2013-05-14 21:21:38 +00:00
*/
public function write($type, $message) {
if (!$this->_open) {
$config = $this->_config;
$this->_open($config['prefix'], $config['flag'], $config['facility']);
$this->_open = true;
}
$priority = LOG_DEBUG;
if (isset($this->_priorityMap[$type])) {
$priority = $this->_priorityMap[$type];
}
$messages = explode("\n", $message);
foreach ($messages as $message) {
$message = sprintf($this->_config['format'], $type, $message);
$this->_write($priority, $message);
}
return true;
}
/**
2013-05-16 14:06:17 +00:00
* Extracts the call to openlog() in order to run unit tests on it. This function
* will initialize the connection to the system logger
2013-05-14 21:21:38 +00:00
*
2013-05-15 07:52:25 +00:00
* @param string $ident the prefix to add to all messages logged
* @param int $options the options flags to be used for logged messages
* @param int $facility the stream or facility to log to
2013-05-14 21:21:38 +00:00
* @return void
*/
2013-05-14 21:52:39 +00:00
protected function _open($ident, $options, $facility) {
2013-05-14 21:21:38 +00:00
openlog($ident, $options, $facility);
}
/**
2013-05-16 14:06:17 +00:00
* Extracts the call to syslog() in order to run unit tests on it. This function
* will perform the actual write in the system logger
2013-05-14 21:21:38 +00:00
*
* @param int $priority Message priority.
2014-05-31 21:36:05 +00:00
* @param string $message Message to log.
* @return bool
2013-05-14 21:21:38 +00:00
*/
2013-05-15 07:52:25 +00:00
protected function _write($priority, $message) {
return syslog($priority, $message);
2013-05-14 21:21:38 +00:00
}
/**
* Closes the logger connection
2013-10-15 00:07:36 +00:00
*/
2013-05-14 21:21:38 +00:00
public function __destruct() {
closelog();
}
}