-
Notifications
You must be signed in to change notification settings - Fork 315
/
lib.rs
516 lines (441 loc) · 16.7 KB
/
lib.rs
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
pub mod macros;
/// Metadata about the log entry
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LogMetadata {
pub level: slog::Level,
pub module_path: &'static str,
pub line: u32,
pub column: u32,
}
/// Logs data of type `T`
pub trait Logger<T>: Clone {
/// Log the given log event
fn log(&self, message: String, data: T, metadata: LogMetadata);
/// Return true if events should be logged at the given level and module,
/// false otherwise
fn is_enabled_at(&self, level: slog::Level, module_path: &'static str) -> bool;
/// Return true if a `sample!` log with the given key and value should be
/// logged, false otherwise
fn should_sample<V: Into<u32>>(&self, key: String, value: V) -> bool;
/// Return true if a log with the given tag should be logged, false
/// otherwise
fn is_tag_enabled(&self, tag: String) -> bool;
/// Return true if this is the first log in n seconds, false otherwise
fn is_n_seconds<V: Into<i32>>(&self, seconds: V, metadata: LogMetadata) -> bool;
}
/// A logger that holds context that can be updated and logged
///
/// Methods of this trait should not be used directly, instead use the macros
/// found in macros.rs
#[derive(Debug, Clone)]
pub struct ContextLogger<C, L>
where
C: Clone + Default,
L: Logger<C>,
{
pub context: C,
pub inner_logger: L,
}
impl<C, L> Default for ContextLogger<C, L>
where
C: Clone + Default,
L: Logger<C> + Default,
{
fn default() -> Self {
Self {
context: Default::default(),
inner_logger: Default::default(),
}
}
}
impl<C, L> From<slog::Logger> for ContextLogger<C, L>
where
C: Clone + Default,
L: Logger<C> + From<slog::Logger>,
{
fn from(logger: slog::Logger) -> Self {
Self {
context: Default::default(),
inner_logger: logger.into(),
}
}
}
impl<C, L> ContextLogger<C, L>
where
C: Clone + Default,
L: Logger<C>,
{
pub fn new(logger: L) -> Self {
Self {
context: C::default(),
inner_logger: logger,
}
}
pub fn get_context(&self) -> C {
self.context.clone()
}
pub fn with_new_context(&self, context: C) -> Self {
Self {
context,
inner_logger: self.inner_logger.clone(),
}
}
pub fn log(&self, message: String, context: C, metadata: LogMetadata) {
self.inner_logger.log(message, context, metadata)
}
pub fn is_enabled_at(&self, level: slog::Level, module_path: &'static str) -> bool {
self.inner_logger.is_enabled_at(level, module_path)
}
pub fn should_sample<T: Into<u32>>(&self, key: String, value: T) -> bool {
self.inner_logger.should_sample(key, value)
}
pub fn is_tag_enabled(&self, tag: String) -> bool {
self.inner_logger.is_tag_enabled(tag)
}
pub fn is_n_seconds<T: Into<i32>>(&self, seconds: T, metadata: LogMetadata) -> bool {
self.inner_logger.is_n_seconds(seconds, metadata)
}
}
#[cfg(test)]
mod tests {
use super::*;
/// A context type used for testing purposes
#[derive(Clone, Debug, Default, PartialEq)]
struct TestContext {
sub_context1: Option<TestSubContext1>,
sub_context2: Option<TestSubContext2>,
}
#[derive(Clone, Debug, Default, PartialEq)]
struct TestSubContext1 {
pub field_u64: u64,
pub field_opt_i32: Option<i32>,
pub field_string: String,
}
#[derive(Clone, Debug, Default, PartialEq)]
struct TestSubContext2 {
pub field_bool: bool,
}
/// A Logger that, instead of logging, checks expectations of what
/// would be logged
#[derive(Clone, Debug, PartialEq)]
struct ExpectationLogger {
context: TestContext,
expected_context: TestContext,
expected_message: String,
expected_level: slog::Level,
enabled_tags: Vec<String>,
}
impl ExpectationLogger {
pub fn new(level: slog::Level) -> Self {
Self {
context: Default::default(),
expected_context: Default::default(),
expected_message: Default::default(),
expected_level: level,
enabled_tags: vec![],
}
}
}
impl Logger<TestContext> for ExpectationLogger {
fn log(&self, message: String, context: TestContext, metadata: LogMetadata) {
assert_eq!(message, self.expected_message);
assert_eq!(context, self.expected_context);
assert_eq!(metadata.level, self.expected_level);
}
fn is_enabled_at(&self, _: slog::Level, _: &'static str) -> bool {
true
}
fn should_sample<T: Into<u32>>(&self, _key: String, _value: T) -> bool {
false
}
fn is_tag_enabled(&self, _tag: String) -> bool {
false
}
fn is_n_seconds<T: Into<i32>>(&self, _: T, _: LogMetadata) -> bool {
false
}
}
#[derive(Clone, Default)]
struct DisabledLogger;
impl Logger<TestContext> for DisabledLogger {
fn log(&self, _: String, _: TestContext, _: LogMetadata) {
panic!("Unexpected call to log()!");
}
fn is_enabled_at(&self, _: slog::Level, _: &'static str) -> bool {
false
}
fn should_sample<T: Into<u32>>(&self, _key: String, _value: T) -> bool {
false
}
fn is_tag_enabled(&self, _tag: String) -> bool {
false
}
fn is_n_seconds<T: Into<i32>>(&self, _: T, _: LogMetadata) -> bool {
false
}
}
#[derive(Clone, Default)]
struct TagLogger {
enabled_tags: Vec<String>,
}
impl Logger<TestContext> for TagLogger {
fn log(&self, _: String, _: TestContext, _: LogMetadata) {
panic!("Expected call to log()!");
}
fn is_enabled_at(&self, _: slog::Level, _: &'static str) -> bool {
true
}
fn should_sample<T: Into<u32>>(&self, _key: String, _value: T) -> bool {
false
}
fn is_tag_enabled(&self, tag: String) -> bool {
self.enabled_tags.contains(&tag)
}
fn is_n_seconds<T: Into<i32>>(&self, seconds: T, _: LogMetadata) -> bool {
seconds.into() <= 0
}
}
#[derive(Clone, Default)]
struct EveryNLogger {
count: std::sync::Arc<std::sync::atomic::AtomicU32>,
}
impl Logger<TestContext> for EveryNLogger {
fn log(&self, _: String, _: TestContext, _: LogMetadata) {
self.count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}
fn is_enabled_at(&self, _: slog::Level, _: &'static str) -> bool {
true
}
fn should_sample<T: Into<u32>>(&self, _key: String, _value: T) -> bool {
false
}
fn is_tag_enabled(&self, _tag: String) -> bool {
false
}
fn is_n_seconds<T: Into<i32>>(&self, seconds: T, _: LogMetadata) -> bool {
seconds.into() <= 0
}
}
#[test]
fn test_macro_new_logger() {
let inner_logger = ExpectationLogger::new(slog::Level::Info);
let logger = ContextLogger::<TestContext, ExpectationLogger>::new(inner_logger);
let logger = new_logger!(logger);
let mut logger = new_logger!(logger; sub_context1.field_u64 => 12u64);
logger.inner_logger.expected_context.sub_context1 = Some(TestSubContext1 {
field_u64: 12,
field_opt_i32: None,
field_string: "".into(),
});
info!(logger)
}
#[test]
fn test_disabled_logger() {
let inner_logger = DisabledLogger;
let logger = ContextLogger::<TestContext, DisabledLogger>::new(inner_logger);
info!(logger)
}
#[test]
#[should_panic(expected = "")]
fn test_fatal() {
let inner_logger = ExpectationLogger::new(slog::Level::Info);
let logger = ContextLogger::<TestContext, ExpectationLogger>::new(inner_logger);
fatal!(logger);
}
#[test]
#[should_panic(expected = "Self-destruct sequence initiated: 10, 9, 8...")]
fn test_fatal_with_message() {
let inner_logger = ExpectationLogger::new(slog::Level::Info);
let logger = ContextLogger::<TestContext, ExpectationLogger>::new(inner_logger);
fatal!(
logger,
"Self-destruct sequence initiated: {}, {}, {}...",
10,
9,
8
);
}
#[test]
#[should_panic(expected = "")]
fn test_fatal_with_context() {
let inner_logger = ExpectationLogger::new(slog::Level::Info);
let logger = ContextLogger::<TestContext, ExpectationLogger>::new(inner_logger);
let mut logger = new_logger!(logger; sub_context1.field_u64 => 12u64);
logger.inner_logger.expected_context.sub_context1 = Some(TestSubContext1 {
field_u64: 12,
field_opt_i32: Some(1),
field_string: "".into(),
});
fatal!(logger; sub_context1.field_opt_i32 => 1);
}
#[test]
#[should_panic(expected = "Fatal error")]
fn test_fatal_with_message_and_context() {
let inner_logger = ExpectationLogger::new(slog::Level::Info);
let logger = ContextLogger::<TestContext, ExpectationLogger>::new(inner_logger);
let mut logger = new_logger!(logger; sub_context1.field_u64 => 12u64);
logger.inner_logger.expected_context.sub_context1 = Some(TestSubContext1 {
field_u64: 12,
field_opt_i32: Some(1),
field_string: "".into(),
});
fatal!(logger, "Fatal error"; sub_context1.field_opt_i32 => 1);
}
/// If we log a tag that is enabled, a log call should be made. Calling
/// TagLogger's log method causes a panic, so we should expect the below
/// panic.
#[test]
#[should_panic(expected = "Expected call to log()!")]
fn test_enabled_tag_is_logged() {
let inner_logger = TagLogger::default();
let mut logger = ContextLogger::<TestContext, TagLogger>::new(inner_logger);
logger.inner_logger.enabled_tags = vec!["my_tag".to_string()];
info!(tag => "my_tag", logger, "Hello {} #{}{}", "world", 4, "!");
}
/// If we log a tag that is not enabled, a log call should not be made.
/// Calling TagLogger's log method causes a panic, so we should not
/// expect any panics.
#[test]
fn test_enabled_tag_is_not_logged() {
let inner_logger = TagLogger::default();
let logger = ContextLogger::<TestContext, TagLogger>::new(inner_logger);
info!(tag => "my_tag", logger, "Hello {} #{}{}", "world", 4, "!");
debug!(tag => "my_tag", logger, "Hello {} #{}{}", "world", 4, "!");
info!(tag => "my_tag", logger, "message");
debug!(tag => "my_tag", logger, "message");
}
/// Test the every_n_seconds `info!` calls don't log if the `is_n_seconds`
/// condition is not satisfied.
#[test]
fn test_every_n_seconds_info_does_not_log() {
let inner_logger = EveryNLogger {
count: std::sync::Arc::new(std::sync::atomic::AtomicU32::default()),
};
let logger = ContextLogger::<TestContext, EveryNLogger>::new(inner_logger);
info!(every_n_seconds => 1, logger, "Hello {} #{}{}", "world", 4, "!");
info!(every_n_seconds => 1, logger, "message");
info!(every_n_seconds => 1, logger ; sub_context1.field_opt_i32 => 1i32);
info!(every_n_seconds => 1, logger, "message" ; sub_context1.field_opt_i32 => 1);
assert!(
logger
.inner_logger
.count
.load(std::sync::atomic::Ordering::SeqCst)
== 0
);
}
/// Test the every_n_seconds `info!` calls log if the `is_n_seconds`
/// condition is satisfied.
#[test]
fn test_every_n_seconds_info_logs() {
let inner_logger = EveryNLogger {
count: std::sync::Arc::new(std::sync::atomic::AtomicU32::default()),
};
let logger = ContextLogger::<TestContext, EveryNLogger>::new(inner_logger);
info!(every_n_seconds => 0, logger, "Hello {} #{}{}", "world", 4, "!");
info!(every_n_seconds => 0, logger, "message");
info!(every_n_seconds => 0, logger ; sub_context1.field_opt_i32 => 1i32);
info!(every_n_seconds => 0, logger, "message" ; sub_context1.field_opt_i32 => 1);
assert!(
logger
.inner_logger
.count
.load(std::sync::atomic::Ordering::SeqCst)
== 4
);
}
/// Test the every_n_seconds `warn!` calls don't log if the `is_n_seconds`
/// condition is not satisfied.
#[test]
fn test_every_n_seconds_warn_does_not_log() {
let inner_logger = EveryNLogger {
count: std::sync::Arc::new(std::sync::atomic::AtomicU32::default()),
};
let logger = ContextLogger::<TestContext, EveryNLogger>::new(inner_logger);
warn!(every_n_seconds => 1, logger, "Hello {} #{}{}", "world", 4, "!");
warn!(every_n_seconds => 1, logger, "message");
warn!(every_n_seconds => 1, logger ; sub_context1.field_opt_i32 => 1i32);
warn!(every_n_seconds => 1, logger, "message" ; sub_context1.field_opt_i32 => 1);
assert!(
logger
.inner_logger
.count
.load(std::sync::atomic::Ordering::SeqCst)
== 0
);
}
/// Test the every_n_seconds `warn!` calls log if the `is_n_seconds`
/// condition is satisfied.
#[test]
fn test_every_n_seconds_warn_logs() {
let inner_logger = EveryNLogger {
count: std::sync::Arc::new(std::sync::atomic::AtomicU32::default()),
};
let logger = ContextLogger::<TestContext, EveryNLogger>::new(inner_logger);
warn!(every_n_seconds => 0, logger, "Hello {} #{}{}", "world", 4, "!");
warn!(every_n_seconds => 0, logger, "message");
warn!(every_n_seconds => 0, logger ; sub_context1.field_opt_i32 => 1i32);
warn!(every_n_seconds => 0, logger, "message" ; sub_context1.field_opt_i32 => 1);
assert!(
logger
.inner_logger
.count
.load(std::sync::atomic::Ordering::SeqCst)
== 4
);
}
/// Given one of the log macros (e.g. info!), generate a function that tests
/// all branches of the given macro
macro_rules! test_log_macro {
($name:ident, $log_macro:ident, $level:ident) => {
#[test]
fn $name() {
let inner_logger = ExpectationLogger::new(slog::Level::$level);
let mut logger = ContextLogger::<TestContext, ExpectationLogger>::new(inner_logger);
$log_macro!(logger);
logger.inner_logger.expected_message = "Hello world #4!".into();
$log_macro!(logger, "Hello {} #{}{}", "world", 4, "!");
logger.inner_logger.expected_message = "".into();
logger.inner_logger.expected_context.sub_context1 = Some(TestSubContext1 {
field_u64: 12,
field_opt_i32: Some(45),
field_string: "foo".into(),
});
logger.inner_logger.expected_context.sub_context2 = Some(TestSubContext2 {
field_bool: true
});
$log_macro!(
logger;
sub_context1.field_u64 => 12u64,
sub_context1.field_opt_i32 => 45,
sub_context1.field_string => "foo",
sub_context2.field_bool => true,
);
logger.inner_logger.expected_message = "foo bar".into();
$log_macro!(
logger,
"foo bar";
sub_context1.field_u64 => 12u64,
sub_context1.field_opt_i32 => 45,
sub_context1.field_string => "foo",
sub_context2.field_bool => true,
);
logger.inner_logger.expected_message = "1 2 3 4 5".into();
$log_macro!(
logger,
"{} {} {} {} {}", 1, 2, 3, 4, 5;
sub_context1.field_u64 => 12u64,
sub_context1.field_opt_i32 => 45,
sub_context1.field_string => "foo",
sub_context2.field_bool => true,
);
}
}
}
test_log_macro!(test_trace, trace, Trace);
test_log_macro!(test_debug, debug, Debug);
test_log_macro!(test_info, info, Info);
test_log_macro!(test_warn, warn, Warning);
test_log_macro!(test_error, error, Error);
test_log_macro!(test_crit, crit, Critical);
}