logankilpatrick
logankilpatrick

Reputation: 14561

How to capture the full stack trace when logging in Julia

I am trying to write a custom logger. Currently, when I log, I just get the name of the error and what line it occurred on. If I want to log the full stack trace, I thought I would use the LogLevel from the docs but it's not clear how I would use that to capture a full stack trace. I have the following right now:

using Logging
io = open("log.txt", "w+")
LogLevel(1000001) # see https://github.com/JuliaLang/julia/blob/2d5741174ce3e6a394010d2e470e4269ca54607f/base/logging.jl#L90-L98
logger = SimpleLogger(io)
global_logger(logger)

try
    a+1
catch e
    @warn "failure" exception=e
    @info("a global log message")
end

close(io)

Upvotes: 3

Views: 694

Answers (1)

pfitzseb
pfitzseb

Reputation: 2554

As the docs say:

An associated backtrace bt may be attached using the tuple exception=(ex,bt)

So try

@warn "failure" exception=(e, catch_backtrace())

Upvotes: 6

Related Questions