1.下载
git clone https://github.com/MEONMedical/Log4Qt.git
2.配置
QT里面建项目如myproject,复制Log4Qt\src下面的log4qt目录到新建项目里面,例如复制到项目的跟目录,我这边的目录结构是myproject里面放的是新建的项目,myproject\log4qt是log4qt的源码。
打开myproject.pro项目配置文件,修改或增加以下内容:
QT += core gui network concurrent //为log4qt 增加network concurrent
DEFINES +=LOG4QT_STATIC //说明不是通过dll导入等方式去使用log4qt
LOG4QTSRCPATH = $$PWD/log4qt //log4qt源码目录
INCLUDEPATH += -L $$LOG4QTSRCPATH \
$$LOG4QTSRCPATH/helpers \
$$LOG4QTSRCPATH/spi \
$$LOG4QTSRCPATH/varia
DEPENDPATH += $$LOG4QTSRCPATH \
$$LOG4QTSRCPATH/helpers \
$$LOG4QTSRCPATH/spi \
$$LOG4QTSRCPATH/varia
include($$PWD/log4qt/log4qt.pri)
打开log4qt/log4qt.pri文件,增加以下内容(部分内容复制自build.pri)
INCLUDEPATH += -L $$PWD \
$$PWD/helpers \
$$PWD/spi \
$$PWD/varia
DEPENDPATH += $$PWD \
$$PWD/helpers \
$$PWD/spi \
$$PWD/varia
DEFINES +=LOG4QT_STATIC
LOG4QT_VERSION_MAJOR = 1
LOG4QT_VERSION_MINOR = 6
LOG4QT_VERSION_PATCH = 0
DEFINES += LOG4QT_VERSION_MAJOR=$${LOG4QT_VERSION_MAJOR}
DEFINES += LOG4QT_VERSION_MINOR=$${LOG4QT_VERSION_MINOR}
DEFINES += LOG4QT_VERSION_PATCH=$${LOG4QT_VERSION_PATCH}
DEFINES += LOG4QT_VERSION_STR='\\"$${LOG4QT_VERSION_MAJOR}.$${LOG4QT_VERSION_MINOR}.$${LOG4QT_VERSION_PATCH}\\"'
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x050F00
DEFINES += QT_DEPRECATED_WARNINGS
编写日志输出配置参数文件信息log4qt.conf
#设置储存log文件的根目录
logpath=.
log4j.reset=true
log4j.Debug=WARN
log4j.threshold=NULL
#设置是否监听QDebug输出的字符串
log4j.handleQtMessages=true
#在运行中,是否监视此文件配置的变化
log4j.watchThisFile=false
#设置根Logger的输出log等级为All
#设置Log输出的几种输出源(appender):console, daily, rolling
log4j.rootLogger=ALL, console, daily
#设置终端打印记录器
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=STDOUT_TARGET
log4j.appender.console.layout=org.apache.log4j.TTCCLayout
log4j.appender.console.layout.dateFormat=dd.MM.yyyy hh:mm:ss.zzz
log4j.appender.console.layout.contextPrinting=true
log4j.appender.console.threshold=ALL
#设置一个每日储存一个log文件的记录器
log4j.appender.daily=org.apache.log4j.DailyFileAppender
log4j.appender.daily.file=${logpath}/mylog.log
log4j.appender.daily.appendFile=true
log4j.appender.daily.datePattern=_yyyy_MM_dd
log4j.appender.daily.keepDays=90
log4j.appender.daily.layout=${log4j.appender.console.layout}
log4j.appender.daily.layout.dateFormat=${log4j.appender.console.layout.dateFormat}
log4j.appender.daily.layout.contextPrinting=${log4j.appender.console.layout.contextPrinting}
# 配置一个滚动文件记录器
log4j.appender.rolling=org.apache.log4j.RollingFileAppender
log4j.appender.rolling.file= ${logpath}/mylog_rolling.log
log4j.appender.rolling.appendFile=true
log4j.appender.rolling.maxFileSize= 20MB
log4j.appender.rolling.maxBackupIndex= 10
log4j.appender.rolling.layout=${log4j.appender.console.layout}
log4j.appender.rolling.layout.dateFormat=${log4j.appender.console.layout.dateFormat}
log4j.appender.rolling.layout.contextPrinting=${log4j.appender.console.layout.contextPrinting}
# 给“LoggerObjectPrio”这个类的Logger定义log输出等级为Error,
# 给“LoggerObjectPrio”这个类的Logger定义log输出源:daily, console
log4j.logger.LoggerObjectPrio=ERROR, rolling
#设置为false,表示“LoggerObjectPrio”这个类的logger不继承的rootLogger输出源(appender)
log4j.additivity.LoggerObjectPrio=false
修改main.cpp,增加对log4qt的初始化相关代码
#include <QtDebug>
#include "logmanager.h"
#include "propertyconfigurator.h"
...
Log4Qt::PropertyConfigurator::configure(a.applicationDirPath() + "/log4qt.conf");
Log4Qt::LogManager::setHandleQtMessages(true);
qDebug("start working...");
复制log4qt.conf文件到项目build编译结果路径,运行项目,在build路径内就可以看到日志文件了,如下图mylog_2021_02_03.log。