1
- use std:: { env, sync:: Arc } ;
1
+ use std:: { env, sync:: Arc , time :: Duration } ;
2
2
3
3
use spdlog:: {
4
4
prelude:: * ,
@@ -7,7 +7,10 @@ use spdlog::{
7
7
8
8
fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
9
9
configure_file_logger ( ) ?;
10
- configure_rotating_file_logger ( ) ?;
10
+ configure_rotating_daily_file_logger ( ) ?;
11
+ configure_rotating_size_file_logger ( ) ?;
12
+ configure_rotating_hourly_file_logger ( ) ?;
13
+ configure_rotating_period_file_logger ( ) ?;
11
14
12
15
Ok ( ( ) )
13
16
}
@@ -24,8 +27,8 @@ fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
24
27
Ok ( ( ) )
25
28
}
26
29
27
- fn configure_rotating_file_logger ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
28
- let path = env:: current_exe ( ) ?. with_file_name ( "rotating .log" ) ;
30
+ fn configure_rotating_daily_file_logger ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
31
+ let path = env:: current_exe ( ) ?. with_file_name ( "rotating_daily .log" ) ;
29
32
30
33
let file_sink = Arc :: new (
31
34
RotatingFileSink :: builder ( )
@@ -36,7 +39,60 @@ fn configure_rotating_file_logger() -> Result<(), Box<dyn std::error::Error>> {
36
39
let new_logger = Arc :: new ( Logger :: builder ( ) . sink ( file_sink) . build ( ) ?) ;
37
40
spdlog:: set_default_logger ( new_logger) ;
38
41
39
- info ! ( "this log will be written to the file `rotating.log`, and the file will be rotated daily at 00:00" ) ;
42
+ info ! ( "this log will be written to the file `rotating_daily.log`, and the file will be rotated daily at 00:00" ) ;
43
+
44
+ Ok ( ( ) )
45
+ }
46
+
47
+ fn configure_rotating_size_file_logger ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
48
+ let path = env:: current_exe ( ) ?. with_file_name ( "rotating_size.log" ) ;
49
+
50
+ let file_sink = Arc :: new (
51
+ RotatingFileSink :: builder ( )
52
+ . base_path ( path)
53
+ . rotation_policy ( RotationPolicy :: FileSize ( 1024 ) )
54
+ . build ( ) ?,
55
+ ) ;
56
+ let new_logger = Arc :: new ( Logger :: builder ( ) . sink ( file_sink) . build ( ) ?) ;
57
+ spdlog:: set_default_logger ( new_logger) ;
58
+
59
+ info ! ( "this log will be written to the file `rotating_size.log`, and the file will be rotated when its size reaches 1024 bytes" ) ;
60
+
61
+ Ok ( ( ) )
62
+ }
63
+
64
+ fn configure_rotating_hourly_file_logger ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
65
+ let path = env:: current_exe ( ) ?. with_file_name ( "rotating_hourly.log" ) ;
66
+
67
+ let file_sink = Arc :: new (
68
+ RotatingFileSink :: builder ( )
69
+ . base_path ( path)
70
+ . rotation_policy ( RotationPolicy :: Hourly )
71
+ . build ( ) ?,
72
+ ) ;
73
+ let new_logger = Arc :: new ( Logger :: builder ( ) . sink ( file_sink) . build ( ) ?) ;
74
+ spdlog:: set_default_logger ( new_logger) ;
75
+
76
+ info ! ( "this log will be written to the file `rotating_hourly.log`, and the file will be rotated every hour" ) ;
77
+
78
+ Ok ( ( ) )
79
+ }
80
+
81
+ fn configure_rotating_period_file_logger ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
82
+ let path = env:: current_exe ( ) ?. with_file_name ( "rotating_period.log" ) ;
83
+
84
+ let file_sink = Arc :: new (
85
+ RotatingFileSink :: builder ( )
86
+ . base_path ( path)
87
+ . rotation_policy ( RotationPolicy :: Period ( Duration :: from_secs (
88
+ 60 * 90 , // 90 minutes
89
+ ) ) )
90
+ . build ( ) ?,
91
+ ) ;
92
+ let new_logger = Arc :: new ( Logger :: builder ( ) . sink ( file_sink) . build ( ) ?) ;
93
+ spdlog:: set_default_logger ( new_logger) ;
94
+
95
+ info ! ( "this log will be written to the file `rotating_period.log`, and the file will be rotated every 1.5 hours" ) ;
40
96
41
97
Ok ( ( ) )
42
98
}
0 commit comments