phpsymfonyloggingmonolog

How to change Monolog's default date timezone globally in Symfony 2?


I Am using Monolog in Symfony 2 to do logging, Monolog uses default timezone set in php.ini and fallbacks to UTC.

There is no a configuration parameter that determines the timezone for Monolog.

One way to change the used timezone is to use \Monolog\Logger::setTimezone(), but I don't want to iterate at everytime I use logging and make sure I used the \Monolog\Logger::setTimezone().

As timezone is used globally for all logger instances If I somehow set timezone in some sort of an init function before it is used? My problem is Symfony handles Monolog's instantiation and injection, is there is a way where I can tell Symfony to use setTimezone() after instantiating the logger instance?


Solution

  • namespace AppBundle;
    
    use Monolog\Logger;
    use Symfony\Component\HttpKernel\Bundle\Bundle;
    
    class AppBundle extends Bundle
    {
        public function boot()
        {
            parent::boot(); // TODO: Change the autogenerated stub
            Logger::setTimezone(new \DateTimeZone('UTC'));
        }
    }