# Types and composition in logging

**URL:** <https://discourse.julialang.org/t/types-and-composition-in-logging/16779>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [October 25, 2018, 7:52am UTC](https://discourse.julialang.org/t/types-and-composition-in-logging/16779 "2018-10-25T07:52:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 25, 2018, 7:52am UTC](https://discourse.julialang.org/t/types-and-composition-in-logging/16779/1 "2018-10-25T07:52:26Z")

</div>

I am very excited about the new [logging](https://docs.julialang.org/en/stable/stdlib/Logging/) interface, as I think it would allow numerical algorithms to emit debugging information in a way that does not interrupt the process, but should allow the user to examine parts of a calculation with the exact same input.

Eg a concrete use case, consider a log-likelihood that should always evaluate to `-Inf` or a finite real number. If that’s not the case (eg `NaN`), the logger could emit the parameters. I have some questions on practice though:

1. Is it OK to pass composite types that are defined in a specific module to the logger? This would allow eg a custom `show` method that would just print a small summary, instead of dumping 50 lines of numbers, yet also contain detailed information for inspection. My concern is that this would be opaque to other packages.

2. If the package defines a specific logger to filter its own debug messages, what’s the best way to compose such loggers, so that multiple packages can cooperate smoothly?

Pointers to examples, if any, would also be appreciated.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [October 25, 2018, 8:53am UTC](https://discourse.julialang.org/t/types-and-composition-in-logging/16779/2 "2018-10-25T08:53:31Z")

</div>

> [@Tamas\_Papp](#):
>
> If the package defines a specific logger to filter its own debug messages, what’s the best way to compose such loggers, so that multiple packages can cooperate smoothly?

That’s a bit tricky with the current design, unfortunately. You’ll get the best results by caching the previously used logger in your custom logger (see e.g. [here](https://github.com/JunoLab/Atom.jl/blob/master/src/progress.jl)), and then do something like

```julia
function loglikelihood(x)
  Logging.with_logger(MyCustomLogger(Base.CoreLogging.current_logger()) do
    compute_log_likelihood(x)
  end
end

```

Also make sure to properly define `shouldlog` and `min_enabled_level`.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 25, 2018, 1:59pm UTC](https://discourse.julialang.org/t/types-and-composition-in-logging/16779/3 "2018-10-25T13:59:25Z")

</div>

> [@Tamas\_Papp](#):
>
> Is it OK to pass composite types that are defined in a specific module to the logger?

This depends on how the logger implements printing of log messages, but it shouldn’t be a problem in general.

> [@Tamas\_Papp](#):
>
> If the package defines a specific logger to filter its own debug messages, what’s the best way to compose such loggers, so that multiple packages can cooperate smoothly?

Why not use a custom log level instead? They are going to work on pretty much any logger, regardless of origin. Some packages (shameless plug: e.g. my [IOLogging.jl](https://github.com/Seelengrab/IOLogging.jl)) allow for log message filtering based on the log level.
