lnav icon indicating copy to clipboard operation
lnav copied to clipboard

Honor the TZ variable by converting timezones on the fly

Open jefft opened this issue 4 years ago • 6 comments

I have a server running on UTC, with apps that log UTC dates without a timezone specifier:

2019-12-10 21:37:55,685 WARN .....

I also have pesky users who report problems in non-UTC, like "4pm in Los Angeles".

I would love to be able to run:

TZ='America/Los_Angeles' lnav atlassian-confluence.log

and have lnav translate those UTC dates to (in this case) PST.

jefft avatar Dec 11 '19 05:12 jefft

I came across similar time zone issues between different logging sources on a single machine... I think it was in how the time was shown (Summer time on or off etc).

cw1nte avatar May 09 '20 19:05 cw1nte

This works for some log formats (like JSON logs where the displayed log message is generated dynamically), but is not consistently supported. The :adjust-log-time command should work for all formats, but only works a file-at-a-time. You can use a SQL query to update the time_offset column of the lnav_file table to do it en masse.

tstack avatar Dec 12 '20 20:12 tstack

Thanks for pointing me here. Agreed, :adjust-log-time / time_offset works, but this does require knowledge on the current time differential. When reviewing logs that span several weeks this isn't necessarily feasible because the time differential is dependent on the time of the year due daylight savings, and thus isn't constant. Would you be able to add a feature like @jefft mentioned? I think it would be incredibly useful to start lnav with a given timezone and all timestamps will be converted accordingly.

EDIT: it also seems that :adjust-log-time doesn't work well if used across many different files (i.e. when launching lnav *.log). Only seems to adapt the latest file in my case, unless I am missing something.

NikEyX avatar Jan 01 '21 23:01 NikEyX

I've pushed a change that will convert timestamps that include a timezone to the local time. Unfortunately, this won't quite work for the original request since 2019-12-10 21:37:55,685 doesn't have Z or a numeric offset (like -03:00). I'm not sure what to do about that yet. Let me know if you have any thoughts.

Oof, a year before I first responded and then almost four before actually doing something about it. Sorry folks...

tstack avatar Aug 11 '23 16:08 tstack

This is a really useful change, thanks! So for a sample access.log:

localhost:443 1.2.3.4 cger1e khenry [10/Aug/2023:01:00:00 -0700] "GET /bar HTTP/2.0" 200 16517 "-" "Mozilla/5.0" 156302
localhost:443 1.2.3.4 1j0vhgd jsmith [10/Aug/2023:02:00:00 -0700] "GET /foo HTTP/2.0" 200 17163 "-" "Mozilla/5.0" 116075

Old lnav showed the timestamps in their native timezone:

image

Now lnav shows the timestamps in the computer's local timezone (here AEST, +1000):

image

or to whatever TZ says, e.g. TZ=UTC:

image

log_time and log_actual_time are translated too. All excellent.

There is a minor bug, in that the timezone offset is always +0000. Try TZ=Australia/Sydney (aka AEST aka +1000):

image

The first timestamp should be 10/Aug/2023:18:00:00 +1000, not 10/Aug/2023:18:00:00 +0000.

jefft avatar Aug 15 '23 03:08 jefft

@jefft Thanks for hanging around and trying out the change, I really appreciate it! I've pushed a change to show the offset. Here's your example:

Screen Region 2023-08-15 at 06 43 38

tstack avatar Aug 15 '23 13:08 tstack