mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Adding initial implementation of Logging streams.
This commit is contained in:
parent
ceb7826db0
commit
6523fb1d47
2 changed files with 141 additions and 13 deletions
|
@ -55,16 +55,89 @@
|
||||||
*/
|
*/
|
||||||
class CakeLog {
|
class CakeLog {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An array of connected streams.
|
||||||
|
* Each stream represents a callable that will be called when write() is called.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access protected
|
||||||
|
**/
|
||||||
|
var $_streams = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an instance
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
* @static
|
||||||
|
**/
|
||||||
|
function getInstance() {
|
||||||
|
static $instance = array();
|
||||||
|
if (!isset($instance[0])) {
|
||||||
|
$instance[0] = new CakeLog();
|
||||||
|
}
|
||||||
|
return $instance[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the keynames of the currently active streams
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @static
|
||||||
|
**/
|
||||||
|
function streams() {
|
||||||
|
$self = CakeLog::getInstance();
|
||||||
|
return array_keys($self->_streams);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a stream from the active streams. Once a stream has been removed
|
||||||
|
* it will no longer be called.
|
||||||
|
*
|
||||||
|
* @param string $keyname Key name of callable to remove.
|
||||||
|
* @return void
|
||||||
|
* @static
|
||||||
|
**/
|
||||||
|
function removeStream($streamName) {
|
||||||
|
$self = CakeLog::getInstance();
|
||||||
|
unset($self->_streams[$streamName]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a stream the logger.
|
||||||
|
* Streams represent destinations for log messages. Each stream can connect to
|
||||||
|
* a different resource /interface and capture/write output to that source.
|
||||||
|
*
|
||||||
|
* @param string $key Keyname of config.
|
||||||
|
* @param array $config Array of config information for the LogStream
|
||||||
|
* @return boolean success
|
||||||
|
**/
|
||||||
|
function addStream($key, $config) {
|
||||||
|
$self = CakeLog::getInstance();
|
||||||
|
$self->_streams[$key] = $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures the automatic/default stream a FileLogger.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function _autoConfig() {
|
||||||
|
if (!class_exists('FileLogger')) {
|
||||||
|
require LIBS . 'log' . DS . 'file.php';
|
||||||
|
}
|
||||||
|
$this->_streams['default'] = array('FileLogger', 'write');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes given message to a log file in the logs directory.
|
* Writes given message to a log file in the logs directory.
|
||||||
*
|
*
|
||||||
* @param string $type Type of log, becomes part of the log's filename
|
* @param string $type Type of log, becomes part of the log's filename
|
||||||
* @param string $msg Message to log
|
* @param string $message Message to log
|
||||||
* @return boolean Success
|
* @return boolean Success
|
||||||
* @access public
|
* @access public
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
function write($type, $msg) {
|
function write($type, $message) {
|
||||||
if (!defined('LOG_ERROR')) {
|
if (!defined('LOG_ERROR')) {
|
||||||
define('LOG_ERROR', 2);
|
define('LOG_ERROR', 2);
|
||||||
}
|
}
|
||||||
|
@ -83,18 +156,12 @@ class CakeLog {
|
||||||
if (is_int($type) && isset($levels[$type])) {
|
if (is_int($type) && isset($levels[$type])) {
|
||||||
$type = $levels[$type];
|
$type = $levels[$type];
|
||||||
}
|
}
|
||||||
|
$self = CakeLog::getInstance();
|
||||||
if ($type == 'error' || $type == 'warning') {
|
if (empty($self->_streams)) {
|
||||||
$filename = LOGS . 'error.log';
|
$self->_autoConfig();
|
||||||
} elseif (in_array($type, $levels)) {
|
|
||||||
$filename = LOGS . 'debug.log';
|
|
||||||
} else {
|
|
||||||
$filename = LOGS . $type . '.log';
|
|
||||||
}
|
}
|
||||||
$output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $msg . "\n";
|
foreach ($self->_streams as $key => $callable) {
|
||||||
$log = new File($filename, true);
|
call_user_func($callable, $type, $message);
|
||||||
if ($log->writable()) {
|
|
||||||
return $log->append($output);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
61
cake/libs/log/file.php
Normal file
61
cake/libs/log/file.php
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* File Storage stream for Logging
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* PHP versions 4 and 5
|
||||||
|
*
|
||||||
|
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
||||||
|
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||||
|
*
|
||||||
|
* Licensed under The MIT License
|
||||||
|
* Redistributions of files must retain the above copyright notice.
|
||||||
|
*
|
||||||
|
* @filesource
|
||||||
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
||||||
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
||||||
|
* @package cake
|
||||||
|
* @subpackage cake.cake.libs.log
|
||||||
|
* @since CakePHP(tm) v 1.3
|
||||||
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* File Storage stream for Logging
|
||||||
|
*
|
||||||
|
* @package cake
|
||||||
|
* @subpackage cake.cake.libs.log
|
||||||
|
*/
|
||||||
|
class FileLogger {
|
||||||
|
/**
|
||||||
|
* Implements writing to log files.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function write($type, $message) {
|
||||||
|
$levels = array(
|
||||||
|
LOG_WARNING => 'warning',
|
||||||
|
LOG_NOTICE => 'notice',
|
||||||
|
LOG_INFO => 'info',
|
||||||
|
LOG_DEBUG => 'debug',
|
||||||
|
LOG_ERR => 'error',
|
||||||
|
LOG_ERROR => 'error'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (is_int($type) && isset($levels[$type])) {
|
||||||
|
$type = $levels[$type];
|
||||||
|
}
|
||||||
|
if ($type == 'error' || $type == 'warning') {
|
||||||
|
$filename = LOGS . 'error.log';
|
||||||
|
} elseif (in_array($type, $levels)) {
|
||||||
|
$filename = LOGS . 'debug.log';
|
||||||
|
} else {
|
||||||
|
$filename = LOGS . $type . '.log';
|
||||||
|
}
|
||||||
|
$output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
|
||||||
|
$log = new File($filename, true);
|
||||||
|
if ($log->writable()) {
|
||||||
|
return $log->append($output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue