taoensso / timbre

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

Idea: Add `set-ns-level!` function #328

Closed pjlegato closed 1 year ago

pjlegato commented 3 years ago

Adjust the log level for just one specific namespace seems to be non-trivial (unless I've missed a builtin function somehow): I currently have a single code block like:

(timbre/merge-config! {:min-level [
                                   [ #{"dev.jt.iboga.plumbing"} :info]
                                   ;;[ #{"dev.jt.iboga.plumbing"} :debug]

                                   [ #{"foo.bar"} :info]
                                   ;;[ #{"foo.bar"} :debug]

                                   [#{"*"} :info]
]})

where I can toggle either dev.jt.iboga.plumbing or foo.bar between :info and :debug by commenting and uncommenting, then re-evaluating the statement.

The disadvantages of this approach are that it requires centralized logging level specification -- that is, log levels are usually not specified in the file of the namespace they affect, and it's quite verbose.

Is there any interest in a function like:

(set-ns-level! :info)

which would live locally within each source file, and auto-adjust the log level for the namespace it's being loaded into?

As I understand it, such a function would have to:

ptaoussanis commented 3 years ago

@pjlegato Hi Paul!

You are correct that there's currently no built-in util for this. And agree that it might be nice to have one, good idea πŸ‘

I'd recommend the following API:

(set-ns-min-level [config ?min-level] [config ns ?min-level]) ; Returns an updated config map
(set-ns-min-level! [?min-level] [ns ?min-level] [ns ?min-level]) ; Uses `alter-var-root` with `set-ns-min-level` and `*config*`.

Where

Am happy to implement this myself next time I'm working on Timbre (I expect that getting the implementation right might be somewhat non-trivial). Also happy to look at a PR if you'd like to submit one.

ptaoussanis commented 1 year ago

The above-mentioned utils will be added in a forthcoming release πŸ‘