当前位置:网站首页>文盘Rust -- 给程序加个日志
文盘Rust -- 给程序加个日志
2022-07-08 00:43:00 【京东科技开发者】
日志是应用程序的重要组成部分。无论是服务端程序还是客户端程序都需要日志做为错误输出或者业务记录。在这篇文章中,我们结合[log4rs](https://github.com/estk/log4rs)聊聊rust 程序中如何使用日志。
[log4rs](https://github.com/estk/log4rs)类似java生态中的log4j,使用方式也很相似
log4rs中的基本概念
log4rs 的功能组件也由 appender 和 logger构成。
· appender
负责向指定文件或控制台追加日志
· logger
包含多个 appender ,比如一条日志既要输出到控制台也要持久化到日志文件中,就可以在logger中同时绑定 ConsoleAppender 和 FileAppender
log4rs 使用示例
· 示例描述
我们需要在工程中记录系统日志和业务日志,分别记录在logs/sys.log 和 logs/business.log
· 定义 appender 和 logger 并初始化
代码位置 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();
左右滑动查看代码
代码中定义了 sys_file 和 business_file 两个FileAppender 分别用于像sys.log 和 business.log中追加日志。
config 中定义了两个logger 分别绑定 sys appender 和 business appender。
最后通过 init_config 初始化 log4rs。
· 在程序中输出日志
· 定义 uselog 命令及两个子命令,分别输入sys 日志和 business 日志。
代码位置 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")
}
左右滑动查看代码
· 解析命令并输出日志
代码位置 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");
}
}
左右滑动查看代码
输出时,通过 target 来区分输出到不同的logger。
本文代码的github地址:https://github.com/jiashiwen/interactcli-rs
下期见
-End-
►更多了解◄
点击阅读原文,查看文盘Rust系列内容
边栏推荐
- COMSOL --- construction of micro resistance beam model --- final temperature distribution and deformation --- addition of materials
- ClickHouse原理解析与应用实践》读书笔记(8)
- Redission源码解析
- Remote sensing contribution experience sharing
- 谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
- 发现值守设备被攻击后分析思路
- 喜欢测特曼的阿洛
- Installing and using mpi4py
- 力扣5_876. 链表的中间结点
- 静态路由配置全面详解,静态路由快速入门指南
猜你喜欢
ClickHouse原理解析与应用实践》读书笔记(8)
分布式定时任务之XXL-JOB
日志特征选择汇总(基于天池比赛)
XMeter Newsletter 2022-06|企业版 v3.2.3 发布,错误日志与测试报告图表优化
Exit of processes and threads
leetcode 865. Smallest Subtree with all the Deepest Nodes | 865. The smallest subtree with all the deepest nodes (BFs of the tree, parent reverse index map)
Flutter 3.0框架下的小程序运行
常见的磁盘格式以及它们之间的区别
[target tracking] |atom
Graphic network: uncover the principle behind TCP's four waves, combined with the example of boyfriend and girlfriend breaking up, which is easy to understand
随机推荐
nmap工具介紹及常用命令
leetcode 866. Prime Palindrome | 866. 回文素数
A comprehensive and detailed explanation of static routing configuration, a quick start guide to static routing
#797div3 A---C
Version 2.0 of tapdata, the open source live data platform, has been released
常见的磁盘格式以及它们之间的区别
Keras深度学习实战——基于Inception v3实现性别分类
Codeforces Round #643 (Div. 2)——B. Young Explorers
JVM memory and garbage collection-3-object instantiation and memory layout
喜欢测特曼的阿洛
cv2读取视频-并保存图像或视频
leetcode 865. Smallest Subtree with all the Deepest Nodes | 865.具有所有最深节点的最小子树(树的BFS,parent反向索引map)
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
Why does the updated DNS record not take effect?
[reinforcement learning medical] deep reinforcement learning for clinical decision support: a brief overview
Sword finger offer II 041 Average value of sliding window
WPF custom realistic wind radar chart control
Redission源码解析
burpsuite
【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking