log4qt
https://www.it610.com/article/1288451581632651264.htm
直接下载这个tag的版本。编译时如果出现错误,共4处,serial的注掉,light改为lighter, dark改为darker。也可能是正常的,没有错误。
https://github.com/MEONMedical/Log4Qt/releases/tag/v1.5.1
注意:master分支代码并不支持文件名与行号打印。
============================================
qt工程引入 :
include(Log4Qt/src/log4qt/log4qt.pri)
include(Log4Qt/build.pri)
include(Log4Qt/g++.pri)
QT += concurrent
DEFINES += LOG4QT_LIBRARY
DEFINES += LOG4QT_EXPORT
DEFINES += QT_MESSAGELOGCONTEXT
INCLUDEPATH += Log4Qt/src/
INCLUDEPATH += Log4Qt/src/log4qt
DEPENDPATH += Log4Qt/src/log4qt
上面的红色必须要加,否则也会有问题
=============================================
配置文件
log4qt.properties
#log4j.rootLogger=DEBUG,STDOUT
#log4j.rootLogger=DEBUG,FILE
#log4j.rootLogger=DEBUG,STDOUT,FILE,DAILY
#log4j.rootLogger=DEBUG,FILE,DAILY
#log4j.rootLogger=DEBUG,FILE
#log4j.rootLogger=DEBUG,DAILY
log4j.rootLogger=DEBUG,STDOUT,FILE,DAILY
######################## FILE ################################
log4j.appender.FILE = org.apache.log4j.FileAppender
log4j.appender.FILE.File=file.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] - %m%n
########################## STDOUT ##################################
log4j.appender.STDOUT = org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%F:%L] [%p] - %m%n
########################## DAILY ##################################
log4j.appender.DAILY = org.apache.log4j.DailyRollingFileAppender
log4j.appender.DAILY.File=daily.log
log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout
log4j.appender.DAILY.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] - %m%n
测试:
#include <QCoreApplication>
#include "log4qt/logger.h"
#include "log4qt/basicconfigurator.h"
#include "log4qt/propertyconfigurator.h"
#include "log4qt/logmanager.h"
#include "QDebug"
using namespace std;
using namespace Log4Qt;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
PropertyConfigurator::configureAndWatch("D:\\qtworkspace\\test_log4qt_for_function\\log4qt.properties");
LogManager::setHandleQtMessages(true);
qDebug()<<"radar display begins to boot up ."<<endl; //只有这种打日志的方式可以输出文件名,函数名与行号
qDebug("hello world!");
return a.exec();
}
最后这种能够输出文件名,行号,函数名的原因是他们已经自带国这些信息。
#define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
#define qInfo QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).info
#define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
#define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical
#define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal