# Logging in Julia ## Logging stdlib and LoggingExtras.jl .row[ .col[ .image-30[  ] ] ] .row[ .col[ **Frames White** (she/they)
EDA & Compiler Engineer
] .col[ **JuliaCon** 2023
Boston
] ] .row[ .col[ .image-40[  ] ] .col[ ] ] --- ## Can you chose a more difficult location for a conference? .image-80[  ] * It took me over 42 hours to get here. * 18,700 km in great circle distance The problem is the remoteness of Boston, and not of Perth. --- ## History and Credit * LoggingExtras initially created in 2018 shortly after Julia 1.0 and the new Logging stdlib * We recently tagged v1.0 of LoggingExtra, though the core of it hasn't changed much since 2018 * The Julia 1.0 logging stdlib was largely created by **Claire Foster** * Thanks also to other contributors, in particular **Fredrik Ekre** and **Jacob Quinn** --- ## What is logging in julia like? ```julia julia> @info "hello $name" y len_y=length(y) x.+y ┌ Info: hello oxinabox │ y = │ 3-element Vector{Int64}: │ 1 │ 2 │ 3 │ len_y = 3 │ x .+ y = │ 3-element Vector{Int64}: │ 2 │ 3 └ 4 ``` --- ## Logging vs print .row[ .col[ **Logging** High level abstraction Default fancy styling (by `ConsoleLogger`) Routable with log plumbing (either with LoggingExtras or otherwise) Always printed to stderr (by `ConsoleLogger`) ] .col[ **Print** Low level primitive for writing text to streams. Outputs to the given stream. Defaults to stdout. ] ] --- ## Who is logging for ? Logging needs to serve the needs of many different users * Operations Team monitoring production environment. * Probably using some tool liked Cloudwatch or SumoLogic work with the logs * The developer: * printline logging is the classic debugging technique * The end user on a local interactive session * communicating the state of the program --- ## What is log plumbing? .image-100[] .col[ Direct the right content to the right place. ] --- ## Compositional Logging Core idea is to break down logging in to single function building blocks with a single purpose. We compose these building blocks to do more complicated things. Primarily the end-user configures a logger out of composable parts to meet their needs of the moment. --- ## What are the parts of a `AbstractLogger`? (1) * `handle_message(logger::AbstractLogger, args...; kwargs...)` * actually log the thing, in what ever sense that is * used by **sinks** (and several other compositional components) * `shouldlog(logger::AbstractLogger, args...)` * decide if to log ot not * This saves time avoiding evaluated computations in message or arguments to the log macro. * used by `EarlyFilteredLogger` --- ## What are the parts of a `AbstractLogger`? (2) * `min_enabled_level(logger::AbstractLogger)` * Even earlier filtering than `shouldlog` based on the lowest level this logger will accept. * used by `MinLevelLogger` filter. * `catch_exceptions(logger::AbstractLogger)` * not related to logger, so much as global need of end user. * When using this logger should julia catch exceptions thrown during log message preparation. --- ## What information is available when? .image-100[  ] --- # Logging Filters * `MinLevelLogger` * controls `min_enabled_level`, thus allows disabling most of the logging machinery. * `EarlyFilteredLogger` * controls `should_log`, thus allows stopping a message before computing arguments * `ActiveFilteredLogger` * decides if `handle_message` should pass the message to it's child logger's `handle_message`. --- ## Muting a known warning Consider you may be calling a method from a package that logs a warning that is not applicable to you. You can mute eveything from that module via ```julia with_logger(EarlyFilteredLogger(x->x._module != Foo) current_logger()) do foobar() end ``` Or for more confidence you won't mute anything unexpected: ```julia filter = ActiveFilteredLogger(current_logger()) do contains(x.message, r"Spam .* eggs .* ham") end with_logger(filter) do foobar() end ``` --- ## LevelOverrideLogger `LevelOverrideLogger` is the functional opposite of the `MinLevelLogger`. It causes the wrapped logger to ignore it's `min_enabled_level`. It is use as part of the together with the `with_level` helper to enable different log levels in a particular scope. ```julia julia> LoggingExtras.withlevel(Logging.Debug) do @debug "debug logs are normally muted" @debug "but not in this dynamic scope" end ┌ Debug: debug logs are normally muted └ @ Main REPL:2 ┌ Debug: but not in this dynamic scope └ @ Main REPL:3 ``` --- ## Transformer The `TransformerLogger` takes a function to perform on all the log message and metadata, generating a new version of it, and passes that information on to its child logger. This is very useful for many things. * add global state to all messages like * timestamps * configuration details * truncate overlong messages * add context data from logger declaration site (esp when using `with_logger`) --- ## Include stack-traces in every log messaage ```julia stacktrace_logger(logger) = TransformerLogger(logger) do log st = stacktrace()[5:end] # skip stuff from the logging system pretty_st = ["[$ii] $frame" for (ii, frame) in enumerate(st)] kwargs = (;log.kwargs..., stacktrace=pretty_st) return merge(log,(;kwargs)) end; ``` .image-80[  ] --- ## TeeLogger It sends the same content to multiple loggers that it wraps. Used in conjunction with filters to * Apply Transforms to some inputs but not others * Send different messages to different files etc There is no JoinLogger to do the reverse, because that is done by reusing the logger in the composition. --- ## Include stacktraces only in deprecation warnings ```julia is_depwarn(log) = log.group==:depwarn global_logger( TeeLogger( EarlyFilteredLogger( is_depwarn, stacktrace_logger(current_logger()) ), EarlyFilteredLogger(!is_depwarn, current_logger()) ) ) ``` .image-100[  ] --- ## Sinks The final destination of a log message. A pure sink only does `handle_message`. Simply returning `true` for `should_log` etc, and `BelowMinLevel` for `min_enabled_level`. Since this filtering should be done by a composable filter. A lot of sink's are inpure – copying the logging stdlib by implementing `min_enabled_level` themself; but that's not actually a problem. They still work well as a sink. While the `TransformerLogger` transforms the content of the log, and enriches it with more information, the sink is ultimately responsible for how it is presented to the user (or stored etc). --- ## Sinks in the Logging StdLib * `ConsoleLogger`: the default logger in the REPL we all know and love * `NullLogger`: It eats everything and produces no output. Usedul to mute *all* logs via `withlogger(NullLogger()) do` * `SimpleLogger`: A simple logger, no where near as much pretty formatting etc as the `ConsoleLogger`. --- ## Sinks in LoggingExtras Sink's are not the purpose of LoggingExtras. We accumulated a few though as it is a place to stick extra stuff. * `FileLogger`: a wrapper around the `SimpleLogger` that takes a filename and forces immediate flushing after every message. * `FormatLogger`: A logger that takes a function (that uses `print`) controlling how to render things (MiniLoggers.jl also does this idea) * `DatetimeRotatingFileLogger` a log that is automatically rolled into a new file periodically (LogRollers.jl also does this idea) --- ## Other Sinks * TensorBoardLogger.jl * competitor to TensorboardX in python. * TensorBoard can be well thought of as logger that accepts numbers to plots, vectors to make histograms etc * TerminalLoggers.jl * Like the `ConsoleLogger` but even richer * Supports markdown, and progress bars * SyslogLogging.jl * Sink for the unix syslog --- # Many Examples can be found in the LoggingExtras readme, and/or the very good logging extras website. * truncating long messages * throttling messages based on time (useful for periodic updates) * adding timestamps to every log message --- # Future Work ## On LoggingExtras and the Logging stdlib I have a much longer list in the appendix here. But I will just share a few so we have time for questions and discussion --- ## Should the LoggingExtras examples have a package? .row[ .col[ **Pros** These things are useful Would make them testable Some are nonobvious (e.g. stacktraces) ] .col[ ] .col[ **Cons** These things are really configurable Nontrivial configuration is easier handled though writing code than passing arguments. (Hot-take) They are just not that long or complex ] ] --- ## We have no solution for deprecation warnings This is [JuliaLang/Julia#49583](https://github.com/JuliaLang/julia/issues/49583). In Julia 1.5 we made deprecations silent by default: * They were adding too much performance overhead (10μs) * They were often from dependencies of dependencies – not something the end user could fix We would like to bring them back, but only occurring if it is *your code* that is calling the deprecated method. --- ## We have no solution for deprecation warning (2) To work out if it is your code: * we need to know what module the calling code the code that called `depwarn`. i.e. `stacktrack()[2].module`. * But calling `stacktrace()` takes 40ms – way to expensive to use in a filter. An alternative: * we could throw deprecation warning if the `_module` at the `depwarn` log site is one of your direct dependencies * only cost about 10ns to check * but false positives for things that are both direct and indirect dependences. --- ## Group is a single value Group is the main thing we have for assigning a category to a log message. But it makes more sense to think of this as a set of tags, than as a single category. For example, the kinds of tags a system doing data processing might have includes `missing-data`, `model-fitting-issue`, `alert-pager` etc. and we could want to do `alert-pager` and `missing-data` Further-more, the LoggingExtras verbosity macros `@infov`, `@warnv` etc store the verbosity in the group. --- ## No distinction between data and metadata Often you want to talk to the logger at the call-site. The emergent way of doing this has been via setting keys. For example, the `ConsoleLogger` accepts a `maxlog` to control repeated printing of same message. ```julia for ii in 1:100 @info "this mesage will be shown only 3 times" maxlog=3 end ``` But that will also print `maxlog=3` in every message, that doesn't special case it --- ## We have no Tables.jl logging sink (AFAIK) Particulary useful is using numerical keyed values Something that could then naturally be passes to CSV.jl, or Arrow.jl etc to dump the logs to a file. Or that could be passed to PrettyTables.jl to have a nice display of the values. This would be natural as a companion to TensorBoardLogger --- # Summary * Logging in julia is pretty powerful and configuable * use LoggingExtras to configure log plumbing to meet your needs * Logging in Julia could be better, maybe work on that if its interesting to you .row[ .col[ .image-40[  ] ] ] --- # Appendix --- ## Console Logger is very complex The `ConsoleLogger` is the opposite of a compositional logger. It is a very nice sink, with lots of sensible and good rendering. It has the configurable `meta_formatter` for coloring and adding suffixed and prefixed to messages. It has code for shortenning long values, and for justification of text. It does filtering based on the `maxlog` handle and the `id`. A lot of this other loggers would like to reuse. Most of it wouldn't be enhanced by merely rewriting it as the composion of LoggingExtras types. --- ## `ConsoleLogger` color/naming is not centered on /Info/Warn/etc Log Levelss are actually both numerical and categorical. It would be nice to be able to represent a level slightly less than a warning as `Warn-1` and one slightly more as `Warn+1`. But `Warn` is the lowest level that is printed as a Warning. `Warn-1` prints as an `Info`.  --- ## `JULIA_DEBUG` environment variable is not quite right This is an enviroment variable that takes a list of package names to always show no matter what the `min_enabled_level(logger)` returns. It's not part of the `ConsoleLogger` – it actually bypasses the logging system almost entirely. It's not free and it can't be turned off. Takes about least 20ns to check an env that doesn't exist. This means you can't put logging in a extremely tight loop, since no matter how you configure your logging setup you can't disable this check. See: https://github.com/JuliaLogging/LoggingExtras.jl/issues/20 --- ## Enabling debug logging is too annoying Either swapping out the default logger, or using `JULIA_DEBUG` env-var and having to remember how it works. In short term we should make `enable_debug_logging()` and `enable_debug_logging(module)` functions available in REPL. --- ## `file`/`line` should be available to `shouldlog` There is no reason for it not to be It is known at this time. `group` is avaiable, and by default is set to the file but it might be being used for something else Similar, for julia 1.10 the function name is known at macro expansion time, and that also could be made available at this time (and later) --- ## Add timestamps to every log message ```julia using LoggingExtras, Dates timestamp_logger(logger) = TransformerLogger(logger) do log kwargs = merge(log.kwargs, pairs((; timestamp=now()))) return merge(log, (; kwargs)) end global_logger(timestamp_logger(current_logger())) ``` ``` julia> @info "hi" x = 2 ┌ Info: hi │ timestamp = 2023-07-20T02:52:02.525 └ x = 2 julia> @warn "everything is wrong" ┌ Warning: everything is wrong │ timestamp = 2023-07-20T02:52:28.297 └ @ Main REPL[6]:1 ```