Skip to content

Conversation

@atusy
Copy link
Contributor

@atusy atusy commented Jan 25, 2025

This PR enables formatting timestamp of logs by specifying functions to the following options

  • logger.format_time
  • logger.format_time.{namespace} (has priority)

I actually wanted to achieve the feature without using options, but I find no suitable place to implement it...

Note that the behavior is not yet documented because I am not sure where to document it.
Do you have any ideas?

Some use cases are below

  • modifying timezone
  • get sub-second precision
withr::with_options(
  list(`logger.format_time` = function(x) format(x, "%Y-%m-%d %H:%M:%OS6 %Z", tz = "UTC")),
  {
    logger::log_formatter(logger::formatter_logging)
    logger::log_layout(logger::layout_logging)
    logger::log_info("foo")

    logger::log_formatter(logger::formatter_json)
    logger::log_layout(logger::layout_json_parser(fields = "time"))
    logger::log_warn(message = "foo")
  }
)
2025-01-25 14:45:39.519480 UTC INFO::foo
{"time":"2025-01-25 14:45:39.565330 UTC","message":"foo"}

@daroczig
Copy link
Owner

Hm, that's a good question 👍

But I before getting into the details, I might try to defend the original design and let the layout fn handle the time format: how the time is rendered seems to be part of the layout to me. In the layout fn, the time could be converted to M/D/Y or UNIX timestamp etc.

Please let me know what's the benefit of detaching the time formatter function from the layout.

@atusy
Copy link
Contributor Author

atusy commented Jan 27, 2025

@daroczig
Thanks. I agree with you. I first tried make changes on layout functions.
However, I realized this approach is applicable to layout_* functions that generates the layout functions (e.g., layout_json)

layout_json(fields = c("time"), format_time = function(x) format(x))

Other layout_* functions are not customizable (e.g., layout_logging).

The another choice is to add an argument to log_layout.
However, this looks ugly because format_time semantically part of the layout, but is apart from the layout argument...
Also, it requires internal setter/getter function that treats the format function.

log_layout(layout = layout_json(...), format_time = function(x) format(x))

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.

2 participants