当前位置:网站首页>Qt项目中的日志库log4qt使用
Qt项目中的日志库log4qt使用
2022-07-05 06:27:00 【苏州-青蛙】
项目中编译了一个log4qt的库来作为项目应用层的日志库,就写下log4qt的运用。
1.下载编译
拉取 https://github.com/devbean/log4qt 上log4qt代码自行编译
https://github.com/devbean/log4qt
2.log4qt配置文件 log4qt.conf
log4j.rootLogger=INFO,ROLLING_FILE
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.AppendFile=true
log4j.appender.ROLLING_FILE.MaxFileSize=10MB
log4j.appender.ROLLING_FILE.MaxBackupIndex=5
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%p] %m%n日志参数说明,转载自Log4Qt的基本使用_52_赫兹的鲸的博客-CSDN博客_log4qt
一.参数意义说明
输出级别的种类
ERROR、WARN、INFO、DEBUG
ERROR 为严重错误 主要是程序的错误
WARN 为一般警告,比如session丢失
INFO 为一般要显示的信息,比如登录登出
DEBUG 为程序的调试信息
配置日志信息输出目的地
log4j.appender.appenderName = fully.qualified.name.of.appender.class
1.org.apache.log4j.ConsoleAppender(控制台)
2.org.apache.log4j.FileAppender(文件)
3.org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
4.org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件)
5.org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
配置日志信息的格式
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
1.org.apache.log4j.HTMLLayout(以HTML表格形式布局),
2.org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
3.org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
4.org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
控制台选项
Threshold=DEBUG:指定日志消息的输出最低层次。
ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。
Target=System.err:默认情况下是:System.out,指定输出控制台
FileAppender 选项
Threshold=DEBUF:指定日志消息的输出最低层次。
ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。
File=mylog.txt:指定消息输出到mylog.txt文件。
Append=false:默认值是true,即将消息增加到指定文件中,false指将消息覆盖指定的文件内容。
RollingFileAppender 选项
Threshold=DEBUG:指定日志消息的输出最低层次。
ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。
File=mylog.txt:指定消息输出到mylog.txt文件。
Append=false:默认值是true,即将消息增加到指定文件中,false指将消息覆盖指定的文件内容。
MaxFileSize=100KB: 后缀可以是KB, MB 或者是 GB. 在日志文件到达该大小时,将会自动滚动,即将原来的内容移到mylog.log.1文件。
MaxBackupIndex=2:指定可以产生的滚动文件的最大数。
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p %d{yyyy-MM-dd HH:mm:ssS} %c %m%n
日志信息格式中几个符号所代表的含义:
-X号: X信息输出时左对齐;
%p: 输出日志信息优先级,即DEBUG,INFO,WARN,ERROR,FATAL,
%d: 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},输出类似:2002年10月18日 22:10:28,921
%r: 输出自应用启动到输出该log信息耗费的毫秒数
%c: 输出日志信息所属的类目,通常就是所在类的全名
%t: 输出产生该日志事件的线程名
%l: 输出日志事件的发生位置,相当于%C.%M(%F:%L)的组合,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main (TestLog4.java:10)
%x: 输出和当前线程相关联的NDC(嵌套诊断环境),尤其用到像java servlets这样的多客户多线程的应用中。
%%: 输出一个"%"字符
%F: 输出日志消息产生时所在的文件名称
%L: 输出代码中的行号
%m: 输出代码中指定的消息,产生的日志具体信息
%n: 输出一个回车换行符,Windows平台为"\r\n",Unix平台为"\n"输出日志信息换行
可以在%与模式字符之间加上修饰符来控制其最小宽度、最大宽度、和文本的对齐方式。如:
1) c:指定输出category的名称,最小的宽度是20,如果category的名称小于20的话,默认的情况下右对齐。
2)%-20c:指定输出category的名称,最小的宽度是20,如果category的名称小于20的话,"-"号指定左对齐。
3)%.30c:指定输出category的名称,最大的宽度是30,如果category的名称大于30的话,就会将左边多出的字符截掉,但小于30的话也不会有空格。
4) .30c:如果category的名称小于20就补空格,并且右对齐,如果其名称长于30字符,就从左边较远输出的字符截掉。
3.项目使用
//path为log4qt配置文件路径
Log4Qt::PropertyConfigurator::configure("log4qt.conf");
//在Release下将所有qDebug()、qFatal()等输出到控制台的调试信息写入到日志文件中,Debug则设置不写入
#ifdef QT_DEBUG
Log4Qt::LogManager::setHandleQtMessages(false);
#else
Log4Qt::LogManager::setHandleQtMessages(true);
#endif
//logger可以添加多个Appender,所以可以同一条信息写入多个输出“文件”中,这里只输出一个
QList<Log4Qt::Appender*> appenders = Log4Qt::Logger::rootLogger()->appenders();
for(int i=0; i<appenders.size(); i++)
{
Log4Qt::Appender* appender = appenders[i];
Log4Qt::FileAppender* fileAppender = dynamic_cast<Log4Qt::FileAppender*>(appender);
if(fileAppender)
{
QString homePath = GetHomePath();
QDir dir(homePath);
if (!dir.exists()) {
dir.mkpath(homePath);
}
fileAppender->setFile("path");
fileAppender->activateOptions();
break;
}
}边栏推荐
- Adg5412fbruz-rl7 applies dual power analog switch and multiplexer IC
- Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
- Vscode editor
- 7. Oracle table structure
- Find the combination number acwing 888 Find the combination number IV
- kata container
- Interval problem acwing 906 Interval grouping
- TCP's understanding of three handshakes and four waves
- Redis-01. First meet redis
- Financial risk control practice -- feature derivation based on time series
猜你喜欢

Financial risk control practice -- feature derivation based on time series

Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a

【高德地图POI踩坑】AMap.PlaceSearch无法使用

容斥原理 AcWing 890. 能被整除的数

5. Oracle TABLESPACE

Alibaba's new member "Lingyang" officially appeared, led by Peng Xinyu, Alibaba's vice president, and assembled a number of core department technical teams

Stack acwing 3302 Expression evaluation

论文阅读报告

namespace

4. Oracle redo log file management
随机推荐
Filter the numbers and pick out even numbers from several numbers
AE tutorial - path growth animation
H5 module suspension drag effect
Dataframe (1): introduction and creation of dataframe
P2575 master fight
Stack acwing 3302 Expression evaluation
在本地搭建一个微服务集群环境,学习自动化部署
区间问题 AcWing 906. 区间分组
安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
微信小程序路由再次跳轉不觸發onload
Paper reading report
Vant weapp swippecell set multiple buttons
What is linting
Vscode creates its own code template
Mutual transformation between two-dimensional array and sparse array (sparse matrix)
Rehabilitation type force deduction brush question notes D1
Bit of MySQL_ OR、BIT_ Count function
GDB code debugging
2022-5-the fourth week daily
Skywalking全部