Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty @logmsg #29672

Closed
cossio opened this issue Oct 16, 2018 · 6 comments
Closed

Allow empty @logmsg #29672

cossio opened this issue Oct 16, 2018 · 6 comments
Labels
logging The logging framework

Comments

@cossio
Copy link
Contributor

cossio commented Oct 16, 2018

I was trying to use @debug like this:

@debug if expensive_check
     @error "massive failure"
end

If Debug logging is enabled, I perform an expensive check. If the check is not true, the event should log as an error (I want to see the red color pop out in the middle of many logging messages). In this case, the @debug itself should not log at all.

However currently an empty @logmsg (with return value `nothing) produces an error:

julia> @warn begin end
┌ Error: Exception while generating log record in module Main at REPL[4]:1
│   exception =
│    ArgumentError: `nothing` should not be printed; use `show`, `repr`, or custom output instead.
│    Stacktrace:
│     [1] print(::Base.GenericIOBuffer{Array{UInt8,1}}, ::Nothing) at ./show.jl:566
│     [2] print_to_string(::Nothing) at ./strings/io.jl:122
│     [3] string(::Nothing) at ./strings/io.jl:155
│     [4] #handle_message#2(::Nothing, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Logging.ConsoleLogger, ::Base.CoreLogging.LogLevel, ::Nothing, ::Module, ::String, ::Symbol, ::String, ::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Logging/src/ConsoleLogger.jl:108
│     [5] handle_message(::Logging.ConsoleLogger, ::Base.CoreLogging.LogLevel, ::Nothing, ::Module, ::String, ::Symbol, ::String, ::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Logging/src/ConsoleLogger.jl:100
│     [6] top-level scope at REPL[4]:1
│     [7] top-level scope at logging.jl:321
│     [8] eval(::Module, ::Any) at ./boot.jl:319
│     [9] eval_user_input(::Any, ::REPL.REPLBackend) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/REPL/src/REPL.jl:85
│     [10] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/REPL/src/REPL.jl:117 [inlined]
│     [11] (::getfield(REPL, Symbol("##28#29")){REPL.REPLBackend})() at ./task.jl:259
└ @ Main REPL[4]:1

Would it be worthwhile to allow empty @logmsg to simply register no log message at all instead of throwing an error that doesn't seem to serve any purpose?

@cossio cossio changed the title Allow empty @debug, @warn, etc. Allow empty @logmsg Oct 16, 2018
@maleadt
Copy link
Member

maleadt commented Oct 16, 2018

ref #27650

@kshyatt kshyatt added the logging The logging framework label Oct 23, 2018
@c42f
Copy link
Member

c42f commented Jul 16, 2019

This is an interesting use case; it's certainly not what I had in mind for @debug but I can see the value in what you're trying to do.

One possibility would be to have something like @if_shouldlog (@iflog / @logif / @logwhen ) or some such thing which runs the standard log filtering checks and executes the block when they're true. The best thing would be if we can design things so that the behavior of @info and friends can be specified in terms of this lower level primitive operation.

@if_shouldlog Debug begin
    @error "massive failure"
end

@tkf
Copy link
Member

tkf commented Jul 17, 2019

Why not just a function like shouldlog(:info)? None of the advantages of macros in logging seem to apply here (e.g., you don't need to create a record id). Or maybe I'm missing something?

@c42f
Copy link
Member

c42f commented Jul 17, 2019

@tkf I think the logic should be as similar as possible to the macro versions, including passing any statically known metadata to shouldlog such as the current module. With a function the users would have to do that manually by inserting @__MODULE__ which might be kind of annoying. Other than that though, yes; we could have a function version.

@tkf
Copy link
Member

tkf commented Jul 17, 2019

Thanks for the clarification. I missed that @__MODULE__ was used for filtering.

@vtjnash
Copy link
Member

vtjnash commented Apr 9, 2021

This now works (nothing can again be printed)

@vtjnash vtjnash closed this as completed Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logging The logging framework
Projects
None yet
Development

No branches or pull requests

6 participants