How to gracefully disable a PHP logging class
So i've got this logger (actually a wrapper), which is accessed all over the app with Log::getInstance()->log_this
or ->log_that
, etc. There is a config param in an ini which turns logging off, but the implementation to disable LogWrapper isn't quite right:
private function __construct() {
if(config::get('logging_enabled')) {
$this->logger = new Log();
}
//else?
}
public static function getInstance() {
if(self::$_instance == NULL)开发者_开发问答 {
self::$_instance = new LogWrapper();
}
return self::$_instance;
}
Calls to getInstance()->any_method
delegate to the $logger instance, but will fail because $logger isn't anything.
Where can i stop this flow within this class? It's not an option to modify the Log class, nor can i go through each call in the app to LogWrapper. I can't seem to get my head around this, and I know one smartly-placed line would fix it...
thanks for any help!
Why not implement a dummy logger-class, that wouldn't log anything ?
And, then, instanciate either Log
or that dummy-useless class, depending on the configuration :
if(config::get('logging_enabled')) {
$this->logger = new Log();
}
else {
$this->logger = new DummyLogThatDoesntLog();
}
精彩评论