Custom logger in Atom?


#1

I’d like to customize the response to @info, as in the long quote below. I find this useful for debugging package tests. This works in REPL (Julia 0.7) but not in Atom, where the current logger is wrapped in a JunoProgressLogger (Atom/src/comm.jl:96 or :109).

Since loggers are immutable, I believe the only way to do this is to define a new logger.

I have tried to detect Atom, and when in Atom setting the new logger like this:
Logging.global_logger(Atom.Progress.JunoProgressLogger(TESTLOGR))

…which works if the next logging event is initated from the Atom console, but reverts to JunoProgressLogger(ConsoleLogger) at the next evaluation.

Does Atom only check the current logger during precompilation?
How can I define my own logging customization in Atom?

import Logging
import Logging:default_logcolor,
                LogLevel,
                Info,
                Warn,
                Error
using Dates
const OLDLOGGER = Logging.global_logger()
const T0_TESTS = now()
"Return file, location and time since start of test in info messages"
function custom_metafmt(level, _module, group, id, file, line)
    color = default_logcolor(level)
    prefix = (level == Warn ? "WARNING" : string(level))*':'
    suffix = ""
    # Next line was 'Info <= level <' in default_metafmt.
    # We exclude level  = Info from this early return
    Info <= level -1 < Warn && return color, prefix, suffix
    _module !== nothing && (suffix *= "$(_module)")
    if file !== nothing
        _module !== nothing && (suffix *= " ")
        suffix *= Base.contractuser(file)
        if line !== nothing
            suffix *= ":$(isa(line, UnitRange) ? "$(first(line))-$(last(line))" : line)"
        end
        suffix *= " $(Int(round((now() - T0_TESTS).value / 1000))) s"
    end
    !isempty(suffix) && (suffix = "@ " * suffix)
    return color, prefix, suffix
end
const TESTLOGR = Logging.ConsoleLogger(meta_formatter = custom_metafmt)
Logging.global_logger(TESTLOGR)
@info """
The info logger now has a suffix (not in Atom).
      To reinstate the original logger:
  julia> Logging.global_logger(OLDLOGGER)
"""


Using @debug in Atom console
#2

Since loggers are immutable, I believe the only way to do this is to define a new logger.

No, that’s just me making a rather stupid design decision (the REPL is also wrapped in the JunoProgessLogger). Should be fixed soonish…


#3

Should be fixed on Atom.jl master now, would be cool if you could try it.

I knew this would come back to bite me… :stuck_out_tongue:


#4

That’s some quick response! Unfortunately, my current 5 Mb/day data quota didn’t fit the whole repo, but I’ll get back to it when I can.
I can otherwise recommend a low quota.


#5

It works, and that with or without the Juno wrapper. I’m no good with Julia markdown, so display the results by pasting the below. Thank you!

import Logging
import Logging:default_logcolor,
                LogLevel,
                Info,
                Warn,
                Error
using Dates
const OLDLOGGER = Logging.global_logger()
const T0_TESTS = now()
"Return file, location and time since start of test in info messages"
function custom_metafmt(level, _module, group, id, file, line)
    color = default_logcolor(level)
    prefix = (level == Warn ? "WARNING" : string(level))*':'
    suffix = ""
    # Next line was 'Info <= level <' in default_metafmt.
    # We exclude level  = Info from this early return
    Info <= level -1 < Warn && return color, prefix, suffix
    _module !== nothing && (suffix *= "$(_module)")
    if file !== nothing
        _module !== nothing && (suffix *= " ")
        suffix *= Base.contractuser(file)
        if line !== nothing
            suffix *= ":$(isa(line, UnitRange) ? "$(first(line))-$(last(line))" : line)"
        end
        suffix *= " $(Int(round((now() - T0_TESTS).value / 1000))) s"
    end
    !isempty(suffix) && (suffix = "@ " * suffix)
    return color, prefix, suffix
end
const TESTLOGR = Logging.ConsoleLogger(meta_formatter = custom_metafmt)
if @isdefined Atom
    Logging.global_logger(Atom.Progress.JunoProgressLogger(TESTLOGR))
    @info """
        @info messages now have a suffix.
              Presumably, the Atom log pane works as normal.
              To reinstate the original logger:
                  julia> Logging.global_logger(OLDLOGGER)
    """
else
    Logging.global_logger(TESTLOGR)
    @info """
        @info messages now have a suffix.
              Presumably, the Atom log pane works as normal.
              To reinstate the original logger:
                  julia> Logging.global_logger(OLDLOGGER)
    """
end
nothing