changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/lib/logger/src/lib.rs

changeset 213: 4f49127c9048
parent: 611060cba9f0
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 23 Feb 2024 21:27:25 -0500
permissions: -rw-r--r--
description: alik updates
1 //! logger library
2 
3 pub use flexi_logger::{
4  with_thread, AdaptiveFormat, Duplicate, FileSpec, Logger, LoggerHandle,
5 };
6 
7 pub use log::{
8  self, debug, error, info, trace, warn, Level, LevelFilter, Metadata, Record,
9 };
10 
11 pub use tracing;
12 pub use tracing_subscriber;
13 
14 mod err;
15 pub use err::{Error, Result};
16 
17 #[cfg(test)]
18 mod tests;
19 
20 /// initialize a simple logger
21 pub fn simple() -> Result<()> {
22  log::set_logger(&SimpleLogger)
23  .map(|()| log::set_max_level(LevelFilter::Trace))?;
24  Ok(())
25 }
26 
27 /// a simple logger
28 struct SimpleLogger;
29 
30 impl log::Log for SimpleLogger {
31  fn enabled(&self, metadata: &Metadata) -> bool {
32  metadata.level() <= Level::Info
33  }
34 
35  fn log(&self, record: &Record) {
36  if self.enabled(record.metadata()) {
37  println!("{} - {}", record.level(), record.args());
38  }
39  }
40 
41  fn flush(&self) {}
42 }
43 
44 /// Initialize a `Logger` with a specified logging level
45 pub fn flexi(level: &str) -> Result<()> {
46  Logger::try_with_env_or_str(level)?
47  .format(flexi_logger::colored_default_format)
48  .set_palette("196;208;50;7;8".to_string())
49  .adaptive_format_for_stderr(AdaptiveFormat::Detailed)
50  .adaptive_format_for_stdout(AdaptiveFormat::Default)
51  .log_to_stdout()
52  .start()?;
53  Ok(())
54 }
55 
56 /// Initialize file Logger
57 pub fn file(env: &str, log_path: &str, log_name: &str) -> Result<LoggerHandle> {
58  Ok(
59  Logger::try_with_env_or_str(env)?
60  .log_to_file(
61  FileSpec::default()
62  .suppress_timestamp()
63  .directory(log_path)
64  .basename(log_name)
65  .suffix("log"),
66  )
67  .format_for_files(with_thread)
68  .append()
69  .duplicate_to_stdout(Duplicate::Error)
70  .start()?,
71  )
72 }