2009-08-19 05:55:23 +02:00
|
|
|
<?php
|
2010-04-23 02:11:41 +02:00
|
|
|
require_once 'Zend/Log.php';
|
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
/**
|
|
|
|
* Wrapper class for a logging handler like {@link Zend_Log}
|
|
|
|
* which takes a message (or a map of context variables) and
|
|
|
|
* sends it to one or more {@link Zend_Log_Writer_Abstract}
|
|
|
|
* subclasses for output.
|
|
|
|
*
|
2009-08-19 07:41:58 +02:00
|
|
|
* These priorities are currently supported:
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* - SS_Log::ERR
|
|
|
|
* - SS_Log::WARN
|
|
|
|
* - SS_Log::NOTICE
|
2009-08-19 05:55:23 +02:00
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* You can add an error writer by calling {@link SS_Log::add_writer()}
|
2009-08-19 05:55:23 +02:00
|
|
|
*
|
2009-08-19 23:55:03 +02:00
|
|
|
* Example usage of logging errors by email notification:
|
2009-08-19 05:55:23 +02:00
|
|
|
* <code>
|
2009-12-16 06:40:40 +01:00
|
|
|
* SS_Log::add_writer(new SS_LogEmailWriter('my@email.com'), SS_Log::ERR);
|
2009-08-19 23:55:03 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Example usage of logging errors by file:
|
|
|
|
* <code>
|
2009-12-16 06:40:40 +01:00
|
|
|
* SS_Log::add_writer(new SS_LogFileWriter('/var/log/silverstripe/errors.log'), SS_Log::ERR);
|
2009-08-19 23:55:03 +02:00
|
|
|
* </code>
|
|
|
|
*
|
2009-12-16 06:40:40 +01:00
|
|
|
* Example usage of logging at warnings and errors by setting the priority to '<=':
|
|
|
|
* <code>
|
|
|
|
* SS_Log::add_writer(new SS_LogEmailWriter('my@email.com'), SS_Log::WARN, '<=');
|
|
|
|
* </code>
|
|
|
|
*
|
2009-08-19 23:55:03 +02:00
|
|
|
* Each writer object can be assigned a formatter. The formatter is
|
|
|
|
* responsible for formatting the message before giving it to the writer.
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* {@link SS_LogErrorEmailFormatter} is such an example that formats errors
|
2009-08-19 23:55:03 +02:00
|
|
|
* into HTML for human readability in an email client.
|
|
|
|
*
|
|
|
|
* Formatters are added to writers like this:
|
|
|
|
* <code>
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* $logEmailWriter = new SS_LogEmailWriter('my@email.com');
|
2009-08-19 23:55:03 +02:00
|
|
|
* $myEmailFormatter = new MyLogEmailFormatter();
|
|
|
|
* $logEmailWriter->setFormatter($myEmailFormatter);
|
2009-08-19 05:55:23 +02:00
|
|
|
* </code>
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-08-19 05:55:23 +02:00
|
|
|
* @subpackage dev
|
|
|
|
*/
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
class SS_Log {
|
2009-08-19 05:55:23 +02:00
|
|
|
|
|
|
|
const ERR = Zend_Log::ERR;
|
|
|
|
const WARN = Zend_Log::WARN;
|
2009-08-19 07:41:58 +02:00
|
|
|
const NOTICE = Zend_Log::NOTICE;
|
2009-08-19 05:55:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logger class to use.
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @see SS_Log::get_logger()
|
2009-08-19 05:55:23 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
public static $logger_class = 'SS_ZendLog';
|
2009-08-19 05:55:23 +02:00
|
|
|
|
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @see SS_Log::get_logger()
|
2009-08-19 05:55:23 +02:00
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected static $logger;
|
|
|
|
|
|
|
|
/**
|
2012-02-06 17:52:19 +01:00
|
|
|
* @var array Logs additional context from PHP's superglobals.
|
|
|
|
* Caution: Depends on logger implementation (mainly targeted at {@link SS_LogEmailWriter}).
|
|
|
|
* @see http://framework.zend.com/manual/en/zend.log.overview.html#zend.log.overview.understanding-fields
|
|
|
|
*/
|
|
|
|
static $log_globals = array(
|
|
|
|
'_SERVER' => array(
|
|
|
|
'HTTP_ACCEPT',
|
|
|
|
'HTTP_ACCEPT_CHARSET',
|
|
|
|
'HTTP_ACCEPT_ENCODING',
|
|
|
|
'HTTP_ACCEPT_LANGUAGE',
|
|
|
|
'HTTP_REFERRER',
|
|
|
|
'HTTP_USER_AGENT',
|
|
|
|
'HTTPS',
|
|
|
|
'REMOTE_ADDR',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the logger currently in use, or create a new one if it doesn't exist.
|
2009-08-19 05:55:23 +02:00
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public static function get_logger() {
|
2012-10-04 06:40:03 +02:00
|
|
|
if(!static::$logger) {
|
2012-02-06 17:52:19 +01:00
|
|
|
// Create default logger
|
2012-10-04 06:40:03 +02:00
|
|
|
static::$logger = new static::$logger_class;
|
2012-02-06 17:52:19 +01:00
|
|
|
|
|
|
|
// Add default context (shouldn't change until the actual log event happens)
|
2012-10-04 06:40:03 +02:00
|
|
|
foreach(static::$log_globals as $globalName => $keys) {
|
2012-02-06 17:52:19 +01:00
|
|
|
foreach($keys as $key) {
|
|
|
|
$val = @$GLOBALS[$globalName][$key];
|
2012-10-04 06:40:03 +02:00
|
|
|
static::$logger->setEventItem(sprintf('$%s[\'%s\']', $globalName, $key), $val);
|
2012-02-06 17:52:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
}
|
2012-10-04 06:40:03 +02:00
|
|
|
return static::$logger;
|
2009-08-19 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all writers in use by the logger.
|
|
|
|
* @return array Collection of Zend_Log_Writer_Abstract instances
|
|
|
|
*/
|
|
|
|
public static function get_writers() {
|
2012-10-04 06:40:03 +02:00
|
|
|
return static::get_logger()->getWriters();
|
2009-08-19 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
2009-08-19 23:55:03 +02:00
|
|
|
/**
|
|
|
|
* Remove all writers currently in use.
|
|
|
|
*/
|
|
|
|
public static function clear_writers() {
|
2012-10-04 06:40:03 +02:00
|
|
|
static::get_logger()->clearWriters();
|
2009-08-19 23:55:03 +02:00
|
|
|
}
|
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
/**
|
|
|
|
* Remove a writer instance from the logger.
|
|
|
|
* @param object $writer Zend_Log_Writer_Abstract instance
|
|
|
|
*/
|
|
|
|
public static function remove_writer($writer) {
|
2012-10-04 06:40:03 +02:00
|
|
|
static::get_logger()->removeWriter($writer);
|
2009-08-19 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a writer instance to the logger.
|
|
|
|
* @param object $writer Zend_Log_Writer_Abstract instance
|
2009-12-16 06:40:40 +01:00
|
|
|
* @param const $priority Priority. Possible values: SS_Log::ERR, SS_Log::WARN or SS_Log::NOTICE
|
2009-08-24 05:21:12 +02:00
|
|
|
* @param $comparison Priority comparison operator. Acts on the integer values of the error
|
|
|
|
* levels, where more serious errors are lower numbers. By default this is "=", which means only
|
|
|
|
* the given priority will be logged. Set to "<=" if you want to track errors of *at least*
|
|
|
|
* the given priority.
|
2009-08-19 05:55:23 +02:00
|
|
|
*/
|
2009-08-24 05:21:12 +02:00
|
|
|
public static function add_writer($writer, $priority = null, $comparison = '=') {
|
|
|
|
if($priority) $writer->addFilter(new Zend_Log_Filter_Priority($priority, $comparison));
|
2012-10-04 06:40:03 +02:00
|
|
|
static::get_logger()->addWriter($writer);
|
2009-08-19 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch a message by priority level.
|
|
|
|
*
|
|
|
|
* The message parameter can be either a string (a simple error
|
|
|
|
* message), or an array of variables. The latter is useful for passing
|
|
|
|
* along a list of debug information for the writer to handle, such as
|
|
|
|
* error code, error line, error context (backtrace).
|
|
|
|
*
|
2009-12-16 06:40:52 +01:00
|
|
|
* @param mixed $message Exception object or array of error context variables
|
2009-12-16 06:40:40 +01:00
|
|
|
* @param const $priority Priority. Possible values: SS_Log::ERR, SS_Log::WARN or SS_Log::NOTICE
|
2012-02-06 17:52:19 +01:00
|
|
|
* @param mixed $extras Extra information to log in event
|
2009-08-19 05:55:23 +02:00
|
|
|
*/
|
2012-02-06 17:52:19 +01:00
|
|
|
public static function log($message, $priority, $extras = null) {
|
2009-12-16 06:40:52 +01:00
|
|
|
if($message instanceof Exception) {
|
|
|
|
$message = array(
|
|
|
|
'errno' => '',
|
|
|
|
'errstr' => $message->getMessage(),
|
|
|
|
'errfile' => $message->getFile(),
|
|
|
|
'errline' => $message->getLine(),
|
|
|
|
'errcontext' => $message->getTrace()
|
|
|
|
);
|
2011-08-23 14:34:59 +02:00
|
|
|
} elseif(is_string($message)) {
|
|
|
|
$trace = SS_Backtrace::filtered_backtrace();
|
|
|
|
$lastTrace = $trace[0];
|
|
|
|
$message = array(
|
|
|
|
'errno' => '',
|
|
|
|
'errstr' => $message,
|
|
|
|
'errfile' => @$lastTrace['file'],
|
|
|
|
'errline' => @$lastTrace['line'],
|
|
|
|
'errcontext' => $trace
|
|
|
|
);
|
2009-12-16 06:40:52 +01:00
|
|
|
}
|
2009-08-19 05:55:23 +02:00
|
|
|
try {
|
2012-10-04 06:40:03 +02:00
|
|
|
static::get_logger()->log($message, $priority, $extras);
|
2009-08-19 05:55:23 +02:00
|
|
|
} catch(Exception $e) {
|
|
|
|
// @todo How do we handle exceptions thrown from Zend_Log?
|
|
|
|
// For example, an exception is thrown if no writers are added
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|