@@ -7,6 +7,7 @@ use lightning::util::ser::Writer;
77use chrono:: Utc ;
88
99use std:: fs;
10+ use std:: os:: unix:: fs:: symlink;
1011use std:: path:: Path ;
1112
1213pub ( crate ) struct FilesystemLogger {
@@ -18,7 +19,35 @@ impl FilesystemLogger {
1819 pub ( crate ) fn new ( file_path : String , level : Level ) -> Self {
1920 if let Some ( parent_dir) = Path :: new ( & file_path) . parent ( ) {
2021 fs:: create_dir_all ( parent_dir) . expect ( "Failed to create log parent directory" ) ;
22+
23+ if let Some ( parent_dir) = Path :: new ( & parent_dir) . parent ( ) {
24+ // make sure the file exists, so that the symlink has something to point to.
25+ fs:: OpenOptions :: new ( )
26+ . create ( true )
27+ . append ( true )
28+ . open ( file_path. clone ( ) )
29+ . expect ( "Failed to open log file" ) ;
30+
31+ // Create a symlink to the current log file, with prior cleanup
32+ let log_file_symlink = parent_dir. join ( "ldk_node.log" ) ;
33+ if log_file_symlink. as_path ( ) . exists ( ) {
34+ if log_file_symlink. as_path ( ) . is_symlink ( ) {
35+ fs:: remove_file ( & log_file_symlink)
36+ . expect ( "Failed to remove an old symlink for the log file" ) ;
37+ } else {
38+ let log_file_legacy =
39+ format ! ( "{}logs/ldk_node.log" , parent_dir. to_string_lossy( ) ) ;
40+ fs:: rename ( & file_path, log_file_legacy)
41+ . expect ( "Failed to rename the old log file" ) ;
42+ }
43+ }
44+ symlink ( & file_path, & log_file_symlink) . expect ( & format ! (
45+ "Failed to create symlink for the log file: {:?}" ,
46+ log_file_symlink
47+ ) ) ;
48+ }
2149 }
50+
2251 Self { file_path, level }
2352 }
2453}
0 commit comments