You can write a monolog formatter that converts the timezone of the date.
@spekkionu, thanks for your feedback.
Is there a proper way of doing this within Laravel framework? or do I need to modify one of the core files in the monolog source inside the vendor package?
Could you please provide some insight on the best way t accomplish this? (ideally so that composer update does not rollback this change)
Thank you!
Hi, does anyone know how I can accomplish this? Please help.
Hi Avidh,
There are a number of ways you could approach this. By default Laravel uses the excellent Monolog library. Like most of the framework, this gets included into your app via a service provider. In this case that would be Illuminate\Log\LogServiceProvider which you will find listed in the 'providers' array in app/config/app.php.
The key bit is the static Monolog\Logger::$timezone property. There is no setter for this property, so a good option would be to extend Monolog\Logger and set the timezone in the constructor. In my app, I put this in a services directory (which I have mapped in composer via the psr-4 directive).
app/services/Log/Logger.php -- obviously you should change the namespace to match yours. https://gist.github.com/anaxamaxan/11024637
Now that we've set our timezone, we just need to call it. Here's my service provider.
app/services/Log/LogServiceProvider.php https://gist.github.com/anaxamaxan/11024602
The last step is just to go back to app/config/app.php. Comment out the line for Illuminate\Log\LogServiceProvider and append your new LogServiceProvider in its place. In my case, that means appending 'Sa\Services\Log\LogServiceProvider', to the providers array.
Does that help?
One last bit: as per the PHP Timezones.American docs the EST timezone is America/New_York. In my own code I have a default timezone config var, which is what I actually call in Logger.php:
static::$timezone = new \DateTimeZone(Config::get('sa.default_timezone'));
@anaxamaxan, dude.. it worked flawlessly. you are the man! thank you!
I wrestled with this for a while because there was a no way to set the $timezone property in the Monolog\Logger class and I didn't want to hard code it inside since my changes would get overwritten with the next package update but I didn't know the proper way of extending it.
Again, thank you so much for taking time to explain it in details in very clear manner.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community