Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timbre `set-config!` has changed arity thus don't know how to use it to output std err/out to a file

Tags:

clojure

timbre

I am trying to use https://github.com/ptaoussanis/timbre to log to a file rather than the console. Here is some documentation I've found on how to do that:

; The default setup is simple console logging.  We with to turn off console logging and
; turn on file logging to our chosen filename.
(timbre/set-config! [:appenders :standard-out   :enabled?] false)
(timbre/set-config! [:appenders :spit           :enabled?] true)
(timbre/set-config! [:shared-appender-config :spit-filename] log-file-name)
(timbre/set-config! [:shared-appender-config :spit-filename] log-file-name)

This works for a previous version of sente, but not for version [com.taoensso/timbre "4.3.1"]. (For unrelated reasons I need to use the latest). The issue with the above code is that set-config! now takes one argument - a hash-map. And I can't find any documentation that would help me with translating the above 'two params' code to the new 'one param' code.

I know there's a very similar question out there. This question has actual code in it so is more specific. I raised an issue as well. The code above basically comes straight from here.

like image 462
Chris Murphy Avatar asked Oct 18 '22 11:10

Chris Murphy


2 Answers

To log to a file rather than the console in timbre v4.0.0 (2015 June 10) you can do the following:

(ns app.core
  (:require [taoensso.timbre :as timbre]
            [taoensso.timbre.appenders.core :as appenders]))

;; Disable logging to the console in timbre v4.0.0:
(timbre/merge-config! {:appenders {:println {:enabled? false}}})

;; Create a "spit to file" appender in timbre v4.0.0: 
(timbre/merge-config! {:appenders {:spit (appenders/spit-appender {:fname "log.txt"})}})

Note that in timbre v4.3.1 the following does not remove nor disable the println (console) appender:

(timbre/merge-config! {:appenders {:println nil}})

The related issue can be found here https://github.com/ptaoussanis/timbre/issues/163

Addition: As Timbre allows to modify it's config map timbre/*config* simply by using the standard clojure API, you may also use

(timbre/swap-config! assoc-in [:appenders :spit :enabled?] false)
(timbre/swap-config! assoc-in [:appenders :spit] (appenders/spit-appender {:fname "log.txt"}))

Based on this you can additionally define a simple helper:

(def set-log-config-param! (partial timbre/swap-config! assoc-in))

and then be consistent with the timbre v3.4.0 set-config! syntax you have quoted in your question:

(set-log-config-param! [:appenders :println :enabled?] false)
(set-log-config-param! [:appenders :spit]   (appenders/spit-appender {:fname "log.txt"}))

Imho this eases the config transition between timbre v3 and v4 and makes the path to the params more readable then the equivalent

(timbre/merge-config! {:appenders {:println {:enabled? false}}})
(timbre/merge-config! {:appenders {:spit    (appenders/spit-appender {:fname "log.txt"})}})
like image 80
andreas.thoelke Avatar answered Oct 21 '22 06:10

andreas.thoelke


I got a quick response from the maintainer:

"Usage of the spit (file) appender is documented in the README at https://github.com/ptaoussanis/timbre#file-appender"

And here's the code to answer the question:

;; (:require [taoensso.timbre.appenders.core :as appenders]) ; Add to ns
(timbre/merge-config!  
    {:appenders {:println nil ; Remove println appender
                 :spit (appenders/spit-appender {:fname log-file-name})}})

Unfortunately even with the :println nil mapentry the same output will go to two places. So this answer is incorrect.

like image 28
Chris Murphy Avatar answered Oct 21 '22 05:10

Chris Murphy