Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying kwargs #18

Open
yha opened this issue Sep 22, 2019 · 1 comment
Open

Modifying kwargs #18

yha opened this issue Sep 22, 2019 · 1 comment

Comments

@yha
Copy link

yha commented Sep 22, 2019

This may be a documentation issue, but it seems like there is no easy way to add custom keys to a log event. I initially expected to be able to simply do something like

TransformerLogger(current_logger()) do log
    merge(log, (; newkey="value"))
end

but that doesn't work, because apparently TransformerLogger should return a named tuple with specific fields, and the custom key-value pairs are in a kwargs field. Writing to that field also doesn't work because that would change its type.
After digging around the package code, the easiest solution I came up with was calling LoggingExtras.handle_message_args (which is not mentioned in the docs), and using to the replace the event with a new one:

function merge_log_event(event; kwargs...)
    args = Tuple(event)[1:end-1]   # to drop the kwargs field
    LoggingExtras.handle_message_args(args...; kwargs..., event.kwargs...)
end

logger = TransformerLogger(current_logger()) do event
    merge_log_event(event; newkey="value")
end

Maybe something like this should be provided in the package?

@oxinabox
Copy link
Member

oxinabox commented Sep 23, 2019

the custom key-value pairs are in a kwargs field.

Yes, so what you need to do is access that field to get the old kwargs,
and then use that to build new kwargs,
which you then need to use to build a new log message.

We can't put them at the top-level, with the standard parts of the log message,
as that would disallow working with custom kwargs with names like msg,
since that would clash with actual message.

Following is an example of how to do this.
A PR to add something similar to the readme would be appreciated.

using Logging, LoggingExtras, Dates 

const date_format = "yyyy-mm-dd HH:MM:SS"

timestamp_kwlogger(logger) = TransformerLogger(logger) do orig_log
  new_kwargs = (timestamp=now(), orig_log.kwargs...)
  new_log = merge(orig_log, (kwargs=new_kwargs,))
  return new_log
end

ConsoleLogger(stdout, Logging.Debug) |> timestamp_kwlogger |> global_logger

Demo

julia> @info "Hi"
┌ Info: Hi
└   timestamp = 2019-09-23T11:02:19.403

After digging around the package code, the easiest solution I came up with was calling LoggingExtras.handle_message_args (which is not mentioned in the docs),

handle_message_args is mentioned in the docstring for TransformLogger.
But only because it contains the definitions of what each arg is.
Those defintions maybe should be moved elsewhere.
handle_message_args is not part of the public API.
It should never need to be invoked by the user of this package.
It is a internal helper function for building the single log_args nametuple out of the mix of positional and kwargs that default logging interface uses.

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

No branches or pull requests

2 participants