Skip to content

Commit

Permalink
[fix] [#381] Handle possible invalid stacktrace fonts
Browse files Browse the repository at this point in the history
Pretty v2 introduced an incompatible change to its font format.

Few (if any?) Timbre users actually customize these fonts beyond
possibly disabling them, so this shouldn't affect most folks.

But to be on the safe side, this commit introduces handling for
possible v1 style fonts.

There's 2 cases:

  1. [Compile-time] Invalid default fonts (set via environment variable / JVM property).
     In this case Timbre will throw, preventing compilation.

  2. [Runtime] Invalid fonts in output opts.
     In this case Timbre will log an error and ignore the invalid fonts.
  • Loading branch information
ptaoussanis committed Feb 26, 2024
1 parent b28eded commit 3d730f9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
46 changes: 35 additions & 11 deletions src/taoensso/timbre.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -899,13 +899,34 @@
:output-opts {:arg->str-fn (fn [_] "x")}}))

#?(:clj
(def ^:private default-stacktrace-fonts
(enc/get-env
{:as :edn
:default clj-commons.format.exceptions/default-fonts}
[:taoensso.timbre.default-stacktrace-fonts<.edn> ; Undocumented
:timbre-defaut-stacktrace-fonts<.edn> ; Legacy
])))
(do
;; Pretty v1 used string fonts, v2 uses unrelated keywords
(def ^:private invalid-stacktrace-fonts-msg "Invalid Timbre stacktrace fonts (see `org.clj-commons/pretty` docs)")
(def ^:private valid-stacktrace-fonts? (fn [st-fonts] (not (enc/rsome-kv (fn [k v] (string? v)) st-fonts))))
(def ^:private default-stacktrace-fonts
(let [st-fonts
(enc/get-env
{:as :edn :default fmt-ex/default-fonts}
[:taoensso.timbre.default-stacktrace-fonts<.edn> ; Undocumented
:timbre-defaut-stacktrace-fonts<.edn> ; Legacy
])]

(if (valid-stacktrace-fonts? st-fonts) ; May be nil or {}
(do st-fonts)
(throw
(ex-info invalid-stacktrace-fonts-msg
{:given-fonts st-fonts
:default-fonts fmt-ex/default-fonts})))))

(def ^:private valid-stacktrace-fonts!
(enc/fmemoize
(fn [st-fonts]
(if (valid-stacktrace-fonts? st-fonts)
(do st-fonts)
(do
;; Log error on first encounter
(error invalid-stacktrace-fonts-msg st-fonts)
default-stacktrace-fonts)))))))

(defn default-output-error-fn
"Default (fn [data]) -> string, used by `default-output-fn` to
Expand Down Expand Up @@ -935,10 +956,13 @@
(assoc data :?err c))))))

:clj
(binding [fmt-ex/*fonts*
(get output-opts :stacktrace-fonts
default-stacktrace-fonts)]
(fmt-ex/format-exception err)))))
(let [st-fonts
(if-let [e (find output-opts :stacktrace-fonts)]
(valid-stacktrace-fonts! (val e))
default-stacktrace-fonts)]

(binding [fmt-ex/*fonts* st-fonts]
(fmt-ex/format-exception err))))))

(comment
(default-output-error-fn
Expand Down
4 changes: 2 additions & 2 deletions wiki/1-Getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ lein cljsbuild once # Compile js with appropriate logging calls excluded
lein uberjar # Compile jar ''
```

## Stacktrace colors
## Stacktrace fonts

ANSI colors are enabled by default for Clojure stacktraces. To turn these off (e.g. for log files or emails), you can add the following entry to your top-level config or individual appender map/s:
ANSI fonts are enabled by default for Clojure stacktraces being printed to a console. To disable these, add the following entry to your top-level config or individual appender map/s:

```clojure
:output-opts {:stacktrace-fonts {}}
Expand Down

0 comments on commit 3d730f9

Please sign in to comment.