0

I'm attempting to use monolog in an application that contains a startup page, a Configuration class called by startup, and a lot of MVC type classes. Trying to figure out a way to make logging easily accessible. (I read this, which was helpful, and tried (failed, so far) to understand DI. But I've ended up with this, which is kind of a singleton and seems to work. Can someone point out the flaws?

 class MxLogger
    {
        private static  $logger = null;  

         protected function __construct (){
            $name='f3.log';
            $logger = new Logger('f3');
            $logger->pushHandler(new StreamHandler(LOGPATH . "/$name", Logger::DEBUG));
            $logger->pushProcessor(new IntrospectionProcessor(LOGGER::DEBUG,['Configuration'],0));
            self::$logger = $logger;
        }

        public static function getLogger(){
            if (self::$logger === null ) {
                new MxLogger();
            }
            return self::$logger;
        }

    }

and then in other classes:

MxLogger::getlogger()->info("Testing on line 27");

1 Answers1

0

You could wrap a few of the logger methods to make them more easily and uniformly accessible like so:

public static info($text) {    
    self::getLogger()->info($text)    
}

and then maybe change the access modifier of getLogger to protected.

asiop
  • 707
  • 4
  • 11