taoensso / timbre

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

Skipping a message for a specific appender #385

Closed vemv closed 4 months ago

vemv commented 4 months ago

Hi @ptaoussanis ,

is it possible to skip a logging message for a specific appender, according to custom logic?

I'm not sure Timbre middleware can do that.

What I intend to accomplish is preventing the Sentry logger from logging messages that don't contain an exception, given that the Sentry logger will log everything past the :min-level.

(It also seems feasible to just fork the Sentry logger. But this might be a nice use case for flexibility)

Thanks - V

NoahTheDuke commented 4 months ago

I believe if a middleware returns nil, then nothing is printed. See Architecture on the wiki, step 4: "Pass the data map through any middleware fns: (fn [data]) -> ?data. These may transform the data. If returned data is nil, end here and noop."

As an example, I have this in a test fixture, to not do any logging when testing our authorization endpoints:

(defn silence-auth-logs
  [f]
  (timbre/with-config
    (update timbre/*config* :middleware conj
            (fn [data]
              (when-not (str/includes? (or (first (:vargs data)) "") "Incorrect authorization")
                data)))
    (f)))
vemv commented 4 months ago

But what if I have N appenders and I just want to drop it for Sentry?

That's the essence of my question.

NoahTheDuke commented 4 months ago

Oh, my apologies. I missed that you meant a single appender of many. I'm not sure that answer.

ptaoussanis commented 4 months ago

Just have my hands a bit full atm, so apologies for the brief answer-

Timbre's appenders are ultimately just normal Clojure functions, so you can easily wrap them with whatever behaviour you like.

I.e. you can basically implement appender-specific "middleware" by hand - just wrap the handler function you want with something like this:

(fn my-wrapped-handler [data] (when (:?err data) (actual-handler data)))

Will check back tomorrow in case you need more details!

vemv commented 4 months ago

Thanks! I hadn't realised that.

I might also PR some Sentry-related improvements at some point.

Cheers - V

ptaoussanis commented 4 months ago

@vemv Great! Just ping if you'd like an actual working example 👍

@NoahTheDuke Thanks for your help on this, your advice re: (fn [data]) -> ?data was fundamentally correct 👍