当前位置:网站首页>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
边栏推荐
- Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
- 【错误】加载h5权重出错AttributeError: ‘str‘ object has no attribute ‘decode‘
- "Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.2 data preprocessing_ Learning thinking and exercise answers
- 很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
- 如何用Diffusion models做interpolation插值任务?——原理解析和代码实战
- 金融业数字化转型中,业务和技术融合需要经历三个阶段
- Many friends don't know the underlying principle of ORM framework very well. No, glacier will take you 10 minutes to hand roll a minimalist ORM framework (collect it quickly)
- node js 保持长连接
- "Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.1 data operation_ Learning thinking and exercise answers
- Kwai applet guaranteed payment PHP source code packaging
猜你喜欢
咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
Introduction to ADB tools
微信小程序uniapp页面无法跳转:“navigateTo:fail can not navigateTo a tabbar page“
Introduction à l'outil nmap et aux commandes communes
科普 | 什么是灵魂绑定代币SBT?有何价值?
Master go game through deep neural network and tree search
nmap工具介紹及常用命令
神经网络与深度学习-5- 感知机-PyTorch
Introduction to grpc for cloud native application development
Nacos microservice gateway component +swagger2 interface generation
随机推荐
静态路由配置全面详解,静态路由快速入门指南
喜欢测特曼的阿洛
Random walk reasoning and learning in large-scale knowledge base
Height of life
For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
[reinforcement learning medical] deep reinforcement learning for clinical decision support: a brief overview
电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
Master go game through deep neural network and tree search
Semantic segmentation | learning record (5) FCN network structure officially implemented by pytoch
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
需要思考的地方
软件测试笔试题你会吗?
VIM use
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.1 data operation_ Learning thinking and exercise answers
C语言-Cmake-CMakeLists.txt教程
How mysql/mariadb generates core files
Introduction à l'outil nmap et aux commandes communes
See how names are added to namespace STD from cmath file
adb工具介绍
Ml backward propagation