demo_multi/
demo-multi.rs

1// Copyright 2021 Twitter, Inc.
2// Licensed under the Apache License, Version 2.0
3// http://www.apache.org/licenses/LICENSE-2.0
4
5use core::time::Duration;
6use ringlog::*;
7
8macro_rules! command {
9    ($($arg:tt)*) => (
10        // we choose error level here because it is the lowest level and will
11        // not be filtered unless the level filter is set to `off`
12        error!(target: "command", $($arg)*);
13    )
14}
15
16macro_rules! noplog {
17    ($($arg:tt)*) => (
18        // we choose error level here because it is the lowest level and will
19        // not be filtered unless the level filter is set to `off`
20        error!(target: "noplog", $($arg)*);
21    )
22}
23
24fn main() {
25    let default = LogBuilder::new()
26        .output(Box::new(Stdout::new()))
27        .build()
28        .expect("failed to initialize default log");
29
30    let command = LogBuilder::new()
31        .output(Box::new(
32            File::new("command.log", "command.old", 100).expect("failed to create file log"),
33        ))
34        .format(klog_format)
35        .build()
36        .expect("failed to initialize command log");
37
38    let noplog = NopLogBuilder::new().build();
39
40    let mut drain = MultiLogBuilder::new()
41        .default(default)
42        .add_target("command", command)
43        .add_target("noplog", noplog)
44        .build()
45        .start();
46
47    std::thread::spawn(move || loop {
48        let _ = drain.flush();
49        std::thread::sleep(Duration::from_millis(100));
50    });
51
52    error!("error");
53    warn!("warning");
54    info!("info");
55    debug!("debug");
56    trace!("trace");
57
58    command!("\"get 0\" 0 0");
59
60    noplog!("this won't get displayed");
61
62    std::thread::sleep(Duration::from_millis(1000));
63}