Skip to content

Commit ec42ae7

Browse files
committed
feat(logger): add & impl LogWriter for Writer
1 parent c920e55 commit ec42ae7

File tree

3 files changed

+76
-28
lines changed

3 files changed

+76
-28
lines changed

bindings/ldk_node.udl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,18 @@ dictionary NodeAnnouncementInfo {
558558
sequence<SocketAddress> addresses;
559559
};
560560

561+
dictionary LogRecord {
562+
LdkLevel level;
563+
string args;
564+
string module_path;
565+
u32 line;
566+
};
567+
568+
[Trait]
569+
interface LogWriter {
570+
void log(LogRecord record);
571+
};
572+
561573
[Custom]
562574
typedef string Txid;
563575

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub use event::Event;
111111
pub use io::utils::generate_entropy_mnemonic;
112112

113113
pub use config::FilesystemLoggerConfig;
114-
pub use logger::LdkLevel;
114+
pub use logger::{LdkLevel, LogRecord, LogWriter};
115115

116116
#[cfg(feature = "uniffi")]
117117
use uniffi_types::*;

src/logger.rs

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,48 @@
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};
99
pub(crate) use lightning::{log_bytes, log_debug, log_error, log_info, log_trace};
1010

1111
pub use lightning::util::logger::Level as LdkLevel;
12-
use lightning::util::logger::Record;
1312

1413
use chrono::Utc;
1514

1615
use std::fs;
1716
use std::io::Write;
1817
use 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+
2050
pub(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+
3191
pub(crate) struct Logger {
3292
/// Specifies the logger's writer.
3393
writer: Writer,
@@ -57,30 +117,6 @@ impl Logger {
57117

58118
impl 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

Comments
 (0)