wip: impelement file logger
This commit is contained in:
parent
5dac2c60aa
commit
5cf2b0c568
2 changed files with 34 additions and 7 deletions
|
|
@ -97,8 +97,8 @@ Options:
|
||||||
If you set `--log-dir=<log_dir>`, the log files are created in the specified directory. Otherwise, the log is printed to stdout.
|
If you set `--log-dir=<log_dir>`, the log files are created in the specified directory. Otherwise, the log is printed to stdout.
|
||||||
|
|
||||||
- `${log_dir}/access.log` for access log
|
- `${log_dir}/access.log` for access log
|
||||||
- `${log_dir}/error.log` for error log
|
<!-- - `${log_dir}/error.log` for error log -->
|
||||||
- `${log_dir}/rpxy.log` for system log
|
- `${log_dir}/rpxy.log` for system and error log
|
||||||
|
|
||||||
That's all!
|
That's all!
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,30 @@ pub use tracing::{debug, error, info, warn};
|
||||||
|
|
||||||
/// Initialize the logger with the RUST_LOG environment variable.
|
/// Initialize the logger with the RUST_LOG environment variable.
|
||||||
pub fn init_logger(log_dir_path: Option<&str>) {
|
pub fn init_logger(log_dir_path: Option<&str>) {
|
||||||
if log_dir_path.is_some() {
|
|
||||||
// TODO:
|
|
||||||
println!("Activate logging to files")
|
|
||||||
}
|
|
||||||
|
|
||||||
let level_string = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
|
let level_string = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
|
||||||
let level = tracing::Level::from_str(level_string.as_str()).unwrap_or(tracing::Level::INFO);
|
let level = tracing::Level::from_str(level_string.as_str()).unwrap_or(tracing::Level::INFO);
|
||||||
|
|
||||||
|
match log_dir_path {
|
||||||
|
None => {
|
||||||
|
// log to stdout
|
||||||
|
init_stdio_logger(level);
|
||||||
|
}
|
||||||
|
Some(log_dir_path) => {
|
||||||
|
// log to files
|
||||||
|
println!("Activate logging to files: {log_dir_path}");
|
||||||
|
init_file_logger(level, log_dir_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// file logging
|
||||||
|
fn init_file_logger(level: tracing::Level, log_dir_path: &str) {
|
||||||
|
// TODO: implement
|
||||||
|
init_stdio_logger(level);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// stdio logging
|
||||||
|
fn init_stdio_logger(level: tracing::Level) {
|
||||||
// This limits the logger to emits only this crate with any level above RUST_LOG, for included crates it will emit only ERROR (in prod)/INFO (in dev) or above level.
|
// This limits the logger to emits only this crate with any level above RUST_LOG, for included crates it will emit only ERROR (in prod)/INFO (in dev) or above level.
|
||||||
let stdio_layer = fmt::layer().with_level(true).with_thread_ids(false);
|
let stdio_layer = fmt::layer().with_level(true).with_thread_ids(false);
|
||||||
if level <= tracing::Level::INFO {
|
if level <= tracing::Level::INFO {
|
||||||
|
|
@ -47,3 +63,14 @@ pub fn init_logger(log_dir_path: Option<&str>) {
|
||||||
tracing_subscriber::registry().with(stdio_layer).init();
|
tracing_subscriber::registry().with(stdio_layer).init();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
/// Create a file for logging
|
||||||
|
fn open_log_file(path: &str) -> std::fs::File {
|
||||||
|
// crate a file if it does not exist
|
||||||
|
std::fs::OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.append(true)
|
||||||
|
.open(path)
|
||||||
|
.expect("Failed to open the log file")
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue