mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Make it possible to extend SS_Log
Using late static binding makes it possible to override SS_Log to create logs which are separate to the main Silverstripe log but still use the built in functionality. Add test for SS_Log subclassing.
This commit is contained in:
parent
5d127e1254
commit
de36063d15
20
dev/Log.php
20
dev/Log.php
@ -87,20 +87,20 @@ class SS_Log {
|
|||||||
* @return object
|
* @return object
|
||||||
*/
|
*/
|
||||||
public static function get_logger() {
|
public static function get_logger() {
|
||||||
if(!self::$logger) {
|
if(!static::$logger) {
|
||||||
// Create default logger
|
// Create default logger
|
||||||
self::$logger = new self::$logger_class;
|
static::$logger = new static::$logger_class;
|
||||||
|
|
||||||
// Add default context (shouldn't change until the actual log event happens)
|
// Add default context (shouldn't change until the actual log event happens)
|
||||||
foreach(self::$log_globals as $globalName => $keys) {
|
foreach(static::$log_globals as $globalName => $keys) {
|
||||||
foreach($keys as $key) {
|
foreach($keys as $key) {
|
||||||
$val = @$GLOBALS[$globalName][$key];
|
$val = @$GLOBALS[$globalName][$key];
|
||||||
self::$logger->setEventItem(sprintf('$%s[\'%s\']', $globalName, $key), $val);
|
static::$logger->setEventItem(sprintf('$%s[\'%s\']', $globalName, $key), $val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return self::$logger;
|
return static::$logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -108,14 +108,14 @@ class SS_Log {
|
|||||||
* @return array Collection of Zend_Log_Writer_Abstract instances
|
* @return array Collection of Zend_Log_Writer_Abstract instances
|
||||||
*/
|
*/
|
||||||
public static function get_writers() {
|
public static function get_writers() {
|
||||||
return self::get_logger()->getWriters();
|
return static::get_logger()->getWriters();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove all writers currently in use.
|
* Remove all writers currently in use.
|
||||||
*/
|
*/
|
||||||
public static function clear_writers() {
|
public static function clear_writers() {
|
||||||
self::get_logger()->clearWriters();
|
static::get_logger()->clearWriters();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,7 +123,7 @@ class SS_Log {
|
|||||||
* @param object $writer Zend_Log_Writer_Abstract instance
|
* @param object $writer Zend_Log_Writer_Abstract instance
|
||||||
*/
|
*/
|
||||||
public static function remove_writer($writer) {
|
public static function remove_writer($writer) {
|
||||||
self::get_logger()->removeWriter($writer);
|
static::get_logger()->removeWriter($writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -137,7 +137,7 @@ class SS_Log {
|
|||||||
*/
|
*/
|
||||||
public static function add_writer($writer, $priority = null, $comparison = '=') {
|
public static function add_writer($writer, $priority = null, $comparison = '=') {
|
||||||
if($priority) $writer->addFilter(new Zend_Log_Filter_Priority($priority, $comparison));
|
if($priority) $writer->addFilter(new Zend_Log_Filter_Priority($priority, $comparison));
|
||||||
self::get_logger()->addWriter($writer);
|
static::get_logger()->addWriter($writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -173,7 +173,7 @@ class SS_Log {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
self::get_logger()->log($message, $priority, $extras);
|
static::get_logger()->log($message, $priority, $extras);
|
||||||
} catch(Exception $e) {
|
} catch(Exception $e) {
|
||||||
// @todo How do we handle exceptions thrown from Zend_Log?
|
// @todo How do we handle exceptions thrown from Zend_Log?
|
||||||
// For example, an exception is thrown if no writers are added
|
// For example, an exception is thrown if no writers are added
|
||||||
|
@ -64,5 +64,13 @@ class SS_LogTest extends SapphireTest {
|
|||||||
'Serializes arrays correctly'
|
'Serializes arrays correctly'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubclassedLogger() {
|
||||||
|
$this->assertTrue(SS_Log::get_logger() !== SS_LogTest_NewLogger::get_logger());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class SS_LogTest_NewLogger extends SS_Log {
|
||||||
|
protected static $logger;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user