-
Notifications
You must be signed in to change notification settings - Fork 2
/
pretty.clj
164 lines (141 loc) · 4.4 KB
/
pretty.clj
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(ns dialog.format.pretty
"Log format which presents events in ANSI-colored text, suitable for human
consumption in a REPL console."
(:require
[clojure.string :as str]
[io.aviso.ansi :as ansi]
[io.aviso.exception :as ex])
(:import
(java.time
LocalDateTime
ZoneId)
(java.time.format
DateTimeFormatter)))
(defn- field-widths
"Determine the configured field padding widths for the output formatter."
[output]
(let [padding (:padding output true)]
(merge
{:level 5
:thread 24
:logger 30}
(cond
(map? padding)
padding
(false? padding)
{:level 0
:thread 0
:logger 0}))))
(defn- rpad
"Pad a string on the right with spaces to make it fit a certain visual width."
[string width]
(if (pos-int? width)
(let [vlen (ansi/visual-length string)]
(if (<= width vlen)
string
(apply str string (repeat (- width vlen) " "))))
string))
(defn- timestamp-formatter
"Constructs a new function to format instants using the given format string."
[format-key]
(case format-key
:full
str
:short
(let [dt-fmt (DateTimeFormatter/ofPattern "HH:mm:ss.SSS")
utc (ZoneId/of "UTC")]
(fn format-short
[instant]
(.format dt-fmt (LocalDateTime/ofInstant instant utc))))))
(defn- format-thread
"Format a thread name for printing."
[thread]
(str "["
(ansi/green
(if thread
(if (str/starts-with? (str/lower-case thread) "nrepl-session-")
"nREPL"
thread)
"-"))
"]"))
(defn- format-level
"Format a log level for printing."
[level]
(let [level-str (str/upper-case (name level))]
(case level
:fatal (ansi/bold-magenta level-str)
:error (ansi/bold-red level-str)
:warn (ansi/red level-str)
:info (ansi/blue level-str)
level-str)))
(defn- format-logger
"Format a logger name to fit within the desired max length."
[logger max-length]
(ansi/cyan
(cond
;; Don't do trimming
(not (pos-int? max-length))
logger
;; Logger name fits in limit
(<= (count logger) max-length)
logger
;; Collapse logger segments
:else
(loop [collapsed []
parts (str/split logger #"\.")]
(let [candidate (str/join "." (concat collapsed parts))]
(if (< max-length (count candidate))
;; Need to trim more.
(if-let [next-part (first parts)]
;; Collapse next part in the ns into a single character.
(recur (conj collapsed (subs next-part 0 1))
(rest parts))
;; No more parts, just truncate it.
(subs candidate 0 max-length))
;; Abbreviated logger fits within limit.
candidate))))))
(defn formatter
"Construct a pretty event formatting function.
Formatting options may include:
- `:padding`
Either true (the default) to pad fields to standard fixed widths, false to
print them with no padding, or a map with `:level`, `:thread`, and `:logger`
widths to specify custom amounts.
- `:timestamp`
Either `:full` (the default) which shows the entire timestamp value, or
`:short` which will render only the local time portion."
[output]
(let [widths (field-widths output)
level-width (:level widths)
thread-width (:thread widths)
logger-width (:logger widths)
format-time (timestamp-formatter (:timestamp output :full))]
(fn format-message
[event]
(str
;; Timestamp
(format-time (:time event))
" "
;; Thread
(rpad (format-thread (:thread event)) thread-width)
" "
;; Level
(rpad (format-level (:level event)) level-width)
" "
;; Logger
(rpad (format-logger (str (:logger event)) logger-width) logger-width)
" "
;; Message
(or (:message event) "-")
;; Duration
(when-let [duration (:duration event)]
(format " (%.3f ms)" duration))
;; Custom trailer
(when-let [tail (:dialog.format/tail (meta event))]
(str " " tail))
;; Extra Data
(when-let [extra (:dialog.format/extra (meta event))]
(str " " (ansi/cyan (pr-str extra))))
;; Exceptions
(when-let [ex (:error event)]
(str "\n" (ex/format-exception ex)))))))