WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 6ce7593

Browse files
committed
Use LineWriter to write logs to the log file
`BufWriter` only flushes its buffer when it reaches capacity, currently at 8KiB. `LineWriter` flushes on every '\n', matching the experience expected when calling `tail -f` on the log file.
1 parent e9f047b commit 6ce7593

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

ldk-server/src/util/logger.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use log::{Level, LevelFilter, Log, Metadata, Record};
1111
use std::fs::{self, File, OpenOptions};
12-
use std::io::{self, BufWriter, Write};
12+
use std::io::{self, LineWriter, Write};
1313
use std::path::{Path, PathBuf};
1414
use std::sync::{Arc, Mutex};
1515

@@ -29,7 +29,7 @@ pub struct ServerLogger {
2929
/// The maximum log level to display
3030
level: LevelFilter,
3131
/// The file to write logs to, protected by a mutex for thread-safe access
32-
file: Mutex<BufWriter<File>>,
32+
file: Mutex<LineWriter<File>>,
3333
/// Path to the log file for reopening on SIGHUP
3434
log_file_path: PathBuf,
3535
}
@@ -155,9 +155,9 @@ fn format_level(level: Level) -> &'static str {
155155
}
156156
}
157157

158-
fn open_log_file(log_file_path: &Path) -> Result<BufWriter<File>, io::Error> {
158+
fn open_log_file(log_file_path: &Path) -> Result<LineWriter<File>, io::Error> {
159159
let file = OpenOptions::new().create(true).append(true).open(log_file_path)?;
160-
Ok(BufWriter::new(file))
160+
Ok(LineWriter::new(file))
161161
}
162162

163163
/// Wrapper to allow Arc<ServerLogger> to implement Log trait

0 commit comments

Comments
 (0)