-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtestasynclog.rs
148 lines (130 loc) · 5.34 KB
/
testasynclog.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
use std::{borrow::BorrowMut, sync::Arc};
use tklog::{
async_debug, async_debugs, async_error, async_errors, async_fatal, async_fatals, async_info, async_infos, async_trace, async_traces, async_warn, async_warns, Format, LogContext, ASYNC_LOG, LEVEL, MODE
};
use tokio::{sync::Mutex, time::Instant};
async fn async_log_init() {
ASYNC_LOG
.set_console(true)
.set_level(LEVEL::Debug)
.set_format(Format::LevelFlag | Format::Date | Format::Time | Format::ShortFileName)
.set_cutmode_by_size("tklog_async.log", 10000, 10, true)
.await;
}
#[tokio::test]
async fn testlog() {
async_log_init().await;
async_trace!("trace>>>>", "aaaaaaaaaaaa", 1, 2, 3);
async_debug!("debug>>>>", "aaaaaaaaaaaa", 1, 2, 3);
ASYNC_LOG.set_separator("|");
async_info!("info>>>>", "bbbbbbbbbbbb", 1, 2, 3);
async_warn!("warn>>>>", "ccccccccccccc", 1, 2, 3);
ASYNC_LOG.set_separator(",");
async_error!("error>>>>", "ddddddddddddd", 1, 2, 3);
async_fatal!("fatal>>>>", "eeeeeeeeeeeeee", 1, 2, 3);
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
}
#[tokio::test]
async fn testmultilogs() {
let mut log = tklog::Async::Logger::new();
log.set_console(true)
.set_level(LEVEL::Debug)
.set_separator(",")
.set_cutmode_by_time("tklogs.log", MODE::DAY, 10, true)
.await
.set_formatter("{message} | {time} {file}{level}\n");
let mut logger = Arc::clone(&Arc::new(Mutex::new(log)));
let log = logger.borrow_mut();
async_traces!(log, "async_traces>>>>", "AAAAAAAAAA", 1, 2, 3);
async_debugs!(log, "async_debugs>>>>", "BBBBBBBBBB", 1, 2, 3);
async_infos!(log, "async_infos>>>>", "CCCCCCCCCC", 1, 2, 3);
async_warns!(log, "async_warns>>>>", "DDDDDDDDDD", 1, 2, 3);
async_errors!(log, "async_errors>>>>", "EEEEEEEEEEE", 1, 2, 3);
async_fatals!(log, "async_fatals>>>>", "FFFFFFFFFFFF", 1, 2, 3);
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
}
#[tokio::test]
async fn testformats() {
let mut log = tklog::Async::Logger::new();
log.set_console(true)
.set_level(LEVEL::Debug)
.set_cutmode_by_time("tklogasyncs.log", MODE::DAY, 10, true)
.await;
let mut logger = Arc::clone(&Arc::new(Mutex::new(log)));
let log = logger.borrow_mut();
let v = vec![1, 2, 3];
tklog::async_formats!(log, LEVEL::Debug, "Debug>>>{},{}>>>{:?}", 1, 2, v);
let v2 = vec!['a', 'b'];
tklog::async_formats!(log, LEVEL::Info, "Info>>>{},{}>>{:?}", 1, 2, v2);
tklog::async_formats!(log, LEVEL::Warn, "Warn>>>{},{}", 1, 2);
tklog::async_formats!(log, LEVEL::Error, "Error>>>{},{}", 1, 2);
tklog::async_formats!(log, LEVEL::Fatal, "Fatal>>>{},{}", 1, 2);
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}
#[tokio::test]
async fn testlogsize() {
let mut log = tklog::Async::Logger::new();
log.set_console(true)
.set_level(LEVEL::Debug)
.set_cutmode_by_time("tklogs.log", MODE::DAY, 10, true)
.await;
let logger = Arc::new(Mutex::new(log));
let mut handles = vec![];
for i in 0..100 {
let mut log = logger.clone();
let handle = tokio::spawn(async move {
let logmut = log.borrow_mut();
async_debugs!(logmut, "thread>>", i, format!("{:?}", Instant::now()))
});
handles.push(handle);
}
for handle in handles {
handle.await.unwrap();
}
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}
#[tokio::test]
async fn test_custom() {
fn custom_handle(lc: &LogContext) -> bool {
println!("level >>>>>>>>>>>>>>>>>{:?}", lc.level);
println!("message >>>>>>>>>>>>>>>>>{:?}", lc.log_body);
println!("filename >>>>>>>>>>>>>>>>>{:?}", lc.filename);
println!("line >>>>>>>>>>>>>>>>>{:?}", lc.line);
println!("modname >>>>>>>>>>>>>>>>>{:?}", lc.modname);
if lc.level == LEVEL::Debug {
println!("{}", "debug now");
return false;
}
true
}
ASYNC_LOG.set_custom_handler(custom_handle);
async_debug!("000000000000000000");
async_info!("1111111111111111111");
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}
#[tokio::test]
async fn test_custom_multi() {
fn custom_handle(lc: &LogContext) -> bool {
println!("level >>>>>>>>>>>>>>>>>{:?}", lc.level);
println!("message >>>>>>>>>>>>>>>>>{:?}", lc.log_body);
println!("filename >>>>>>>>>>>>>>>>>{:?}", lc.filename);
println!("line >>>>>>>>>>>>>>>>>{:?}", lc.line);
println!("modname >>>>>>>>>>>>>>>>>{:?}", lc.modname);
if lc.level == LEVEL::Debug {
println!("{}", "debug now");
return false;
}
true
}
let mut log = tklog::Async::Logger::new();
log.set_custom_handler(custom_handle);
let mut logger = Arc::clone(&Arc::new(Mutex::new(log)));
let log = logger.borrow_mut();
async_traces!(log, "async_traces>>>>", "AAAAAAAAAA", 1, 2, 3);
async_debugs!(log, "async_debugs>>>>", "BBBBBBBBBB", 1, 2, 3);
async_infos!(log, "async_infos>>>>", "CCCCCCCCCC", 1, 2, 3);
async_warns!(log, "async_warns>>>>", "DDDDDDDDDD", 1, 2, 3);
async_errors!(log, "async_errors>>>>", "EEEEEEEEEEE", 1, 2, 3);
async_fatals!(log, "async_fatals>>>>", "FFFFFFFFFFFF", 1, 2, 3);
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}