forked from tracel-ai/burn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configurable application logger to learner builder (tracel-ai#1774)
* refactor: add TracingSubscriberLogger trait and FileTracingSubscriberLogger struct * Remove unused log module and renames, fmt * Renamed tracing subscriber logger * renamed to application logger installer * book learner configuration update update * fix typo * unused import
- Loading branch information
Showing
6 changed files
with
89 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::path::Path; | ||
use tracing_core::{Level, LevelFilter}; | ||
use tracing_subscriber::filter::filter_fn; | ||
use tracing_subscriber::prelude::*; | ||
use tracing_subscriber::{registry, Layer}; | ||
|
||
/// This trait is used to install an application logger. | ||
pub trait ApplicationLoggerInstaller { | ||
/// Install the application logger. | ||
fn install(&self) -> Result<(), String>; | ||
} | ||
|
||
/// This struct is used to install a local file application logger to output logs to a given file path. | ||
pub struct FileApplicationLoggerInstaller { | ||
path: String, | ||
} | ||
|
||
impl FileApplicationLoggerInstaller { | ||
/// Create a new file application logger. | ||
pub fn new(path: &str) -> Self { | ||
Self { | ||
path: path.to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl ApplicationLoggerInstaller for FileApplicationLoggerInstaller { | ||
fn install(&self) -> Result<(), String> { | ||
let path = Path::new(&self.path); | ||
let writer = tracing_appender::rolling::never( | ||
path.parent().unwrap_or_else(|| Path::new(".")), | ||
path.file_name() | ||
.unwrap_or_else(|| panic!("The path '{}' to point to a file.", self.path)), | ||
); | ||
let layer = tracing_subscriber::fmt::layer() | ||
.with_ansi(false) | ||
.with_writer(writer) | ||
.with_filter(LevelFilter::INFO) | ||
.with_filter(filter_fn(|m| { | ||
if let Some(path) = m.module_path() { | ||
// The wgpu crate is logging too much, so we skip `info` level. | ||
if path.starts_with("wgpu") && *m.level() >= Level::INFO { | ||
return false; | ||
} | ||
} | ||
true | ||
})); | ||
|
||
if registry().with(layer).try_init().is_err() { | ||
return Err("Failed to install the file logger.".to_string()); | ||
} | ||
|
||
let hook = std::panic::take_hook(); | ||
let file_path: String = self.path.to_owned(); | ||
|
||
std::panic::set_hook(Box::new(move |info| { | ||
log::error!("PANIC => {}", info.to_string()); | ||
eprintln!( | ||
"=== PANIC ===\nA fatal error happened, you can check the experiment logs here => \ | ||
'{file_path}'\n=============" | ||
); | ||
hook(info); | ||
})); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters