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

Add default extra fields to messages #40

Closed
Fuco1 opened this issue Dec 5, 2020 · 4 comments
Closed

Add default extra fields to messages #40

Fuco1 opened this issue Dec 5, 2020 · 4 comments

Comments

@Fuco1
Copy link

Fuco1 commented Dec 5, 2020

I have a json appender where I would like to automatically add some meta-data without having to specify them in the log function every time.

For example, I have a loop which operates on some entities, I'd like to automatically add the entity ID to each log message emitted in the loop.

The motivation is removal of extra clutter in the logging code, where often I'd have 4-5 extra arguments which don't change inside the entire function/loop body.

In code (non-working):

appender$set_default_meta(a = "b", id = 33)

...

lg$info("Hello")
lg$warn("Warning")

Both logs would include the a and id extra keys without me having to specify them repeatedly.

@s-fleck
Copy link
Owner

s-fleck commented Dec 5, 2020

This is already possible with (abusing) Filters! And since this is actually something I also needed, lgr already includes a constructor for Filters for your exact usecase!

lg$add_filter(FilterInject$new(a = "b", id = 33), "inject")

see ?EventFilter for more details

you can afterwards remove the filter with lg$remove_filter("inject"). Note that Filters can be attached to Loggers as well as Appenders.

@Fuco1
Copy link
Author

Fuco1 commented Dec 6, 2020

Genius! Is this described somewhere in the docs? Maybe a short vignette or a blog post would be nice, I think this is fairly common use-case in data processing.

@s-fleck
Copy link
Owner

s-fleck commented Dec 6, 2020

Ok great that it works :)

No it's not super well documented yet, only in the help of ?EventFilter and with_log_value() (which is just a wrapper for temporary setting a Filter that injects extra values)

I'm leaving this issue open because I agree it should be better documented (how it works, and also WHY it works). Sadly, refining the documentation is a lot of work :(. In the meanwhile, note that lgr is basically a very close clone of python logging and most of the documentation of python logging applies - with small modifications - also to lgr (in this case Filter Objects)

@s-fleck
Copy link
Owner

s-fleck commented Dec 11, 2020

I just saw, this was actually documented in the vignette already, I just forgot about it ;) I gave the section a slightly better heading now.

@s-fleck s-fleck closed this as completed Dec 11, 2020
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