Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions contrib/ldk-server-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ dir_path = "/tmp/ldk-server/" # Path for LDK and BDK data persis
[log]
level = "Debug" # Log level (Error, Warn, Info, Debug, Trace)
#file = "/tmp/ldk-server/ldk-server.log" # Log file path
log_to_file = false # Enable logging to a file (default: false, logs to stderr only)
#max_size_mb = 50 # Max size of log file before rotation (default: 50MB)
#rotation_interval_hours = 24 # Max age of log file before rotation (default: 24h)
#max_files = 5 # Number of rotated log files to keep (default: 5)

[tls]
#cert_path = "/path/to/tls.crt" # Path to TLS certificate, by default uses dir_path/tls.crt
Expand Down
8 changes: 6 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ Where persistent data is stored. Defaults to `~/.ldk-server/` on Linux and

### `[log]`

Log level and file path. The server reopens the log file on `SIGHUP`, which integrates with
standard `logrotate` setups.
Controls logging behavior. By default, `log_to_file` is `false` and logs are written
to `stdout`/`stderr`.

If `log_to_file` is enabled, the server performs internal rotation and retention
based on `max_size_mb`, `rotation_interval_hours`, and `max_files`. The server still
reopens the log file on `SIGHUP` for compatibility with external tools.

### `[tls]`

Expand Down
16 changes: 10 additions & 6 deletions docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ The server handles `SIGTERM` and `CTRL-C` (SIGINT). On receipt, it:

### Log Rotation

> **Important:** LDK Server does not rotate or truncate its own log file. Without log rotation
> configured, the log file will grow indefinitely and can eventually fill your disk. A full
> disk can prevent the node from persisting channel state, risking fund loss.
By default, LDK Server logs to `stdout`/`stderr`. When running under `systemd` or Docker,
this allows the environment (e.g., `journald`) to handle persistence, rotation, and
compression automatically.

The server reopens its log file on `SIGHUP`. This integrates with standard `logrotate`. Save
the following config to `/etc/logrotate.d/ldk-server` (adjust the log path to match your
setup):
If you enable `log_to_file` in the configuration, LDK Server will automatically rotate
logs when they exceed 50MB or 24 hours (configurable) and keep the last 5 uncompressed
log files.

If you prefer to use system `logrotate` for file logs, the server still reopens its log
file on `SIGHUP`. Save the following config to `/etc/logrotate.d/ldk-server`
(adjust the log path to match your setup):

```
/var/lib/ldk-server/regtest/ldk-server.log {
Expand Down
31 changes: 11 additions & 20 deletions ldk-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use crate::io::persist::{
};
use crate::service::NodeService;
use crate::util::config::{load_config, ArgsConfig, ChainSource};
use crate::util::logger::ServerLogger;
use crate::util::logger::{LogConfig, ServerLogger};
use crate::util::metrics::Metrics;
use crate::util::proto_adapter::{forwarded_payment_to_proto, payment_to_proto};
use crate::util::systemd;
Expand Down Expand Up @@ -121,14 +121,18 @@ fn main() {
std::process::exit(-1);
}

let logger = match ServerLogger::init(config_file.log_level, &log_file_path) {
Ok(logger) => logger,
Err(e) => {
eprintln!("Failed to initialize logger: {e}");
std::process::exit(-1);
},
let log_config = LogConfig {
log_to_file: config_file.log_to_file,
log_max_files: config_file.log_max_files,
log_max_size_bytes: config_file.log_max_size_bytes,
log_rotation_interval_secs: config_file.log_rotation_interval_secs,
};

if let Err(e) = ServerLogger::init(config_file.log_level, &log_file_path, log_config) {
eprintln!("Failed to initialize logger: {e}");
std::process::exit(-1);
}

let api_key = match load_or_generate_api_key(&network_dir) {
Ok(key) => key,
Err(e) => {
Expand Down Expand Up @@ -254,14 +258,6 @@ fn main() {
}

runtime.block_on(async {
// Register SIGHUP handler for log rotation
let mut sighup_stream = match tokio::signal::unix::signal(SignalKind::hangup()) {
Ok(stream) => stream,
Err(e) => {
error!("Failed to register SIGHUP handler: {e}");
std::process::exit(-1);
}
};

let mut sigterm_stream = match tokio::signal::unix::signal(SignalKind::terminate()) {
Ok(stream) => stream,
Expand Down Expand Up @@ -517,11 +513,6 @@ fn main() {
let _ = shutdown_tx.send(true);
break;
}
_ = sighup_stream.recv() => {
if let Err(e) = logger.reopen() {
error!("Failed to reopen log file on SIGHUP: {e}");
}
}
_ = sigterm_stream.recv() => {
info!("Received SIGTERM, shutting down..");
let _ = shutdown_tx.send(true);
Expand Down
98 changes: 98 additions & 0 deletions ldk-server/src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub struct Config {
pub lsps2_service_config: Option<LSPS2ServiceConfig>,
pub log_level: LevelFilter,
pub log_file_path: Option<String>,
pub log_max_size_bytes: usize,
pub log_rotation_interval_secs: u64,
pub log_max_files: usize,
pub log_to_file: bool,
pub pathfinding_scores_source_url: Option<String>,
pub metrics_enabled: bool,
pub poll_metrics_interval: Option<u64>,
Expand Down Expand Up @@ -108,6 +112,10 @@ struct ConfigBuilder {
lsps2: Option<LiquidityConfig>,
log_level: Option<String>,
log_file_path: Option<String>,
log_max_size_mb: Option<u64>,
log_rotation_interval_hours: Option<u64>,
log_max_files: Option<usize>,
log_to_file: Option<bool>,
pathfinding_scores_source_url: Option<String>,
metrics_enabled: Option<bool>,
poll_metrics_interval: Option<u64>,
Expand Down Expand Up @@ -155,6 +163,11 @@ impl ConfigBuilder {
if let Some(log) = toml.log {
self.log_level = log.level.or(self.log_level.clone());
self.log_file_path = log.file.or(self.log_file_path.clone());
self.log_max_size_mb = log.max_size_mb.or(self.log_max_size_mb);
self.log_rotation_interval_hours =
log.rotation_interval_hours.or(self.log_rotation_interval_hours);
self.log_max_files = log.max_files.or(self.log_max_files);
self.log_to_file = log.log_to_file.or(self.log_to_file);
}

if let Some(liquidity) = toml.liquidity {
Expand Down Expand Up @@ -242,6 +255,22 @@ impl ConfigBuilder {
if let Some(tor_proxy_address) = &args.tor_proxy_address {
self.tor_proxy_address = Some(tor_proxy_address.clone());
}

if let Some(log_max_size_mb) = args.log_max_size_mb {
self.log_max_size_mb = Some(log_max_size_mb);
}

if let Some(log_rotation_interval_hours) = args.log_rotation_interval_hours {
self.log_rotation_interval_hours = Some(log_rotation_interval_hours);
}

if let Some(log_max_files) = args.log_max_files {
self.log_max_files = Some(log_max_files);
}

if args.log_to_file {
self.log_to_file = Some(true);
}
}

fn build(self) -> io::Result<Config> {
Expand Down Expand Up @@ -351,6 +380,11 @@ impl ConfigBuilder {
.transpose()?
.unwrap_or(LevelFilter::Debug);

let log_max_size_bytes = self.log_max_size_mb.unwrap_or(50) * 1024 * 1024;
let log_rotation_interval_secs = self.log_rotation_interval_hours.unwrap_or(24) * 60 * 60;
let log_max_files = self.log_max_files.unwrap_or(5);
let log_to_file = self.log_to_file.unwrap_or(false);

let lsps2_client_config = self
.lsps2
.as_ref()
Expand Down Expand Up @@ -416,6 +450,10 @@ impl ConfigBuilder {
lsps2_service_config,
log_level,
log_file_path: self.log_file_path,
log_max_size_bytes: log_max_size_bytes as usize,
log_rotation_interval_secs,
log_max_files,
log_to_file,
pathfinding_scores_source_url,
metrics_enabled,
poll_metrics_interval,
Expand Down Expand Up @@ -483,6 +521,10 @@ struct EsploraConfig {
struct LogConfig {
level: Option<String>,
file: Option<String>,
max_size_mb: Option<u64>,
rotation_interval_hours: Option<u64>,
max_files: Option<usize>,
log_to_file: Option<bool>,
}

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -632,6 +674,34 @@ pub struct ArgsConfig {
)]
node_alias: Option<String>,

#[arg(
long,
env = "LDK_SERVER_LOG_MAX_SIZE_MB",
help = "The maximum size of the log file in MB before rotation. Defaults to 50MB."
)]
log_max_size_mb: Option<u64>,

#[arg(
long,
env = "LDK_SERVER_LOG_ROTATION_INTERVAL_HOURS",
help = "The maximum age of the log file in hours before rotation. Defaults to 24h."
)]
log_rotation_interval_hours: Option<u64>,

#[arg(
long,
env = "LDK_SERVER_LOG_MAX_FILES",
help = "The maximum number of rotated log files to keep. Defaults to 5."
)]
log_max_files: Option<usize>,

#[arg(
long,
env = "LDK_SERVER_LOG_TO_FILE",
help = "The option to enable logging to a file. If not set, logging to file is disabled."
)]
log_to_file: bool,

#[arg(
long,
env = "LDK_SERVER_BITCOIND_RPC_ADDRESS",
Expand Down Expand Up @@ -795,6 +865,10 @@ mod tests {
[log]
level = "Trace"
file = "/var/log/ldk-server.log"
max_size_mb = 50
rotation_interval_hours = 24
max_files = 5
log_to_file = false

[bitcoind]
rpc_address = "127.0.0.1:8332"
Expand Down Expand Up @@ -840,6 +914,10 @@ mod tests {
metrics_username: None,
metrics_password: None,
tor_proxy_address: None,
log_to_file: false,
log_max_size_mb: Some(50),
log_rotation_interval_hours: Some(24),
log_max_files: Some(5),
}
}

Expand All @@ -861,6 +939,10 @@ mod tests {
metrics_username: None,
metrics_password: None,
tor_proxy_address: None,
log_to_file: false,
log_max_size_mb: None,
log_rotation_interval_hours: None,
log_max_files: None,
}
}

Expand Down Expand Up @@ -928,6 +1010,10 @@ mod tests {
}),
log_level: LevelFilter::Trace,
log_file_path: Some("/var/log/ldk-server.log".to_string()),
log_max_size_bytes: 50 * 1024 * 1024,
log_rotation_interval_secs: 24 * 60 * 60,
log_max_files: 5,
log_to_file: false,
pathfinding_scores_source_url: None,
metrics_enabled: false,
poll_metrics_interval: None,
Expand Down Expand Up @@ -1241,6 +1327,10 @@ mod tests {
metrics_username: None,
metrics_password: None,
tor_config: None,
log_max_size_bytes: 50 * 1024 * 1024,
log_rotation_interval_secs: 24 * 60 * 60,
log_max_files: 5,
log_to_file: false,
};

assert_eq!(config.listening_addrs, expected.listening_addrs);
Expand All @@ -1254,6 +1344,10 @@ mod tests {
assert_eq!(config.pathfinding_scores_source_url, expected.pathfinding_scores_source_url);
assert_eq!(config.metrics_enabled, expected.metrics_enabled);
assert_eq!(config.tor_config, expected.tor_config);
assert_eq!(config.log_max_size_bytes, expected.log_max_size_bytes);
assert_eq!(config.log_rotation_interval_secs, expected.log_rotation_interval_secs);
assert_eq!(config.log_max_files, expected.log_max_files);
assert_eq!(config.log_to_file, expected.log_to_file);
}

#[test]
Expand Down Expand Up @@ -1350,6 +1444,10 @@ mod tests {
tor_config: Some(TorConfig {
proxy_address: SocketAddress::from_str("127.0.0.1:9050").unwrap(),
}),
log_max_size_bytes: 50 * 1024 * 1024,
log_rotation_interval_secs: 24 * 60 * 60,
log_max_files: 5,
log_to_file: false,
};

assert_eq!(config.listening_addrs, expected.listening_addrs);
Expand Down
Loading