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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! Functions and types for formatting log messages
//!
use ;
use Colorize;
use Formatter;
use ;
/// Customizable styles for [`colog`](crate)
///
/// All functions on this trait come with a provided default implementation.
/// This is how to the default style is provided.
///
/// To create a custom style, make a new type which implements [`CologStyle`],
/// overriding one or more of the default implementations on the trait.
///
/// # Examples
///
/// ```rust
/// #[macro_use]
/// extern crate log;
/// use colog::format::CologStyle;
/// use env_logger::Builder;
/// use log::{Level, LevelFilter};
///
/// // unless we want state, we don't need any fields on our style type
/// pub struct CustomLevelToken;
///
/// // implement CologStyle on our type, and override `level_token`
/// impl CologStyle for CustomLevelToken {
/// fn level_token(&self, level: &Level) -> &str {
/// match *level {
/// Level::Error => "ERR",
/// Level::Warn => "WRN",
/// Level::Info => "INF",
/// Level::Debug => "DBG",
/// Level::Trace => "TRC",
/// }
/// }
/// }
///
/// fn main() {
/// let mut builder = Builder::new();
///
/// // this is where we enable our custom styling
/// builder.format(colog::formatter(CustomLevelToken));
///
/// // set a custom filter level
/// builder.filter(None, LevelFilter::Trace);
///
/// // initialize the logger
/// builder.init();
///
/// error!("error message");
/// error!("error with fmt: {}", 42);
/// warn!("warn message");
/// info!("info message");
/// debug!("debug message");
/// trace!("trace message");
/// }
/// ```
///
/// Default colog style. Can manually be enabled like so:
/// ```rust
/// use log::info;
/// use colog::format::{CologStyle, DefaultCologStyle};
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(colog::formatter(DefaultCologStyle));
/// /* ... */
/// builder.init();
///
/// info!("logging ready");
///
;
/// Format a message for a particular log level
///
/// # Parameters
///
/// - `level`: The log level of the message
/// - `msg`: The message text
///
/// # Returns
///
/// A [`String`] which is formatted according to the level. Typically, this
/// is done by wrapping the string in terminal color codes, appropriate for
/// the log level.
///
/// # Defaults
///
/// | Level | Color |
/// |------------------|---------|
/// | [`Level::Error`] | red |
/// | [`Level::Warn`] | yellow |
/// | [`Level::Info`] | green |
/// | [`Level::Debug`] | green |
/// | [`Level::Trace`] | magenta |
///
/// Default implementation for [`CologStyle::level_token`]
///
/// # Parameters
///
/// - `level`: The log level of the message
///
/// # Returns
///
/// A [`&str`] which represents the token name for the given log level.
///
/// | Level | Token |
/// |------------------|-------|
/// | [`Level::Error`] | `"E"` |
/// | [`Level::Warn`] | `"W"` |
/// | [`Level::Info`] | `"*"` |
/// | [`Level::Debug`] | `"D"` |
/// | [`Level::Trace`] | `"T"` |
///
pub const
/// Default implementation for [`CologStyle::prefix_token`]
///
/// # Returns
///
/// Formats the level token ([`style.level_token`]) using
/// [`style.level_color`], wrapped in `[` and `]` formatted in bold blue.
///
/// Default implementation for [`CologStyle::format`]
///
/// # Returns
///
/// Inject [`style.line_separator`] prefix at newlines, and format result according to
/// [`style.prefix_token`].
///
/// (this is the default [`colog`](crate) style)
///
/// # Errors
///
/// Returns [`std::io::Error`] on write error.