Skip to content

Print time zone in server log.#12939

Closed
CharlesChen888 wants to merge 1 commit intoredis:unstablefrom
CharlesChen888:log-time-zone
Closed

Print time zone in server log.#12939
CharlesChen888 wants to merge 1 commit intoredis:unstablefrom
CharlesChen888:log-time-zone

Conversation

@CharlesChen888
Copy link
Contributor

To resolve #12932

Displaying time zone in the log is very helpful in many circumstances. And as a global cloud service provider, our customers often ask which time zone the time in the log is.

#12934 is very good, but the discussion of configuration items is too complicated. I think we need to stick to the original problem. In this PR a brief solution to print time zone is proposed to meet user needs. Here we only discuss how to display time zone information to avoid complicating the issue.

Currently the implementation in this PR will result in a log format shown below.

113734:M 12 Jan 2024 16:52:25.692 +0800 * Server initialized

@zuiderkwast
Copy link
Contributor

Adding time zone without config might break scripts parsing the logs. I think it's risky.

I'd prefer that we allow some config to select time format, either a custom format string or an enum of some pre-defined formats where some of them include time zone, but the default one doesn't.

A problem with a custom format string for strftime is that milliseconds can't be included, so perhaps an enum with some predifined formats is preferable. A limited number of formats may also be easier to handle for scripts attempting to be independent of this config.

@ecerulm
Copy link

ecerulm commented Jan 12, 2024

As stated in #12932 (comment), I think the issues with strftime() are in my opinion:

  • strftime format specification differences between operating systems
  • strftime usually does not allow fractional seconds/ milliseconds / microseconds

But I fully agree that the default log format should be kept as is today otherwise scripts, and log shippers like fluentd, fluentbit, logstash, etc will stop working on the new redis version, the default must be backwards compatible

If I have any say on this , one of the predefined formats should be iso8601_with_utcoffset which should contain microseconds (not truncated) and a UTC offset in the form +HH:MM as for example 2023-12-08T15:03:26.246333+00:00 and it should always be 32 chars.

I have seen implementations where the fractional seconds are omitted if they are zeros (which is legal by iso 8601) like .246000 gets truncated to .246, so please add a testcase to make sure you don't truncate the zeroes in that case.

Predefined formats that I think are useful

  • iso8601utc: 2023-12-08T15:03:26.246Z, milliseconds and Z (zulu) timezone
  • iso8601_with_utcoffset: 2023-12-08T15:03:26.24600+01:00, microsends and full utc offset.

Comment on lines +139 to +140
time_t zone = server.timezone / 3600;
snprintf(buf+off,sizeof(buf)-off,"%03d %c%02ld00",(int)tv.tv_usec/1000,zone>0?'-':'+',labs(zone));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace that "complex" logic for calculating the offset using another call to strftime using %z :

Suggested change
time_t zone = server.timezone / 3600;
snprintf(buf+off,sizeof(buf)-off,"%03d %c%02ld00",(int)tv.tv_usec/1000,zone>0?'-':'+',labs(zone));
off += snprintf(buf+off,sizeof(buf)-off,"%03d",(int)tv.tv_usec/1000);
strftime(buf+off,sizeof(buf),"%z",&tm);

The linux man page for strftime documents %z:

  %z     The +hhmm or -hhmm numeric timezone (that is, the hour and
         minute offset from UTC). (SU)

unfortunately that will produce the less desirable but equally correct +0100 (no colon) instead of +01:00 (colon between hours and minutes)

Copy link
Contributor Author

@CharlesChen888 CharlesChen888 Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strangely strftime does not work on my centOS 7 machine, or my arm cpu machine.. and that is why I did not use it. It does work on my macOS though. I will try to figure out what's going on.

@zuiderkwast
Copy link
Contributor

Predefined formats that I think are useful (...) iso8601utc, iso8601_with_utcoffset

@ecerulm Are these ISO 8601 variants defined anywhere?

@ecerulm
Copy link

ecerulm commented Jan 12, 2024

@zuiderkwast , You mean the names iso8601utc and iso8601_with_utcoffset? I made the names up I don’t think there are standardized names for them.

The format that I called iso8601utc is what JavaScript calls “isotring” (toISOString)

the format that I called iso8601_with_utcoffset is what python calls “isoformat” (datetime.isoformat())

@soloestoy
Copy link
Contributor

soloestoy commented Jan 15, 2024

Adding time zone without config might break scripts parsing the logs. I think it's risky.

I don't think this is a problem. Redis has gone through multiple versions, and its log format has also evolved. Different versions have different log formats. For example:

2.8: [pid] day month h:m:s.ms level message
4.0: pid:role day month h:m:s.ms level message
6.0: pid:role day month year h:m:s.ms level message

So scripts need to have different handling methods for different versions of Redis. In the next major version, displaying time zone information is also something that the script should adapt to.

@CharlesChen888
Copy link
Contributor Author

If we finally decide there will be a backwards compatibility problem, we can have a new configuration item to control whether to display time zone, and by default it is off.

The point is, we want to keep everything as simple as it can.

@ecerulm
Copy link

ecerulm commented Jan 15, 2024

I don't think this is a problem. Redis has gone through multiple versions, and its log format has also evolved. Different versions have different log formats.

@soloestoy, I think redis has the same log format since Redis 3.x and now we are in Redis 7.2, so it has been the same log format for 8 years. right? correct me if I'm wrong.

@soloestoy
Copy link
Contributor

You can take a look at the example I provided. We made modifications to the log format in both versions 4.0 and 6.0.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NEW] Add timezone to redis logs

5 participants