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
#[cfg(test)]
use std::{thread, time::Duration};

use fast_log::{
    consts::LogSize,
    error::LogError,
    plugin::{file_split::RollingType, packer::LogPacker},
    Logger,
};

pub mod json;

#[derive(Clone)]
pub struct Log<'a> {
    pub file_path: &'a str,
    pub temp_size: LogSize,
    pub chan_len: Option<usize>,
    pub roll_type: RollingType,
    pub packer: LogPacker,
}
impl Default for Log<'_> {
    fn default() -> Self {
        Self {
            file_path: "logs/out.log",
            temp_size: LogSize::MB(100),
            chan_len: Some(100000),
            roll_type: RollingType::All,
            packer: LogPacker {},
        }
    }
}

impl Log<'_> {
    pub fn init(&self) -> Result<&Logger, LogError> {
        fast_log::init(
            fast_log::Config::new()
                .console()
                .chan_len(self.chan_len)
                .file_split(
                    self.file_path,
                    self.temp_size,
                    self.roll_type,
                    self.packer.clone(),
                ),
        )
    }
}

#[test]
fn test_log() {
    Log::default().init().unwrap();
    log::info!("test log...");
    thread::sleep(Duration::from_secs(1));
}