当前位置:网站首页>Disk rust -- add a log to the program

Disk rust -- add a log to the program

2022-07-08 02:12:00 JD technology developer

fcf0be62caef3e8c9bcdb03f6d210602.gif

Logs are an important part of applications . Both server and client programs need logs as error output or business records . In this article , We combine [log4rs](https://github.com/estk/log4rs) Chat rust How to use logs in programs .

[log4rs](https://github.com/estk/log4rs) similar java In ecology log4j, The way of use is also very similar

log4rs Basic concepts in

log4rs The functional components of are also appender and logger constitute .

· appender

  Be responsible for adding logs to the specified file or console  

· logger

  Contains multiple appender , For example, a log should be both output to the console and persisted to the log file , You can go to logger Bind at the same time ConsoleAppender and FileAppender 

log4rs Examples of use

· Sample description

  We need to record system logs and business logs in the project , Record in logs/sys.log and logs/business.log

· Definition appender and logger And initialization

  Code location src/logger/logger.rs

let sys_file = FileAppender::builder()
       .encoder(Box::new(PatternEncoder::new("{d} - {m}{n}")))
       .build("logs/sys.log")
       .unwrap();
   let business_file = FileAppender::builder()
       .encoder(Box::new(PatternEncoder::new("{d} - {m}{n}")))
       .build("logs/business.log")
       .unwrap();

   let stdout = ConsoleAppender::builder().build();

   let config = Config::builder()
       .appender(Appender::builder().build("stdout", Box::new(stdout)))
       .appender(Appender::builder().build("sys", Box::new(sys_file)))
       .appender(Appender::builder().build("business", Box::new(business_file)))
       .logger(
           Logger::builder()
               .appender("sys")
               .build("syslog", LevelFilter::Info),
       )
       .logger(
           Logger::builder()
               .appender("business")
               .build("businesslog", LevelFilter::Info),
       )
       .build(
           Root::builder()
               .appender("stdout")
               .appender("file_out")
               .build(LevelFilter::Info),
       )
       .unwrap();

   let _ = log4rs::init_config(config).unwrap();

Slide left and right to see the code

The code defines sys_file and business_file Two FileAppender Respectively used for image sys.log and business.log Append log in .

config Two are defined in logger Bind separately sys appender and business appender.

Finally through init_config initialization log4rs.

  · Output logs in the program

    ·  Definition uselog Command and two subcommands , Input separately sys Journal and business journal .

      Code location src/cmd/cmdusedifflogger.rs

pub fn new_use_sys_log_cmd() -> Command<'static> {
     clap::Command::new("syslog").about("append to syslog")
 }
 
 pub fn new_use_business_log_cmd() -> Command<'static> {
     clap::Command::new("businesslog").about("append to business log")
 }

Slide left and right to see the code

· Parse the command and output the log

      Code location src/cmd/rootcmd.rs

if let Some(ref log) = matches.subcommand_matches("uselog") {
      println!("use log");
      if let Some(_) = log.subcommand_matches("syslog") {
          log::info!(target:"syslog","Input sys log");
      }  
      if let Some(_) = log.subcommand_matches("businesslog") {
          log::info!(target:"businesslog","Input business log");
      }
  }
 Slide left and right to see the code 

When the output , adopt target To distinguish the output to different logger.

The code of this article is github Address :https://github.com/jiashiwen/interactcli-rs

See you next time

-End-

► Learn more about ◄

46dba2b80cc39a5eec17361bb5930325.png

485a89535cbacb145ed6aa6058dff903.png

f6dcdd4129c9d7509bacc9e10dacc8dc.png

169f0ea2ee5b236f8c9dad89f6dc7103.png

Click to read the original text , View disk Rust Series content

原网站

版权声明
本文为[JD technology developer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/189/202207080043380649.html