Syslog Component
This article provides the code for a SysLog class for use with Cake v1.3.1 to enable
Logging to the syslog is a trivial task, but can provide benefits including better performance and a central location for your log files (if your syslogd is configured in that way). Copy/paste the code from below, or checkout a copy from github to your application. http://github.com/rikdc/Cake-Bits/blob/master/plugins/logger/libs/log/sys_log.php
The next step is to configure your application to use this code, which can be done by placing the following code in your bootstrap.php. Do NOT put this in your core.php - it won't work!
The next step is to configure your application to use this code, which can be done by placing the following code in your bootstrap.php. Do NOT put this in your core.php - it won't work!
CakeLog::config('MyLogFiles', array(
'engine' => 'SysLog',
));
<?php
/**
* Syslog Storage stream for Logging
*
* PHP versions 4 and 5
*
* Copyright 2008-2010, UGR Works Limited.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2008-2010, UGR Works Limited
* @package sunshine
* @subpackage sunshine.cake.libs.log
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* SysLog for Logging.
*
* @package sunshine
* @subpackage sunshine.cake.libs.log
*/
class SysLog {
/**
* Ident to send with the log files.
*
* @var string
*/
var $_ident = null;
/**
* The facility to use for storing log files.
*
* @var string
*/
var $_facility = null;
/**
* Constructs a new SysLog Logger.
*
* Options
*
* - `ident` the ident to be added to each message.
* - `facility` what type of application is recording a message. Default: LOG_LOCAL0. LOG_USER if Windows.
*
* @param array $options Options for the SysLog, see above.
* @return void
*/
function SysLog($options = array()) {
if ($this->isWindows()) {
$default_facility = LOG_USER;
} else {
$default_facility= LOG_LOCAL0;
}
$options += array('ident' => LOGS, 'facility' => $default_facility);
$this->_ident = $options['ident'];
$this->_facility = $options['facility'];
}
/**
* Utilty method to identify if we're running on a Windows box.
*
* @return boolean if running on windows.
*/
function isWindows() {
return (DIRECTORY_SEPARATOR == '\\' ? true : false);
}
/**
* Implements writing to the specified syslog
*
* @param string $type The type of log you are making.
* @param string $message The message you want to log.
* @return boolean success of write.
*/
function write($type, $message) {
$debugTypes = array('notice', 'info', 'debug');
$priority = LOG_INFO;
if ($type == 'error' || $type == 'warning') {
$priority = LOG_ERR;
} elseif (in_array($type, $debugTypes)) {
$priority = LOG_DEBUG;
}
$output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
if (!openlog($this->_ident, LOG_PID | LOG_PERROR, $this->_facility)) {
return false;
}
$result = syslog($priority, $output);
closelog();
return $result;
}
}
?>








Comments are closed for articles over a year old