taoensso / timbre

Pure Clojure/Script logging library
https://www.taoensso.com/timbre
Eclipse Public License 1.0
1.44k stars 171 forks source link

How to add appenders in the edn configuration? #383

Closed publicimageltd closed 8 months ago

publicimageltd commented 8 months ago

I want to use the spit-appender. On the repl, I can log to a file like this:

  (timbre/with-merged-config {:appenders
                              {:spit (taoensso.timbre.appenders.core/spit-appender
                                      {:fname "mylog.log"})}}
    (timbre/info "hi there, logfile"))

Works fine. But I can't manage to translate that to an entry in the corresponding .edn file, which can be also used to set the configuration map. I tried all kind of things, but fail. The file is read, but no matter how I format the reference to the appender, it is just silently ignored.

So what's the syntax for adding a spit-appender in the configuration file, and not runtime?

ptaoussanis commented 8 months ago

Hi there-

It'd be helpful to first understand a bit better what you're ultimately trying to achieve. What's your motivation for wanting to configure an appender via an .edn file?

publicimageltd commented 8 months ago

Separating basic configuration logic from the code, that's all. It's for a server. I can, of course, set up the configuration when starting the system. But I thought it could be possible to configure it separately (without rolling my own configuration format).

ptaoussanis commented 8 months ago

So the challenge here is that edn is intended to convey values, so isn't generally well suited to things like functions (appenders) or function calls (appender constructors).

I'm not sure what platform you're on or what you've already tried exactly, but your simplest options would include:

  1. Set up appenders when starting the system, without any environmental config.
  2. Set up appenders when starting the system, with custom environmental config (i.e. your own JVM properties or ENV vars, etc. that you read for determining which appenders to configure).
  3. Set up appenders when starting the system, then just enable/disable them via Timbre's standard environmental config (taoensso.timbre.config.edn, etc.).
  4. Define the relevant config maps as named vars in your code, then use Timbre's standard environmental config to point to the relevant qualified symbol. I.e. instead of embedding the entire config map in the .edn, your edn just consists of a single qualified symbol that resolves to the appropriate config var. This is described in the *config* docstring.

My 2c- unless you have a good specific reason to create/modify appenders environmentally, I'd normally vote for 1 or 3 above since they're the simplest and should cover most cases.

Cheers :-)

publicimageltd commented 8 months ago

Thanks, that's a useful oversight and confirms my impression that I just can't do it directly in the .edn. I thought there might be some internal mechanism which leverages tagged elements, or automatically parses names so that they will be rebound as function names. Anyways, I see that it would be too complicated for some simple stuff given the alternatives you outlined. Keep on doing the great work!

ptaoussanis commented 8 months ago

You're very welcome, thanks for the kind words. Best of luck!