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 use :min-level at the appender level based on namespace? #390

Closed ftravers closed 1 month ago

ftravers commented 4 months ago

The docs say:

"Note that both :min-level and :ns-filter can also be easily overridden on a per-appender basis."

and I'm using a :min-level like:

{:min-level [[#{"my.namespace"} :debug]  [#{"*"} :debug]]]
 :appenders
    {:my-appender
     {:enabled? true
      :min-level [[#{"my.namespace"} :error]]}}}

but when I use that appender it still logs 'info' log levels?

ptaoussanis commented 4 months ago

@ftravers Hi Fenton!

This should work, and seems to work on my side-

(binding [timbre/*config*
          {:min-level :info
           :appenders
           {:my-appender
            {:enabled?  true
             :min-level [[#{"*"} :warn]]
             :fn (fn [_] (println "Handler called"))}}}]

  (timbre/info "Test"))

Is it different on your side? If so, what version of Timbre are you using? Are you sure your namespace is correct?

jayemar commented 3 months ago

I seem to be having a similar issue, and I'm assuming I just don't understand how the namespace selectors work. I'm using timbre 6.5.0 in a lein project. Trying to get to a sane baseline, I put the following line in core.clj just after the ns expression which includes [taoensso.timbre :as log]:

(binding [log/*config* {:min-level [[#{"*"} :info]]}])

I assumed that that would prevent any log messages below INFO, but I'm still getting DEBUG log messages. If I instead use

(log/set-level! :info)

this works to suppress any messages below the level of INFO, but then I lack the granularity to set levels based on namespaces. Any ideas what I may be doing wrong with my usage of *config*?

ptaoussanis commented 3 months ago

@jayemar Hi Joe,

I put the following line in core.clj just after the ns expression which includes [taoensso.timbre :as log]: (binding [log/config {:min-level [[#{"*"} :info]]}])

Just to confirm, this is literally the exact binding expression that you used? If so, that won't do anything since Clojure's binding macro applies its binding only to the form that it encloses. I.e. it won't affect the rest of your program, only the contents of that one form (and in this case the contents are empty).

The docstring explains this in some more detail.

If you want to set Timbre's minimum level throughout your program, you can call set-min-level! or set-ns-min-level! as described in the Timbre wiki.

There's some more info on general config here.

Just let me know if any of that is unclear 👍

jayemar commented 3 months ago

Thank you for the help. You're right, I was definitely not understanding the functionality of neither the binding macro or the *config* map. I've managed to get things working as expected with the following call:

(log/merge-config! {:min-level [[#{"my-project.*"} :debug]
                                [#{"org.eclipse.jetty.*"} :info]
                                [#{"*"} :info]]})