aboutsummaryrefslogtreecommitdiff
path: root/test/com/github/ivarref/yoltq/log_init.clj
blob: f3fb6dc9996b4d2d8366fe3d6e495824ac2cdf41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(ns com.github.ivarref.yoltq.log-init
  (:require [clojure.term.colors :as colors]
            [taoensso.timbre :as timbre]
            [clojure.string :as str]))

(set! *warn-on-reflection* true)

(def level-colors
  {;:warn colors/red
   :error colors/red})

(def ^:dynamic *override-color* nil)

(defn min-length [n s]
  (loop [s s]
    (if (>= (count s) n)
      s
      (recur (str s " ")))))

(defn local-console-format-fn
  "A simpler log format, suitable for readable logs during development. colorized stacktraces"
  [data]
  (try
    (let [{:keys [level ?err msg_ ?ns-str ?file hostname_
                  timestamp_ ?line context]} data
          ts (force timestamp_)]
      (let [color-f (if (nil? *override-color*)
                      (get level-colors level str)
                      colors/green)
            maybe-stacktrace (when ?err
                               (str "\n" (timbre/stacktrace ?err)))]
        (cond-> (str #_(str ?ns-str ":" ?line)
                  #_(min-length (count "[yoltq:326] ")
                                (str
                                  "["
                                  (some-> ?ns-str
                                          (str/split #"\.")
                                          (last))
                                  ":" ?line))
                  ts
                  " "
                  (color-f (min-length 5 (str/upper-case (name level))))
                  " "

                  (when-let [x-req-id (:x-request-id context)]
                    (str "[" x-req-id "] "))
                  #_(.getName ^Thread (Thread/currentThread))

                  (color-f (force msg_))

                  #_maybe-stacktrace))))


    (catch Throwable t
      (println "error in local-console-format-fn:" (ex-message t))
      nil)))


(defn init-logging! [min-levels]
  (timbre/merge-config!
    {:min-level min-levels
     :timestamp-opts {:pattern "HH:mm:ss.SSS"
                      :timezone :jvm-default}
     :output-fn (fn [data] (local-console-format-fn data))
     :appenders {:println (timbre/println-appender {:stream :std-out})}}))