Browse files

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.
  • Loading branch information...
1 parent 5d127e1 commit de36063d155962da3041e77f8dba455b7688db2b @drzax drzax committed Oct 4, 2012
Showing with 18 additions and 10 deletions.
  1. +10 −10 dev/Log.php
  2. +8 −0 tests/dev/LogTest.php
View
20 dev/Log.php
@@ -87,43 +87,43 @@ class SS_Log {
* @return object
*/
public static function get_logger() {
- if(!self::$logger) {
+ if(!static::$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)
- foreach(self::$log_globals as $globalName => $keys) {
+ foreach(static::$log_globals as $globalName => $keys) {
foreach($keys as $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;
}
/**
* Get all writers in use by the logger.
* @return array Collection of Zend_Log_Writer_Abstract instances
*/
public static function get_writers() {
- return self::get_logger()->getWriters();
+ return static::get_logger()->getWriters();
}
/**
* Remove all writers currently in use.
*/
public static function clear_writers() {
- self::get_logger()->clearWriters();
+ static::get_logger()->clearWriters();
}
/**
* Remove a writer instance from the logger.
* @param object $writer Zend_Log_Writer_Abstract instance
*/
public static function remove_writer($writer) {
- self::get_logger()->removeWriter($writer);
+ static::get_logger()->removeWriter($writer);
}
/**
@@ -137,7 +137,7 @@ public static function remove_writer($writer) {
*/
public static function add_writer($writer, $priority = null, $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 @@ public static function log($message, $priority, $extras = null) {
);
}
try {
- self::get_logger()->log($message, $priority, $extras);
+ static::get_logger()->log($message, $priority, $extras);
} catch(Exception $e) {
// @todo How do we handle exceptions thrown from Zend_Log?
// For example, an exception is thrown if no writers are added
View
8 tests/dev/LogTest.php
@@ -64,5 +64,13 @@ public function testEmailWriter() {
'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;
+}

0 comments on commit de36063

Please sign in to comment.