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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//! A logger compatible with [Elastic Common Schema (ECS) Logging](https://www.elastic.co/guide/en/ecs-logging/overview/current/intro.html).
//!
//! ## Features
//!
//! - Configurable via the `RUST_LOG` environment variable.
//! - Uses [env_logger] under the hood.
//! - **All logging is disabled except for the `error` level by default.**
//! - Logs are written to stderr by default.
//!
//! ## Installation
//!
//! Add the following to your `Cargo.toml` file:
//!
//! ```toml
//! [dependencies]
//! log = "0.4"
//! ecs-logger = "1"
//! ```
//!
//! ## Example
//!
//! In the following examples we assume the binary is `./example`.
//!
//! ### Basic logging
//!
//! ```
//! use log::{debug, error};
//!
//! ecs_logger::init();
//!
//! debug!(
//! "this is a debug {}, which is NOT printed by default",
//! "message"
//! );
//! error!("this is printed by default");
//! ```
//!
//! ```bash
//! $ ./example
//! {"@timestamp":"2021-11-26T15:25:22.321002600Z","log.level":"ERROR","message":"this is printed by default","ecs.version":"1.12.1","log.origin":{"file":{"line":13,"name":"example.rs"},"rust":{"target":"example::tests","module_path":"example::tests","file_path":"tests/example.rs"}}}
//! ```
//!
//! ```bash
//! $ RUST_LOG=debug ./example
//! {"@timestamp":"2021-11-26T15:26:13.524069Z","log.level":"DEBUG","message":"this is a debug message, which is NOT printed by default","ecs.version":"1.12.1","log.origin":{"file":{"line":9,"name":"example.rs"},"rust":{"target":"example::tests","module_path":"example::tests","file_path":"tests/example.rs"}}}
//! {"@timestamp":"2021-11-26T15:26:13.524193100Z","log.level":"ERROR","message":"this is printed by default","ecs.version":"1.12.1","log.origin":{"file":{"line":13,"name":"example.rs"},"rust":{"target":"example::tests","module_path":"example::tests","file_path":"tests/example.rs"}}}
//! ```
//!
//! More filtering config examples are available at [`env_logger`]'s documentation.
//!
//! ### Extra Fields
//!
//! You can add extra fields to the log output by using the [`extra_fields`] module.
//!
//! ```
//! use ecs_logger::extra_fields;
//! use serde::Serialize;
//!
//! #[derive(Serialize)]
//! struct MyExtraFields {
//! my_field: String,
//! }
//!
//! ecs_logger::init();
//!
//! extra_fields::set_extra_fields(MyExtraFields {
//! my_field: "my_value".to_string(),
//! }).unwrap();
//!
//! log::error!("Hello {}!", "world");
//! log::info!("Goodbye {}!", "world");
//!
//! extra_fields::clear_extra_fields();
//! ```
//!
//! ### Custom logging
//!
//! You need to add [`env_logger`] to your `Cargo.toml` for the following examples.
//!
//! ```toml
//! [dependencies]
//! log = "0.4"
//! env_logger = "0.9"
//! ecs-logger = "1"
//! ```
//!
//! #### Write to stdout
//!
//! ```
//! use log::info;
//!
//! // Initialize custom logger
//! env_logger::builder()
//! .format(ecs_logger::format) // Configure ECS logger
//! .target(env_logger::Target::Stdout) // Write to stdout
//! .init();
//!
//! info!("Hello {}!", "world");
//! ```
//!
//! #### Configure log filters
//!
//! ```
//! use log::info;
//!
//! // Initialize custom logger
//! env_logger::builder()
//! .parse_filters("info,my_app=debug") // Set filters
//! .format(ecs_logger::format) // Configure ECS logger
//! .init();
//!
//! info!("Hello {}!", "world");
//! ```
//!
//! ## Default log fields
//!
//! ```json
//! {
//! "@timestamp": "2021-11-26T15:25:22.321002600Z",
//! "log.level": "ERROR",
//! "message": "this is printed by default",
//! "ecs.version": "1.12.1",
//! "log.origin": {
//! "file": {
//! "line": 13,
//! "name": "example.rs"
//! },
//! "rust": {
//! "target": "example::tests",
//! "module_path": "example::tests",
//! "file_path": "tests/example.rs"
//! }
//! }
//! }
//! ```
use Event;
use merge_extra_fields;
use BorrowMut;
/// Initializes the global logger with an instance of [`env_logger::Logger`] with ECS-Logging formatting.
///
/// This should be called early in the execution of a Rust program. Any log events that occur before initialization will be ignored.
///
/// # Panics
///
/// This function will panic if it is called more than once, or if another library has already initialized a global logger.
///
/// # Example
///
/// ```
/// use log::error;
///
/// error!("this is NOT logged");
///
/// ecs_logger::init();
///
/// error!("this is logged");
/// ```
/// Attempts to initialize the global logger with an instance of [`env_logger::Logger`] with ECS-Logging formatting.
///
/// This should be called early in the execution of a Rust program. Any log events that occur before initialization will be ignored.
///
/// # Errors
///
/// This function returns [`log::SetLoggerError`] if it is called more than once, or if another library has already initialized a global logger.
///
/// # Example
///
/// ```
/// use log::error;
///
/// error!("this is NOT logged");
///
/// assert!(ecs_logger::try_init().is_ok());
///
/// error!("this is logged");
///
/// // try_init should not be called more than once
/// assert!(ecs_logger::try_init().is_err());
/// ```
/// Writes an ECS log line to the `buf`.
///
/// You may pass this format function to [`env_logger::Builder::format`] when building a custom logger.
///
/// # Example
///
/// ```
/// use log::info;
///
/// // Initialize custom logger
/// env_logger::builder()
/// .parse_filters("info,my_app=debug") // Set filters
/// .format(ecs_logger::format) // Configure ECS logger
/// .target(env_logger::Target::Stdout) // Write to stdout
/// .init();
///
/// info!("Hello {}!", "world");
/// ```
}