Skip to content

Commit 6d8c65e

Browse files
committed
feat(logger): add & impl LogWriter for Writer
1 parent a88dc5c commit 6d8c65e

File tree

5 files changed

+76
-32
lines changed

5 files changed

+76
-32
lines changed

bindings/kotlin/ldk-node-android/lib/src/androidTest/kotlin/org/lightningdevkit/ldknode/AndroidLibTest.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ class AndroidLibTest {
2828
config1.storageDirPath = tmpDir1
2929
config1.listeningAddresses = listOf(listenAddress1)
3030
config1.network = Network.REGTEST
31-
config1.logLevel = LogLevel.TRACE
3231

3332
val config2 = defaultConfig()
3433
config2.storageDirPath = tmpDir2
3534
config2.listeningAddresses = listOf(listenAddress2)
3635
config2.network = Network.REGTEST
37-
config2.logLevel = LogLevel.TRACE
3836

3937
val builder1 = Builder.fromConfig(config1)
4038
val builder2 = Builder.fromConfig(config2)

bindings/kotlin/ldk-node-jvm/lib/src/test/kotlin/org/lightningdevkit/ldknode/LibraryTest.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,13 @@ class LibraryTest {
118118
config1.storageDirPath = tmpDir1
119119
config1.listeningAddresses = listOf(listenAddress1)
120120
config1.network = Network.REGTEST
121-
config1.logLevel = LogLevel.TRACE
122121

123122
println("Config 1: $config1")
124123

125124
val config2 = defaultConfig()
126125
config2.storageDirPath = tmpDir2
127126
config2.listeningAddresses = listOf(listenAddress2)
128127
config2.network = Network.REGTEST
129-
config2.logLevel = LogLevel.TRACE
130128
println("Config 2: $config2")
131129

132130
val builder1 = Builder.fromConfig(config1)

bindings/ldk_node.udl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,18 @@ dictionary NodeAnnouncementInfo {
588588
sequence<SocketAddress> addresses;
589589
};
590590

591+
dictionary LogRecord {
592+
LdkLevel level;
593+
string args;
594+
string module_path;
595+
u32 line;
596+
};
597+
598+
[Trait]
599+
interface LogWriter {
600+
void log(LogRecord record);
601+
};
602+
591603
[Custom]
592604
typedef string Txid;
593605

src/lib.rs

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

114114
pub use config::FilesystemLoggerConfig;
115-
pub use logger::LdkLevel;
115+
pub use logger::{LdkLevel, LogRecord, LogWriter};
116116

117117
#[cfg(feature = "uniffi")]
118118
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)