Giter Club home page Giter Club logo

Comments (6)

ptaoussanis avatar ptaoussanis commented on May 11, 2024

Hey Adam,

This is the kind of thing you could do from your appender fn:

{:doc "Like the :standard-out appender, but colors output by logging level"
 :min-level nil :enabled? true :async? false :rate-limit nil
 :fn (fn [{:keys [error? output level]}] ; Can use any appender args
        (binding [*out* (if error? *err* *out*)]
          ;; TODO: Do a case against `level` to adjust output color
          (str-println output)))}

Does that help?

from timbre.

adamneilson avatar adamneilson commented on May 11, 2024

Fantastic!!!! Thanks Peter.

I used the standard-out appender which is perfect for my needs:

(timbre/set-config!
  [:appenders :standard-out]
  {:doc "Writes to pretty colorized output for log levels > debug"
   :min-level :debug
   :enabled? true
   :async? false 
   :max-message-per-msecs nil
   :fn (fn [{:keys [error? output level]}] ; Can use any appender args
         (binding [*out* (if error? *err* *out*)]
           (cond 
             (= level :info) (timbre/str-println (timbre/color-str :green output))
             (= level :warn) (timbre/str-println (timbre/color-str :yellow output))
             (= level :error) (timbre/str-println (timbre/color-str :red output))
             (= level :fatal) (timbre/str-println (timbre/color-str :purple output))
             (= level :report) (timbre/str-println (timbre/color-str :blue output))
             :else (timbre/str-println output))))})

from timbre.

ptaoussanis avatar ptaoussanis commented on May 11, 2024

Great, no problem. Note that you may be able to get that a bit shorter by doing something like:

(if-let [color ({:info :green :warn :yellow :error :red :fatal :purple :report :blue} level)]
  (timbre/str-println (timbre/color-str color output))
  (timbre/str-println output))

Does that make sense? Enjoy, cheers! :-)

from timbre.

adamneilson avatar adamneilson commented on May 11, 2024

Awesome!! Thanks!

from timbre.

ccfontes avatar ccfontes commented on May 11, 2024

With current config syntax, could be written like this:

(defn config-timbre! []
  (let [colors {:info :green, :warn :yellow, :error :red, :fatal :purple, :report :blue}]
    (timbre/set-config!
      {:level :debug
       :output-fn default-output-fn
       :appenders
       {:color-appender
         {:enabled?   true
          :async?     false
          :min-level  nil
          :rate-limit nil
          :output-fn  :inherit
          :fn (fn [{:keys [error? level output-fn] :as data}]
                (binding [*out* (if error? *err* *out*)]
                  (if-let [color (colors level)]
                    (println (color-str color (output-fn data)))
                    (println (output-fn data)))))}}})))

from timbre.

theronic avatar theronic commented on May 11, 2024

@ccfontes what should default-output-fn be?

Edit: Looks like it's just timbre/default-output-fn

from timbre.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.