0

I am getting error in below code, cause am not able to access $log in static function Log which gets initialized in _construct.

class Logger extends Singleton{

    protected function __construct() {
        if(!class_exists("Log")) {
            include '/usr/php/Log.php';
        }
        $MONITORING_LOGFILE = "/var/log/Monitoring.log";

        ini_set('error_log', 'syslog');
        openlog($MONITORING_LOGFILE, LOG_NDELAY, LOG_LOCAL0);
        $log = Log::singleton('syslog', LOG_LOCAL0, $MONITORING_LOGFILE, array('lineFormat' => ' %{message}'), PEAR_LOG_DEBUG);
    }


    public static function Log($message){
        $log->err($message);
    }
}

Ok, I modified the above code

class Logger extends Singleton{
    private $log;

    protected function __construct() {
        if(!class_exists("Log")) {
            include '/usr/php/Log.php';
        }
        $MONITORING_LOGFILE = "/var/log/Monitoring.log";

        ini_set('error_log', 'syslog');
        openlog($MONITORING_LOGFILE, LOG_NDELAY, LOG_LOCAL0);
        $this->log = Log::singleton('syslog', LOG_LOCAL0, $MONITORING_LOGFILE, array('lineFormat' => ' %{message}'), PEAR_LOG_DEBUG);
    }


    public function Log($message){
        $this->log->err($message);
    }
}

and now its working fine .... just want to confirm if initializng like this is ok in Singleton pattern?

2
  • The constructor is not called if you just invoke Logger::Log statically. You need to retrieve the singleton instance of Logger from inside Log(). And you need to save $log in the constructor, or it's just a local variable that will get lost. Commented Feb 2, 2015 at 7:02
  • And what? Have you tried anything else? Commented Feb 2, 2015 at 7:03

1 Answer 1

1

To be able to access the $log variable trough a static function you need to have a reference of it:

class Logger extends Singleton{
    private static $log; //static instance of Log::singleton
    protected function __construct() {
        if(!class_exists("Log")) {
            include '/usr/php/Log.php';
        }
        $MONITORING_LOGFILE = "/var/log/Monitoring.log";

        ini_set('error_log', 'syslog');
        openlog($MONITORING_LOGFILE, LOG_NDELAY, LOG_LOCAL0);
        self::$log = Log::singleton('syslog', LOG_LOCAL0, $MONITORING_LOGFILE, array('lineFormat' => ' %{message}'), PEAR_LOG_DEBUG);
    }

    //static method
    public static function Log($message){
        self::$log->err($message);
    }
}

To create your instance of the class Logger and access the static Log function you can do the following:

$mylog = new Logger();
$mylog::Log("Your text here");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Kheshav. I cannot create an object of Logger as its extending Singleton pattern. I accessed it as Logger::getInstance()->Log("Message"); The above code works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.