当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Julia Logging.@logmsg用法及代码示例


用法:

@debug message  [key=value | value ...]
@info  message  [key=value | value ...]
@warn  message  [key=value | value ...]
@error message  [key=value | value ...]

@logmsg level message [key=value | value ...]

创建带有信息性 message 的日志记录。为方便起见,定义了四个日志记录宏 @debug@info@warn@error ,它们以标准严重级别记录 DebugInfoWarnError@logmsg 允许以编程方式将 level 设置为任何 LogLevel 或自定义日志级别类型。

message 应该是一个表达式,计算结果为一个字符串,该字符串是日志事件的人类可读说明。按照惯例,此字符串在显示时将被格式化为降价。

key=value 对的可选列表支持任意用户定义的元数据,这些元数据将作为日志记录的一部分传递到日志后端。如果仅提供 value 表达式,则将使用 Symbol 生成表示该表达式的键。例如,x 变为 x=x ,而 foo(10) 变为 Symbol("foo(10)")=foo(10) 。对于 splatting 键值对列表,请使用正常的 splatting 语法 @info "blah" kws...

有一些键允许覆盖自动生成的日志数据:

  • _module=mod 可用于指定与消息源位置不同的发起模块。
  • _group=symbol 可用于覆盖消息组(这通常源自源文件的基本名称)。
  • _id=symbol 可用于覆盖自动生成的唯一消息标识符。如果您需要非常紧密地关联在不同源代码行上生成的消息,这很有用。
  • _file=string_line=integer 可用于覆盖日志消息的明显源位置。

还有一些具有传统含义的键值对:

  • maxlog=integer 应该用作后端的提示,即消息应该显示不超过 maxlog 次。
  • exception=ex 应该用于传输带有日志消息的异常,通常与 @error 一起使用。可以使用元组 exception=(ex,bt) 附加关联的回溯 bt

例子

@debug "Verbose debugging information.  Invisible by default"
@info  "An informational message"
@warn  "Something was odd.  You should pay attention"
@error "A non fatal error occurred"

x = 10
@info "Some variables attached to the message" x a=42.0

@debug begin
    sA = sum(A)
    "sum(A) = $sA is an expensive operation, evaluated only when `shouldlog` returns true"
end

for i=1:10000
    @info "With the default backend, you will only see (i = $i) ten times"  maxlog=10
    @debug "Algorithm1" i progress=i/10000
end

相关用法


注:本文由纯净天空筛选整理自julialang.org 大神的英文原创作品 Logging.@logmsg — Macro。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。