Logging

Julia has a great logging system. If you come from other programming languages like Python you'll likely already love it, but it can be better!

AS you know, if you use logging macros like @info with a bunch of arguments after, Julia produces something like this:

@info "My log message" 1+1 n="a string!" :x

It shows your log messages and then the other arguments as key = value display even evaluating expressions like 1 + 1 and showing you the result. Very useful, but visually a bit dull. Also, we might want additional info to be shown: at what time was the log message produced, by which line of code, inside which function, what are the types of the arguments...

Well, Term provides logging functionality that gives all that information, plus a ton of styling on top. To do that we need to install Term's own logger (TermLogger, very creative name) as the global logger to handle all error messages:

import Term: install_term_logger
install_term_logger()

Done. Not a lot of work. Now you can just use your logging macros as you would normally, you don't need to change anything. But magic will happen:

@info "My log message" 1+1 n="a string!" :x
@Info (Main):  My log message
  
     (Int64) 1 + 1 = 2
    (String) n     = a string!
    (Symbol) :x    = x
  ╰────────────────────────────────────────────────
                      Mon, 29 Aug 2022 01:36:47

As you can see, it shows all the information that is normally there, an more! If your log macro is called from within a function, it will also tell you that:

function my_useful_function()
    @info "My log message" 1+1 n="a string!" :x
end

my_useful_function()
@Info (Main):  My log message
  
     (Int64) 1 + 1 = 2
    (String) n     = a string!
    (Symbol) :x    = x
  ╰────────────────────────────────────────────────
                      Mon, 29 Aug 2022 01:36:47

And of course it works nicely with all logging macros:

@info "just some info"
@warn "careful!"
@error "uh oh, not good!"
@Info (Main):  just some info
  ╰────────────────────────────────────────────────
                      Mon, 29 Aug 2022 01:36:47
@Warn (Main):  careful!
  ╰────────────────────────────────────────────────
                      Mon, 29 Aug 2022 01:36:47
@Error (Main):  uh oh, not good!
  ╰────────────────────────────────────────────────
                      Mon, 29 Aug 2022 01:36:47
not for developers

Setting up Term's loggers will change this behavior for any downstream user of your code. While this could be okay, it might be surprising and undesirable for some, so do at your own risk.

Tip

You can rever to the original logger using uninstall_term_logger.

Term and other log outputs

See here for some info on how to use Term's logger in conjuction with other logging outputs.