如何将“catch_backtrace”记录到文件
How to log `catch_backtrace` to a file
我想捕获任何错误并将带有完整回溯的错误消息记录到日志文件中供我查看。我的实现是基于这个 ,目前我在我的 STDOUT 中得到了想要的结果,但是日志文件回溯包含很多没有帮助的 Ptr{Nothing} @0x0000000121cc6b5e
。下面是一个最小的例子:
using LoggingExtras, Logging
global_logger(FileLogger("log_file.log"))
# few functions so we get traceback
function bar(b)
error("Failed with b:$b")
end
function foo(a)
bar(a)
end
function run(c)
foo(c)
end
try
run(2)
catch e
@error "failure" exception = (e, catch_backtrace())
end
在 STDOUT 中给出有意义的消息
┌ Error: failure
│ exception =
│ Failed with b:2
│ Stacktrace:
│ [1] error(s::String)
│ @ Base ./error.jl:33
│ [2] bar(b::Int64)
│ @ Main /private/tmp/log_problem/ex.jl:6
│ [3] foo(a::Int64)
│ @ Main /private/tmp/log_problem/ex.jl:10
│ [4] run(c::Int64)
│ @ Main /private/tmp/log_problem/ex.jl:14
│ [5] top-level scope
│ @ /private/tmp/log_problem/ex.jl:17
│ [6] include(mod::Module, _path::String)
│ @ Base ./Base.jl:418
│ [7] exec_options(opts::Base.JLOptions)
│ @ Base ./client.jl:292
│ [8] _start()
│ @ Base ./client.jl:495
└ @ Main /private/tmp/log_problem/ex.jl:19
但在 log_file.log
我得到
┌ Error: failure
│ exception = (ErrorException("Failed with b:2"), Union{Ptr{Nothing}, Base.InterpreterIP}[Ptr{Nothing} @0x0000000121cc6b5e, Ptr{Nothing} @0x000000010a7ed743, Ptr{Nothing} @0x000000010a7ed7af, Ptr{Nothing} @0x000000010a7ed80f, Ptr{Nothing} @0x000000010a7ed835, Ptr{Nothing} @0x000000010896b4a0, Ptr{Nothing} @0x00000001089848df, Ptr{Nothing} @0x0000000108982e26, Ptr{Nothing} @0x000000010898306a, Ptr{Nothing} @0x000000010898341c, Base.InterpreterIP in top-level CodeInfo for Main at statement 1, Ptr{Nothing} @0x000000010899e49f, Ptr{Nothing} @0x000000010899e242, Ptr{Nothing} @0x000000010899f515, Ptr{Nothing} @0x00000001220732fa, Ptr{Nothing} @0x000000010896b4a0, Ptr{Nothing} @0x00000001221b85f5, Ptr{Nothing} @0x0000000121d75a85, Ptr{Nothing} @0x000000010896b4a0, Ptr{Nothing} @0x0000000121e39314, Ptr{Nothing} @0x0000000121683dff, Ptr{Nothing} @0x0000000121683f68, Ptr{Nothing} @0x000000010896b4a0, Ptr{Nothing} @0x00000001089c1d32, Ptr{Nothing} @0x00000001089c1c23])
└ @ Main /private/tmp/log_problem/ex.jl:19
如何让日志文件包含与标准输出相同的信息?
FileLogger
uses a SimpleLogger
internally, which, as the name suggests, is a simple logger without any formatting. You could instead use a FormatLogger
以您喜欢的方式格式化异常。
如果您想要与 Julia REPL 中相同的格式,您可以使用相同的记录器,即 ConsoleLogger
,但是您需要自己处理文件的打开并传递 open writeable IO
给构造函数。
我想捕获任何错误并将带有完整回溯的错误消息记录到日志文件中供我查看。我的实现是基于这个 Ptr{Nothing} @0x0000000121cc6b5e
。下面是一个最小的例子:
using LoggingExtras, Logging
global_logger(FileLogger("log_file.log"))
# few functions so we get traceback
function bar(b)
error("Failed with b:$b")
end
function foo(a)
bar(a)
end
function run(c)
foo(c)
end
try
run(2)
catch e
@error "failure" exception = (e, catch_backtrace())
end
在 STDOUT 中给出有意义的消息
┌ Error: failure
│ exception =
│ Failed with b:2
│ Stacktrace:
│ [1] error(s::String)
│ @ Base ./error.jl:33
│ [2] bar(b::Int64)
│ @ Main /private/tmp/log_problem/ex.jl:6
│ [3] foo(a::Int64)
│ @ Main /private/tmp/log_problem/ex.jl:10
│ [4] run(c::Int64)
│ @ Main /private/tmp/log_problem/ex.jl:14
│ [5] top-level scope
│ @ /private/tmp/log_problem/ex.jl:17
│ [6] include(mod::Module, _path::String)
│ @ Base ./Base.jl:418
│ [7] exec_options(opts::Base.JLOptions)
│ @ Base ./client.jl:292
│ [8] _start()
│ @ Base ./client.jl:495
└ @ Main /private/tmp/log_problem/ex.jl:19
但在 log_file.log
我得到
┌ Error: failure
│ exception = (ErrorException("Failed with b:2"), Union{Ptr{Nothing}, Base.InterpreterIP}[Ptr{Nothing} @0x0000000121cc6b5e, Ptr{Nothing} @0x000000010a7ed743, Ptr{Nothing} @0x000000010a7ed7af, Ptr{Nothing} @0x000000010a7ed80f, Ptr{Nothing} @0x000000010a7ed835, Ptr{Nothing} @0x000000010896b4a0, Ptr{Nothing} @0x00000001089848df, Ptr{Nothing} @0x0000000108982e26, Ptr{Nothing} @0x000000010898306a, Ptr{Nothing} @0x000000010898341c, Base.InterpreterIP in top-level CodeInfo for Main at statement 1, Ptr{Nothing} @0x000000010899e49f, Ptr{Nothing} @0x000000010899e242, Ptr{Nothing} @0x000000010899f515, Ptr{Nothing} @0x00000001220732fa, Ptr{Nothing} @0x000000010896b4a0, Ptr{Nothing} @0x00000001221b85f5, Ptr{Nothing} @0x0000000121d75a85, Ptr{Nothing} @0x000000010896b4a0, Ptr{Nothing} @0x0000000121e39314, Ptr{Nothing} @0x0000000121683dff, Ptr{Nothing} @0x0000000121683f68, Ptr{Nothing} @0x000000010896b4a0, Ptr{Nothing} @0x00000001089c1d32, Ptr{Nothing} @0x00000001089c1c23])
└ @ Main /private/tmp/log_problem/ex.jl:19
如何让日志文件包含与标准输出相同的信息?
FileLogger
uses a SimpleLogger
internally, which, as the name suggests, is a simple logger without any formatting. You could instead use a FormatLogger
以您喜欢的方式格式化异常。
如果您想要与 Julia REPL 中相同的格式,您可以使用相同的记录器,即 ConsoleLogger
,但是您需要自己处理文件的打开并传递 open writeable IO
给构造函数。