Crystal: Logger formatter does not have access to the location of the log call

Created on 16 Feb 2020  路  4Comments  路  Source: crystal-lang/crystal

I find it very useful to have the file name and line number of where a log happened in the header of the log message, for example:

[D 2020-02-15 06:04:32 UTC job.cr:114] Something exciting happened

To do this I currently have to implement a wrapper around the stdlib Logger class so that .log() and friends capture the __FILE__ and __LINE__ parameters. It would be incredibly useful to have these available to the formatter function, so that this can be done without messy hacks.

It could be worth taking this opportunity to refactor the formatter proc to take a single struct argument rather than multiple arguments, so this kind of change can be made without breaking everyone's loggers.

I can implement this, if this approach is OKed by the core team.

feature stdlib

Most helpful comment

The logger will have a revamp soon. No need for a PR until we share a bit of the future design. Stay tuned :-)

All 4 comments

The logger will have a revamp soon. No need for a PR until we share a bit of the future design. Stay tuned :-)

See #5874 for reference

https://github.com/crystal-lang/crystal/pull/8847#discussion_r384551035 is probably the way I would handle this. File/line numbers are specific to a given entry so they should just exist on the Entry itself.

Adding it into context could work, but it would have to be added to that one single entry via Log.with_context, otherwise it would be added to the main fiber context which doesn't make sense.

I'm still happy to implement this in the new logger, I've currently got a personal shard that is doing a poor job at overriding the Log.* methods to add the call location.

If there are concerns with including the locations of log calls in the compiled binary, there could be a compiler option to omit them.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lgphp picture lgphp  路  3Comments

Sija picture Sija  路  3Comments

ArthurZ picture ArthurZ  路  3Comments

pbrusco picture pbrusco  路  3Comments

TechMagister picture TechMagister  路  3Comments