-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 option to add backtrace to log messages #48282
Conversation
f1c4c55
to
79205fc
Compare
@oxinabox would this break logging extensions like https://github.com/JuliaLogging/LoggingExtras.jl ? Edit: Just checked and tests pass with and without |
2dea4e8
to
8c5c98c
Compare
8c5c98c
to
10d87ca
Compare
try | ||
error() | ||
catch | ||
return stacktrace(catch_backtrace()), kwargs | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try | |
error() | |
catch | |
return stacktrace(catch_backtrace()), kwargs | |
end | |
return stacktrace(), kwargs |
Shouldn't this be done with a custom logger? Or does that not work here? An env variable should be a last resort of setting an option imo. |
I agree with Kristoffer, I think this can and thus should be done with a custom logger. |
Very basic one, not super-pretty but: using LoggingExtras
foo() = bar()
bar() = qux()
qux() = @info "Hi"
stacklogger = TransformerLogger(global_logger()) do log
st = stacktrace()[5:end] # skip stuff from the logging system
pretty_st = ["[$ii] $frame" for (ii, frame) in enumerate(st)]
kwargs = (;log.kwargs..., stacktrace=pretty_st)
return merge(log,(;kwargs))
end;
with_logger(stacklogger) do
foo()
end More generally i think this goes well with other kind of LoggingExtras stuff, like you probably want to do with with some filters, so only certain modules and certain levels (e.g. warn) are displayed with stacktraces. |
That's awesome. Thanks. Yeah makes more sense in LoggingExtras |
Tracking down why a log message is happening can be hard from just the call site info.
The idea here is to expose a way to inject stack traces into the log message without having to edit the code site to manually add an error. For instance in CI where adding an env var is easier than editing code in a dependency that logs.
test.jl
includingThis PR:
Ignoring the stackframes from inside the logging code gen might be too crude.
Also, ignoring past an eval as a way to ignore the repl internals might be too, as
Base.scrub_repl_backtrace
didn't work for some reason.