Logging with custom messages

How about this?

using Logging, LoggingExtras, Dates

logger = FormatLogger(stderr) do io, args
    print(io, args.level, " ")
    Dates.format(io, now(), dateformat"yyyy-mm-dd HH:MM:SS")
    print(io, " [", args.file, ":", args.line, "] ")
    println(io, args.message)
end

global_logger(logger)

@debug "hello debug message"
@info "hello info message"
@warn "hello warning message"
@error "hello error message"

with output

$ julia run.jl 
Debug 2021-06-10 22:27:58 [/tmp/run.jl:12] hello debug message
Info 2021-06-10 22:27:58 [/tmp/run.jl:13] hello info message
Warn 2021-06-10 22:27:58 [/tmp/run.jl:14] hello warning message
Error 2021-06-10 22:27:58 [/tmp/run.jl:15] hello error message
4 Likes