Add a function to trunkate x-request-id in console output

Function can be overridden in config. Also made level colorizing configurable

Fixes #2
parent 0a00834d
# Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
## [Unreleased]
## [0.1.9] - 2019-08-02
### Added
- Red color for WARN and ERROR in default console output
- Configurable function to colorize log level in console output. WARN and ERROR printed in red by default
- Configurable function to format the x-request-id in console output. Truncated string by default
## [0.1.8] - 2019-05-15
### Fixed
......@@ -39,7 +39,8 @@ All notable changes to this project will be documented in this file. This change
- Wraps most common Timbre functions.
- Pre configured setup for development and NSD elk logging
[Unreleased]: https://gitlab.nsd.no/clojure/envelope/compare/0.1.8...HEAD
[Unreleased]: https://gitlab.nsd.no/clojure/envelope/compare/0.1.9...HEAD
[0.1.9]: https://gitlab.nsd.no/clojure/envelope/compare/0.1.8...0.1.9
[0.1.8]: https://gitlab.nsd.no/clojure/envelope/compare/0.1.7...0.1.8
[0.1.7]: https://gitlab.nsd.no/clojure/envelope/compare/0.1.6...0.1.7
[0.1.6]: https://gitlab.nsd.no/clojure/envelope/compare/0.1.4...0.1.6
......
......@@ -112,7 +112,15 @@ In addition to the `Timbre` fields, `Envelope` supports the following fields:
; Same as Tibres :level field, but with default value depending on :log-to-elk. If
; not explicitly set, it will be :info when :log-to-elk is true and :debug when :log-to-elk is false
:level :debug}
:level :debug
; A function (string -> string) that formats the x-request-id in the output when using the default console
; output fn. Defaults to a function truncating the string to its last 12 chars and prepending "..."
x-request-formatter x-request-id-formatter
; A function (string -> string) that applies formatting to the log level in the output when using the default
; console output fn. Defaults to a function printing WARN and ERROR in red
level-formatter colorize-level}
```
See the [Timbre
......
......@@ -5,7 +5,53 @@
#?(:clj [clojure.data.json :as json])
#?(:clj [taoensso.timbre.tools.logging :as cljlog])))
(def ansi-colors ; from https://github.com/ams-clj/clansi/blob/master/src/clansi/core.clj
{:default "[39m"
:white "[37m"
:black "[30m"
:red "[31m"
:green "[32m"
:blue "[34m"
:yellow "[33m"
:magenta "[35m"
:cyan "[36m"})
(defn colorize-string-ansi
[s color]
(str \u001b "[0m"
\u001b (get ansi-colors color "[0m")
s
\u001b "[0m"))
(defn colorize-string
[s color]
#?(:clj (colorize-string-ansi s color)
:default (str s)))
(def level-colors
{"WARN" :red
"ERROR" :red})
(defn colorize-level
[level-str]
(colorize-string level-str (get level-colors level-str :default)))
(defn x-request-id-formatter
[s]
(->> s
reverse
(take 12)
reverse
(#(if (< 12 (count s))
(concat (repeat 3 ".") %)
%))
(apply str)))
(defonce default-config {:log-to-elk true
:x-request-formatter x-request-id-formatter
:level-formatter colorize-level
:service-name "Unknown"
:service-version "0.0.0"
:console-time-pattern "HH:mm:ss.SSS"})
......@@ -18,9 +64,6 @@
#?(:clj (json/write-str data)
:cljs (.stringify js/JSON (clj->js data))))
(comment
#_(.toISOString (new js/Date)))
(defn nsd-logstash-format-fn [config data]
"NSD format, compliant with https://gitlab.nsd.no/nsd-commons/log-schema"
(let [{:keys [level ?err msg_ ?ns-str ?file hostname_
......@@ -69,35 +112,7 @@
:request
:response]))))
(def ansi-colors ; from https://github.com/ams-clj/clansi/blob/master/src/clansi/core.clj
{:default "[39m"
:white "[37m"
:black "[30m"
:red "[31m"
:green "[32m"
:blue "[34m"
:yellow "[33m"
:magenta "[35m"
:cyan "[36m"})
(defn colorize-string-ansi [s color]
(str \u001b "[0m"
\u001b (get ansi-colors color "[0m")
s
\u001b "[0m"))
(defn colorize-string [s color]
#?(:clj (colorize-string-ansi s color)
:default (str s)))
(def level-colors
{"WARN" :red
"ERROR" :red})
(defn colorize-level [level-str]
(colorize-string level-str (get level-colors level-str :default)))
(defn local-console-format-fn [data]
(defn local-console-format-fn [config data]
"A simpler log format, suitable for readable logs during development. colorized stacktraces"
(let [{:keys [level ?err msg_ ?ns-str ?file hostname_
timestamp_ ?line context]} data
......@@ -111,10 +126,10 @@
(str ts
(when (:x-request-id context)
(str " ["
(:x-request-id context)
((or (:x-request-formatter config) str) (:x-request-id context))
"]"))
" "
(colorize-level (str/upper-case (name level)))
((or (:level-colorizer config) str) (str/upper-case (name level)))
" "
(force msg_))
?err
......@@ -128,15 +143,15 @@
elk? (:log-to-elk current-conf)]
(timbre/merge-config! (merge current-conf
{:timestamp-opts (if elk?
{:pattern "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"}
{:pattern (:console-time-pattern current-conf)
{:pattern "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"}
{:pattern (:console-time-pattern current-conf)
:timezone :jvm-default})
:level (or (:level current-conf)
(if elk? :info :debug))
:output-fn (if elk?
(partial nsd-logstash-format-fn current-conf)
(or (:output-fn current-conf)
local-console-format-fn))}))
(partial local-console-format-fn current-conf)))}))
current-conf))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment