1- use std:: { env, sync :: Arc , time:: Duration } ;
1+ use std:: { env, time:: Duration } ;
22
33use spdlog:: {
44 prelude:: * ,
@@ -18,8 +18,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1818fn configure_file_logger ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
1919 let path = env:: current_exe ( ) ?. with_file_name ( "file.log" ) ;
2020
21- let file_sink = Arc :: new ( FileSink :: builder ( ) . path ( path) . build ( ) ?) ;
22- let new_logger = Arc :: new ( Logger :: builder ( ) . sink ( file_sink) . build ( ) ?) ;
21+ let file_sink = FileSink :: builder ( ) . path ( path) . build_arc ( ) ?;
22+ let new_logger = Logger :: builder ( ) . sink ( file_sink) . build_arc ( ) ?;
2323 spdlog:: set_default_logger ( new_logger) ;
2424
2525 info ! ( "this log will be written to the file `all.log`" ) ;
@@ -30,13 +30,11 @@ fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
3030fn configure_rotating_daily_file_logger ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
3131 let path = env:: current_exe ( ) ?. with_file_name ( "rotating_daily.log" ) ;
3232
33- let file_sink = Arc :: new (
34- RotatingFileSink :: builder ( )
35- . base_path ( path)
36- . rotation_policy ( RotationPolicy :: Daily { hour : 0 , minute : 0 } )
37- . build ( ) ?,
38- ) ;
39- let new_logger = Arc :: new ( Logger :: builder ( ) . sink ( file_sink) . build ( ) ?) ;
33+ let file_sink = RotatingFileSink :: builder ( )
34+ . base_path ( path)
35+ . rotation_policy ( RotationPolicy :: Daily { hour : 0 , minute : 0 } )
36+ . build_arc ( ) ?;
37+ let new_logger = Logger :: builder ( ) . sink ( file_sink) . build_arc ( ) ?;
4038 spdlog:: set_default_logger ( new_logger) ;
4139
4240 info ! ( "this log will be written to the file `rotating_daily.log`, and the file will be rotated daily at 00:00" ) ;
@@ -47,13 +45,11 @@ fn configure_rotating_daily_file_logger() -> Result<(), Box<dyn std::error::Erro
4745fn configure_rotating_size_file_logger ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
4846 let path = env:: current_exe ( ) ?. with_file_name ( "rotating_size.log" ) ;
4947
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 ( ) ?) ;
48+ let file_sink = RotatingFileSink :: builder ( )
49+ . base_path ( path)
50+ . rotation_policy ( RotationPolicy :: FileSize ( 1024 ) )
51+ . build_arc ( ) ?;
52+ let new_logger = Logger :: builder ( ) . sink ( file_sink) . build_arc ( ) ?;
5753 spdlog:: set_default_logger ( new_logger) ;
5854
5955 info ! ( "this log will be written to the file `rotating_size.log`, and the file will be rotated when its size reaches 1024 bytes" ) ;
@@ -64,13 +60,11 @@ fn configure_rotating_size_file_logger() -> Result<(), Box<dyn std::error::Error
6460fn configure_rotating_hourly_file_logger ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
6561 let path = env:: current_exe ( ) ?. with_file_name ( "rotating_hourly.log" ) ;
6662
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 ( ) ?) ;
63+ let file_sink = RotatingFileSink :: builder ( )
64+ . base_path ( path)
65+ . rotation_policy ( RotationPolicy :: Hourly )
66+ . build_arc ( ) ?;
67+ let new_logger = Logger :: builder ( ) . sink ( file_sink) . build_arc ( ) ?;
7468 spdlog:: set_default_logger ( new_logger) ;
7569
7670 info ! ( "this log will be written to the file `rotating_hourly.log`, and the file will be rotated every hour" ) ;
@@ -81,15 +75,13 @@ fn configure_rotating_hourly_file_logger() -> Result<(), Box<dyn std::error::Err
8175fn configure_rotating_period_file_logger ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
8276 let path = env:: current_exe ( ) ?. with_file_name ( "rotating_period.log" ) ;
8377
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 ( ) ?) ;
78+ let file_sink = RotatingFileSink :: builder ( )
79+ . base_path ( path)
80+ . rotation_policy ( RotationPolicy :: Period ( Duration :: from_secs (
81+ 60 * 90 , // 90 minutes
82+ ) ) )
83+ . build_arc ( ) ?;
84+ let new_logger = Logger :: builder ( ) . sink ( file_sink) . build_arc ( ) ?;
9385 spdlog:: set_default_logger ( new_logger) ;
9486
9587 info ! ( "this log will be written to the file `rotating_period.log`, and the file will be rotated every 1.5 hours" ) ;
0 commit comments