55// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66// accordance with one or both of these licenses.
77
8- pub ( crate ) use lightning:: util:: logger:: Logger as LdkLogger ;
8+ pub ( crate ) use lightning:: util:: logger:: { Logger as LdkLogger , Record } ;
99pub ( crate ) use lightning:: { log_bytes, log_debug, log_error, log_info, log_trace} ;
1010
1111pub use lightning:: util:: logger:: Level as LdkLevel ;
12- use lightning:: util:: logger:: Record ;
1312
1413use chrono:: Utc ;
1514
1615use std:: fs;
1716use std:: io:: Write ;
1817use std:: path:: Path ;
1918
19+ /// A unit of logging output with Metadata to enable filtering Module_path,
20+ /// file, line to inform on log's source.
21+ pub struct LogRecord {
22+ /// The verbosity level of the message.
23+ pub level : LdkLevel ,
24+ /// The message body.
25+ pub args : String ,
26+ /// The module path of the message.
27+ pub module_path : String ,
28+ /// The line containing the message.
29+ pub line : u32 ,
30+ }
31+
32+ impl < ' a > From < Record < ' a > > for LogRecord {
33+ fn from ( record : Record ) -> Self {
34+ Self {
35+ level : record. level ,
36+ args : record. args . to_string ( ) ,
37+ module_path : record. module_path . to_string ( ) ,
38+ line : record. line ,
39+ }
40+ }
41+ }
42+
43+ /// LogWriter trait encapsulating the operations required of a
44+ /// logger's writer.
45+ pub trait LogWriter : Send + Sync {
46+ /// Log the record.
47+ fn log ( & self , record : LogRecord ) ;
48+ }
49+
2050pub ( crate ) struct FilesystemLogger {
2151 file_path : String ,
2252 level : LdkLevel ,
@@ -28,6 +58,36 @@ pub(crate) enum Writer {
2858 FileWriter ( FilesystemLogger ) ,
2959}
3060
61+ impl LogWriter for Writer {
62+ fn log ( & self , record : LogRecord ) {
63+ let raw_log = record. args . to_string ( ) ;
64+ let log = format ! (
65+ "{} {:<5} [{}:{}] {}\n " ,
66+ Utc :: now( ) . format( "%Y-%m-%d %H:%M:%S" ) ,
67+ record. level. to_string( ) ,
68+ record. module_path,
69+ record. line,
70+ raw_log
71+ ) ;
72+
73+ match self {
74+ Writer :: FileWriter ( fs_logger) => {
75+ if record. level < fs_logger. level {
76+ return ;
77+ }
78+
79+ fs:: OpenOptions :: new ( )
80+ . create ( true )
81+ . append ( true )
82+ . open ( fs_logger. file_path . clone ( ) )
83+ . expect ( "Failed to open log file" )
84+ . write_all ( log. as_bytes ( ) )
85+ . expect ( "Failed to write to log file" )
86+ } ,
87+ }
88+ }
89+ }
90+
3191pub ( crate ) struct Logger {
3292 /// Specifies the logger's writer.
3393 writer : Writer ,
@@ -57,30 +117,6 @@ impl Logger {
57117
58118impl LdkLogger for Logger {
59119 fn log ( & self , record : Record ) {
60- let raw_log = record. args . to_string ( ) ;
61- let log = format ! (
62- "{} {:<5} [{}:{}] {}\n " ,
63- Utc :: now( ) . format( "%Y-%m-%d %H:%M:%S" ) ,
64- record. level. to_string( ) ,
65- record. module_path,
66- record. line,
67- raw_log
68- ) ;
69-
70- match & self . writer {
71- Writer :: FileWriter ( fs_logger) => {
72- if record. level < fs_logger. level {
73- return ;
74- }
75-
76- fs:: OpenOptions :: new ( )
77- . create ( true )
78- . append ( true )
79- . open ( fs_logger. file_path . clone ( ) )
80- . expect ( "Failed to open log file" )
81- . write_all ( log. as_bytes ( ) )
82- . expect ( "Failed to write to log file" )
83- } ,
84- }
120+ self . writer . log ( record. into ( ) ) ;
85121 }
86122}
0 commit comments