Skip to content

Commit

Permalink
~[new] [#332] Add ability to load initial Timbre config from edn syst…
Browse files Browse the repository at this point in the history
…em value or resource
  • Loading branch information
ptaoussanis committed Oct 19, 2022
1 parent e4e6f6f commit b7941aa
Showing 1 changed file with 77 additions and 8 deletions.
85 changes: 77 additions & 8 deletions src/taoensso/timbre.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,90 @@
(set-config! default-config)
(infof "Hello %s" "world :-)"))

#?(:clj
(defn- load-config [edn source]
(let [clj
(try
(enc/read-edn edn)
(catch Throwable t
(throw
(ex-info "[Timbre config] Error reading config EDN"
{:edn edn :source source} t))))

config
(if (symbol? clj)
(if-let [var
(or
(resolve clj)
(when-let [ns (namespace clj)]
(require ns)
(resolve clj)))]
@var
(throw
(ex-info "[Timbre config] Failed to resolve config symbol"
{:edn edn :symbol symbol :source source})))
clj)]

(if (map? config)
(if (get config :load/overwrite?) ; Undocumented
(dissoc config :load/overwrite?) ; Without merge
(enc/nested-merge default-config config))

(throw
(ex-info "[Timbre config] Unexpected config value type"
{:value config :type (type config) :source source}))))))

#?(:clj
(def ^:private loaded-config
(when-let [[edn source]
(or
(let [prop-name "taoensso.timbre.config.edn"]
(when-let [edn (System/getProperty prop-name)]
[edn {:jvm-property prop-name}]))

(let [env-name "TAOENSSO_TIMBRE_CONFIG_EDN"]
(when-let [edn (System/getenv env-name)]
[edn {:env-var env-name}]))

(let [res-name
(or
(enc/get-sys-val ; Configurable resource name, undocumented
"taoensso.timbre.config-resource"
"TAOENSSO_TIMBRE_CONFIG_RESOURCE")

"taoensso.timbre.config.edn")]

(when-let [edn (enc/slurp-resource res-name)]
[edn {:resource res-name}])))]

(println (str "Loading Timbre config from: " source))
(load-config edn source))))

(enc/defonce ^:dynamic *config*
"This map controls all Timbre behaviour including:
- When to log (via level and namespace filtering)
- How to log (which appenders to use)
- What to log (config to control how data sent to appenders
will be formatted to output string)
See `default-config` for default value (and example config).
Initial config value will be (in descending order of preference):
1. taoensso.timbre.config.edn JVM property (read as EDN)
2. TAOENSSO_TIMBRE_CONFIG_EDN Env var (read as EDN)
3. ./taoensso.timbre.config.edn resource file (read as EDN)
4. Value of `default-config`
TODO ; mention symbols
See `default-config` for
Modify this config with `binding`, `alter-var-root`, or with utils:
Modify config value with `binding`, `alter-var-root`, or with utils:
`set-config!`, `with-config`,
`merge-config!`, `with-merged-config`,
`set-min-level!`, `with-min-level`,
`set-min-ns-level!`.
MAIN OPTIONS
MAIN OPTIONS (see `default-config` for example)
:min-level
Logging will occur only if a logging call's level is >= this
Expand Down Expand Up @@ -246,13 +314,14 @@
COMPILE-TIME LEVEL/NS ELISION
To control :min-level and :ns-filter at compile-time, use:
- `taoensso.timbre.min-level.edn` JVM property (read as edn)
- `taoensso.timbre.ns-pattern.edn` JVM property (read as edn)
- `taoensso.timbre.min-level.edn` JVM property (read as EDN)
- `taoensso.timbre.ns-pattern.edn` JVM property (read as EDN)
- `TAOENSSO_TIMBRE_MIN_LEVEL_EDN` env var (read as edn)
- `TAOENSSO_TIMBRE_NS_PATTERN_EDN` env var (read as edn)"
- `TAOENSSO_TIMBRE_MIN_LEVEL_EDN` env var (read as EDN)
- `TAOENSSO_TIMBRE_NS_PATTERN_EDN` env var (read as EDN)"

default-config)
#?(:clj (or loaded-config default-config)
:cljs default-config))

(defmacro with-config [config & body] `(binding [*config* ~config ] ~@body))
(defmacro with-merged-config [config & body] `(binding [*config* (enc/nested-merge *config* ~config)] ~@body))
Expand Down

0 comments on commit b7941aa

Please sign in to comment.