當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。