88pub ( crate ) use lightning:: util:: logger:: { Logger as LdkLogger , Record } ;
99pub ( crate ) use lightning:: { log_bytes, log_debug, log_error, log_info, log_trace} ;
1010
11- pub use lightning:: util:: logger:: Level as LdkLevel ;
11+ pub use lightning:: util:: logger:: Level as LogLevel ;
1212
1313use chrono:: Utc ;
1414use log:: { debug, error, info, trace, warn} ;
1515
16+ use std:: fmt:: Debug ;
1617use std:: fs;
1718use std:: io:: Write ;
1819use std:: path:: Path ;
20+ use std:: sync:: Arc ;
1921
2022/// A unit of logging output with Metadata to enable filtering Module_path,
2123/// file, line to inform on log's source.
2224pub struct LogRecord {
2325 /// The verbosity level of the message.
24- pub level : LdkLevel ,
26+ pub level : LogLevel ,
2527 /// The message body.
2628 pub args : String ,
2729 /// The module path of the message.
@@ -43,26 +45,31 @@ impl<'a> From<Record<'a>> for LogRecord {
4345
4446/// LogWriter trait encapsulating the operations required of a
4547/// logger's writer.
46- pub trait LogWriter : Send + Sync {
48+ pub trait LogWriter : Send + Sync + Debug {
4749 /// Log the record.
4850 fn log ( & self , record : LogRecord ) ;
4951}
5052
53+ #[ derive( Debug ) ]
5154pub ( crate ) struct FilesystemLogger {
5255 file_path : String ,
53- level : LdkLevel ,
56+ level : LogLevel ,
5457}
5558
59+ #[ derive( Debug ) ]
5660pub ( crate ) struct LogFacadeLogger {
57- level : LdkLevel ,
61+ level : LogLevel ,
5862}
5963
6064/// Defines a writer for [`Logger`].
65+ #[ derive( Debug ) ]
6166pub ( crate ) enum Writer {
6267 /// Writes logs to the file system.
6368 FileWriter ( FilesystemLogger ) ,
6469 /// Forwards logs to the `log` facade.
6570 LogFacadeWriter ( LogFacadeLogger ) ,
71+ /// Forwards logs to custom writer.
72+ CustomWriter ( Arc < dyn LogWriter + Send + Sync > ) ,
6673}
6774
6875impl LogWriter for Writer {
@@ -92,13 +99,14 @@ impl LogWriter for Writer {
9299 . expect ( "Failed to write to log file" )
93100 } ,
94101 Writer :: LogFacadeWriter ( log_facade_logger) => match log_facade_logger. level {
95- LdkLevel :: Gossip => trace ! ( "{}" , log) ,
96- LdkLevel :: Trace => trace ! ( "{}" , log) ,
97- LdkLevel :: Debug => debug ! ( "{}" , log) ,
98- LdkLevel :: Info => info ! ( "{}" , log) ,
99- LdkLevel :: Warn => warn ! ( "{}" , log) ,
100- LdkLevel :: Error => error ! ( "{}" , log) ,
102+ LogLevel :: Gossip => trace ! ( "{}" , log) ,
103+ LogLevel :: Trace => trace ! ( "{}" , log) ,
104+ LogLevel :: Debug => debug ! ( "{}" , log) ,
105+ LogLevel :: Info => info ! ( "{}" , log) ,
106+ LogLevel :: Warn => warn ! ( "{}" , log) ,
107+ LogLevel :: Error => error ! ( "{}" , log) ,
101108 } ,
109+ Writer :: CustomWriter ( custom_logger) => custom_logger. log ( record) ,
102110 }
103111 }
104112}
@@ -111,7 +119,7 @@ pub(crate) struct Logger {
111119impl Logger {
112120 /// Creates a new logger with a filesystem writer. The parameters to this function
113121 /// are the path to the log file, and the log level.
114- pub fn new_fs_writer ( log_file_path : String , level : LdkLevel ) -> Result < Self , ( ) > {
122+ pub fn new_fs_writer ( log_file_path : String , level : LogLevel ) -> Result < Self , ( ) > {
115123 if let Some ( parent_dir) = Path :: new ( & log_file_path) . parent ( ) {
116124 fs:: create_dir_all ( parent_dir)
117125 . map_err ( |e| eprintln ! ( "ERROR: Failed to create log parent directory: {}" , e) ) ?;
@@ -129,11 +137,15 @@ impl Logger {
129137 Ok ( Self { writer : Writer :: FileWriter ( fs_writer) } )
130138 }
131139
132- pub fn new_log_facade ( level : LdkLevel ) -> Result < Self , ( ) > {
140+ pub fn new_log_facade ( level : LogLevel ) -> Result < Self , ( ) > {
133141 let log_facade_writer = LogFacadeLogger { level } ;
134142
135143 Ok ( Self { writer : Writer :: LogFacadeWriter ( log_facade_writer) } )
136144 }
145+
146+ pub fn new_custom_writer ( log_writer : Arc < dyn LogWriter + Send + Sync > ) -> Result < Self , ( ) > {
147+ Ok ( Self { writer : Writer :: CustomWriter ( log_writer) } )
148+ }
137149}
138150
139151impl LdkLogger for Logger {
0 commit comments