Skip to content

Commit

Permalink
Allow expressions as logging keys (JuliaLang#37036)
Browse files Browse the repository at this point in the history
These will be converted to Symbols, similar to how positional args are handled
  • Loading branch information
simonbyrne authored Sep 8, 2020
1 parent 31d7210 commit c0b6afb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ function process_logmsg_exs(_orig_module, _file, _line, level, message, exs...)
_module = _orig_module
kwargs = Any[]
for ex in exs
if ex isa Expr && ex.head === :(=) && ex.args[1] isa Symbol
if ex isa Expr && ex.head === :(=)
k,v = ex.args
if !(k isa Symbol)
throw(ArgumentError("Expected symbol for key in key value pair `$ex`"))
k = Symbol(k)
end

# Recognize several special keyword arguments
Expand Down
3 changes: 2 additions & 1 deletion test/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ end
foo_val = 10
bar_val = 100
logs,_ = collect_test_logs() do
@info "test" bar_val progress=0.1 foo=foo_val 2*3 real_line=(@__LINE__)
@info "test" bar_val progress=0.1 foo=foo_val 2*3 bar(x)=1.2 real_line=(@__LINE__)
@info begin
value_in_msg_block = 1000.0
"test2"
Expand All @@ -69,6 +69,7 @@ end
@test kwargs[:progress] == 0.1
@test kwargs[:foo] === foo_val
@test kwargs[Symbol(:(2*3))] === 6
@test kwargs[Symbol(:(bar(x)))] === 1.2

# Keyword values accessible from message block
record2 = logs[2]
Expand Down

0 comments on commit c0b6afb

Please sign in to comment.