Question

I have php file which contains class "FooClass" uses in a few projects. I recently add method for logging with log4net like this:

private static function log($message,$level=0)
{
    if(!isset(self::$logger))
    {
        if(!class_exists("Logger")) return;
        self::$logger = Logger::getLogger("DBw");
        if(!isset(self::$logger)) return;
    }
    //logging
}

Some files that use FooClass can define the configuration by ising Logger::configure("config.xml"). But others don't define it, and in that case the FooClass start to use LoggerConfiguratorDefault, which trace logs as "echo". This is not acceptable.

I want to see if the configuration is not defined by other files, the FooClass did not log with LoggerConfiguratorDefault.

How can I do disable LoggerConfiguratorDefault or detect using of that and make return from function?

Was it helpful?

Solution

I was in the same position, slightly different case: My configuration object does log if I access a value, but it cannot log if I access the config value for the configuration file of the logger.

The class Loggerof log4php has a private static property $initialized, which is accessed through a private static method isInitialized(). I solved my problem by using Reflection to access the value of the static property, and if the result was false I returned a self-created NullLogger extends Logger that does nothing if any logging methods are called.

Your class might be able to do the same, but the better solution will be to ensure log4php always is initialized.

I'd suggest that you create a ticket in the projects Jira to have this issue resolved more easily. There really is no need to have the Logger::isInitialized() method private.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top