Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress - How do I set error log timestamp timezone to local?

WordPress uses UTC / GMT for all its timestamps in the PHP error log. I would like to have the timestamps in the local server timezone. Changing the timezone in WP settings does not help; this affects time displays but not timestamps in the error log.

(I know there is a line in the wp_settings.php file which sets this, and I am given to understand changing that setting messes up parts of the WP code hardcoded to use UTC.)

I have looked at overriding the PHP error_log function to parse the time and replace it, but the two methods I found required having something extra installed (APD or Runkit). As I understand it, these are for DEV environments only, so I don't want to mess with them.

Any suggestion on how to get WP to write local timezone stamps to the error log?

like image 887
Andrew Walker Avatar asked Nov 01 '22 21:11

Andrew Walker


1 Answers

There's a reason for logging time in UTC. Many time zones use daylight saving time. For those zones, there's an hour in the spring that is skipped, and an hour in the fall that is duplicated. If you were to log using the local time, there would not be an easy way to disambiguate between the duplicated values.

For example, if you are in the US Eastern time zone ("America/New_York") and you logged using local time, a value like 2014-11-02 01:30:00 could mean either 1:30 in Eastern Daylight Time, or 1:30 in Eastern Standard Time - an hour later.

If you log frequently enough, you might be able to detect this by comparing the timestamps of other items in nearby log entries. But in general, that's not a great solution because you might only log occasionally, or you might not want the overhead of analyzing more than one log entry at a time.

Besides daylight saving time - there's also the issue that if you take log files from multiple servers, they should be able to be compared uniformly. Perhaps I have web servers on the US East coast and West coast, one in Europe, and one in Japan. If there's a spike in my global traffic - I shouldn't have to do time zone conversions to line things up.

If you really must log in local time - then consider including the offset from UTC along with the timestamp. This is known as a "DateTimeOffset" in some languages, and is also part of the ISO-8601 standard. For example, "2014-06-20T01:23:45-07:00". By doing this, you at least allow for conversion back to UTC and remove any ambiguity caused by daylight saving time.

I don't know if there's a specific way to have WordPress or PHP log in this manner, but perhaps someone else can offer that as a separate answer.

like image 91
Matt Johnson-Pint Avatar answered Nov 10 '22 20:11

Matt Johnson-Pint