Yii Logs And Timezone

Hi all,

My situation is that I have multiple users access my project from different timezones.

Each user is able to set their timezone.

When I log something using Yii::log it uses the currently set timezone when outputting the date and time of log entry.

This causes an issue as I cannot determine when the log entry was actually entered without having to check the users set timezone. That is assuming I even know which user triggered the log entry.

Does anyone know of a way to have the Yii logger always output using the GMT timezone?

Thanks,

C

I had the same problem. My solution is to add onEndRequest handler in configuration file:




// This is the main Web application configuration. Any writable

// CWebApplication properties can be configured here.

return array(


    ...


    'onEndRequest' => function() {

        // set timezone to UTC for logging

        date_default_timezone_set('UTC');

    },


    ...

);



Normally Yii writes in logs on end of request, so we can set UTC timezone when user request has already processed, but before actual writing in logs. Yii executes event handlers in same order in which they registered, our function will be executed before writing in logs. I used anonymous function here, for PHP < 5.3 you can use callback:




// This is the main Web application configuration. Any writable

// CWebApplication properties can be configured here.

return array(


    ...


    'onEndRequest' => array('MyClass', 'MethodInMyClass'),


    ...

);



I suppose that this will not work if you will flush logs "manually", then you should specify correct timezone before and after flush.

May be other solutions exists, redefine some method in logging?