当前位置:网站首页>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】
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 ◄
Click to read the original text , View disk Rust Series content
边栏推荐
- Introduction à l'outil nmap et aux commandes communes
- How mysql/mariadb generates core files
- 咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
- 生命的高度
- node js 保持长连接
- leetcode 869. Reordered Power of 2 | 869. Reorder to a power of 2 (state compression)
- Uniapp one click Copy function effect demo (finishing)
- burpsuite
- [recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning
- 力扣4_412. Fizz Buzz
猜你喜欢
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
系统测试的类型有哪些,我给你介绍
OpenGL/WebGL着色器开发入门指南
What are the types of system tests? Let me introduce them to you
Can you write the software test questions?
JVM memory and garbage collection -4-string
科普 | 什么是灵魂绑定代币SBT?有何价值?
静态路由配置全面详解,静态路由快速入门指南
Kwai applet guaranteed payment PHP source code packaging
Introduction à l'outil nmap et aux commandes communes
随机推荐
[knowledge map paper] r2d2: knowledge map reasoning based on debate dynamics
Analysis ideas after discovering that the on duty equipment is attacked
Nacos microservice gateway component +swagger2 interface generation
Redisson分布式锁解锁异常
EMQX 5.0 发布:单集群支持 1 亿 MQTT 连接的开源物联网消息服务器
adb工具介绍
CorelDRAW2022下载安装电脑系统要求技术规格
metasploit
Installing and using mpi4py
BizDevOps与DevOps的关系
Uniapp one click Copy function effect demo (finishing)
电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
C language -cmake cmakelists Txt tutorial
burpsuite
Semantic segmentation | learning record (4) expansion convolution (void convolution)
Key points of data link layer and network layer protocol
喜欢测特曼的阿洛
Little knowledge about TXE and TC flag bits
ClickHouse原理解析与应用实践》读书笔记(8)
VIM use