-
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.
- Loading branch information
Showing
7 changed files
with
163 additions
and
17 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
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,6 @@ | ||
|
||
#[test] | ||
fn test_get_version() { | ||
let version = crate::get_version().unwrap(); | ||
println!("{}", version); | ||
} |
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,3 @@ | ||
pub mod rs_box_log; | ||
mod rs_box_log_tests; | ||
|
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,122 @@ | ||
use std::fs; | ||
use lazy_static::lazy_static; | ||
use log::{info, warn, error, debug, trace, LevelFilter}; | ||
use log4rs::{ | ||
append::{console::ConsoleAppender, file::FileAppender}, | ||
config::{Appender, Config, Root}, | ||
encode::pattern::PatternEncoder, | ||
}; | ||
use std::sync::Mutex; | ||
use std::path::Path; | ||
|
||
use std::sync::Once; | ||
static INIT: Once = Once::new(); | ||
|
||
|
||
lazy_static! { | ||
// static ref LOGGER: Mutex<LoggerManager> = Mutex::new(LoggerManager::new()); | ||
static ref GLOBAL_LOGGER: Mutex<Option<LoggerManager>> = Mutex::new(None); | ||
} | ||
|
||
struct LoggerManager { | ||
enable_file_log: bool, | ||
log_level: LevelFilter, | ||
log_dir: Option<String>, | ||
} | ||
|
||
fn default_log_path() -> String { | ||
if cfg!(target_os = "linux") { | ||
"/var/log/my_app/run.log".to_string() | ||
} else { | ||
"./logs/run.log".to_string() | ||
} | ||
} | ||
|
||
impl LoggerManager { | ||
pub fn new(enable_file_log: bool, log_level: LevelFilter, log_dir: Option<String>) -> Self { | ||
let mut manager = Self { | ||
enable_file_log, | ||
log_level, | ||
log_dir, | ||
}; | ||
manager.setup(); // Configure logging according to the settings | ||
manager | ||
} | ||
|
||
fn setup(&mut self) { | ||
let mut config_builder = Config::builder(); | ||
let mut root_builder = Root::builder(); | ||
|
||
if self.enable_file_log { | ||
// Configure logging to file | ||
let log_file_path = self.log_dir.as_ref() | ||
.map(|dir| { | ||
if !dir.is_empty() { | ||
format!("{}/run.log", dir) | ||
} else { | ||
default_log_path() | ||
} | ||
}) | ||
.unwrap_or_else(default_log_path); | ||
|
||
if let Some(parent) = Path::new(&log_file_path).parent() { | ||
fs::create_dir_all(parent).expect("Failed to create log directory"); | ||
} | ||
|
||
let logfile = FileAppender::builder() | ||
.encoder(Box::new(PatternEncoder::new("{d(%Y-%m-%d %H:%M:%S)} {l} - {m}\n"))) | ||
.build(&log_file_path) | ||
.expect("Failed to create file appender"); | ||
|
||
config_builder = config_builder.appender(Appender::builder().build("logfile", Box::new(logfile))); | ||
root_builder = root_builder.appender("logfile"); | ||
} else { | ||
// Configure logging to stdout only | ||
let stdout_appender = ConsoleAppender::builder() | ||
.encoder(Box::new(PatternEncoder::new("{d} - {l} - {m}\n"))) | ||
.build(); | ||
|
||
config_builder = config_builder.appender(Appender::builder().build("stdout", Box::new(stdout_appender))); | ||
root_builder = root_builder.appender("stdout"); | ||
} | ||
|
||
// Build and initialize the logging configuration outside the if/else block | ||
let root_config = root_builder.build(self.log_level); | ||
let config = config_builder.build(root_config) | ||
.expect("Failed to build logger configuration"); | ||
|
||
// Initialize logger configuration | ||
if let Err(err) = log4rs::init_config(config) { | ||
eprintln!("Logger initialization failed: {}", err); | ||
} | ||
} | ||
} | ||
|
||
pub fn setup_log_tools(enable_file_log: bool, log_level: LevelFilter, log_dir: Option<String>) { | ||
let mut global_logger = GLOBAL_LOGGER.lock().unwrap(); | ||
*global_logger = Some(LoggerManager::new(enable_file_log, log_level, log_dir)); | ||
} | ||
pub fn log_info(message: &str) { | ||
info!("{}", message); | ||
} | ||
|
||
pub fn log_warning(message: &str) { | ||
warn!("{}", message); | ||
} | ||
|
||
pub fn log_error(message: &str) { | ||
error!("{}", message); | ||
} | ||
|
||
pub fn log_debug(message: &str) { | ||
debug!("{}", message); | ||
} | ||
|
||
/// 打印[trace]级别日志。 | ||
/// # 示例 | ||
/// ``` | ||
/// crate::rs_box::rs_box_log::rs_box_log::log_trace("trace log message"); | ||
/// ``` | ||
pub fn log_trace(message: &str) { | ||
trace!("{}", message); | ||
} |
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,22 @@ | ||
use log::{LevelFilter}; | ||
use crate::rs_box_log::rs_box_log; | ||
|
||
#[test] | ||
fn test_logs_with_write_logfile() { | ||
rs_box_log::setup_log_tools(true, LevelFilter::Trace, Some("".to_string())); | ||
rs_box_log::log_info("This is an info message"); | ||
rs_box_log::log_error("This is an error message"); | ||
rs_box_log::log_warning("This is an warning message"); | ||
rs_box_log::log_debug("This is an debug message"); | ||
rs_box_log::log_trace("This is an trace message"); | ||
} | ||
|
||
#[test] | ||
fn test_logs_with_terminal_show() { | ||
rs_box_log::setup_log_tools(false, LevelFilter::Trace, Some("".to_string())); | ||
rs_box_log::log_info("This is an info message"); | ||
rs_box_log::log_error("This is an error message"); | ||
rs_box_log::log_warning("This is an warning message"); | ||
rs_box_log::log_debug("This is an debug message"); | ||
rs_box_log::log_trace("This is an trace message"); | ||
} |
This file was deleted.
Oops, something went wrong.