slog
slog copied to clipboard
slog_term could benefit from an option to print out filename and line number ...
Necessary in case of massive systems where the same error can repeat in many places possibly ... Ideally per level so e.g. ERROR/WARN/CRIT are showing it ...
Yes, it can be put manually on every error! but we already have line/file in record
It's possible already via normal means.
Every key-value pair when being emitted takes a Record
: https://docs.rs/slog/2.0.0-3.1/slog/trait.KV.html
Record
gives it line
, file
and everything if needed. https://docs.rs/slog/2.0.0-3.1/slog/struct.Record.html#method.file
Typically this is used with closures. Eg. here slog-bunyan
: https://github.com/slog-rs/bunyan/blob/00b495dd171ee1f673aea8872f6dea0a08e0af5e/lib.rs#L67
In your system you could add a key-value
object that emits "location" => file:line
or whatever you prefer, and possibly even skip emitting anything on some record.level()
etc.
I'm talking slog v2
here, but IIRC slog v1 can do it quite similarly.
Now, if you'd want some "special treatment" for source location during formatting, that's another story. Probably could be added to a builder, however I'm not sure if it's really worth it. Please let me know if the general approach works for you.
I don't think I expressed myself clearly. I'm kind of looking for a drain (option?) with sterm that automatically prints line/file just like it puts timestamps on everything ...
So for full format this would work already. When creating root logger just do something like:
let log = log.new(o!("location" => PushFnValue(|r : &'c Record<'d>, ser : PushFnSerializer<'c>| {
ser.serialize(format_args!("{}"{}", r.file(), r.line());
})));
and just is the new log
. I haven't actually tried that code, just typing from memory.
It would however defeat the compact format as the first value would keep changing, forcing printing out all information over and over.
I think it makes sense to add list of KVs
to terminal output, in a way similiar to what slog-bunyan
is implementing, that it would be output along the timestamp and message for cases like that.
I think I know what you mean. You'd like this to be printed as the "header" part, not a KV-part. It could be added, as an optional closure or something like that in the builder.
Yupp, I'd like it like a timestamp on the header. Huge systems have thousands of logs and sometimes it's not easy to find out WHERE the log comes from, especially if the same text/log can occur in many places ...
Yes, I would like this as well.
I just tried @dpc approach (btw, this version compiles:
let log = log.new(o!("location" => PushFnValue(|r: &Record, ser: PushFnValueSerializer| {
ser.serialize(format_args!("{}:{}", r.file(), r.line()))
})))
with
slog-term = "2.3.0"
slog-async = "2.1.0"
)
and it works, but it also ruins CompactFormat
output - the whole hierarchy is printed every time, because the location keeps changing...
Being able to easily customize the header (probably custom slog_term::print_msg_header
?) would be nice
Agree. I think this is a useful feature. Please submit a PR!