Log4Qt介绍
Log4Qt是使用Trolltech Qt Framework的Apache Software Foundation Log4j包的C ++端口。它旨在供开源和商业Qt项目使用。
1、下载源码、将源码放在你所需要的项目中。
include($$PWD/src/log4qt/log4qt.pri)
源码地址:https://sourceforge.net/projects/log4qt/
2、封装日志类
封装成独立的日志类,这样不论用什么日志系统只需要改变该类,代码其他模块无需改变,使用单例模式,因log4qt本身是线程安全的,所以不用考虑多线程,下面是封装的log4qt日志类
#ifndef LOG_H
#define LOG_H
#include <QObject>
#include <QMutex>
#include <QMutexLocker>
#include <QCoreApplication>
#include "log4qt/logger.h"
#include "log4qt/basicconfigurator.h"
/************************************************************\
* 类名:Log
* 描述:封装log4qt开源库类
* 注意:模块已带log4qt for qt4 和 qt5两个版本;
* 模块已带log4qt配置文件,分为4级别:DEBUG > INFO > WARN > ERROR
* 信号:
* 函数:
* instance() - 获取日志唯一实例
* 槽函数:
* slot_init() - 初始化加载配置文件
* slot_debug() - 调试级别日志
* slot_info() - 信息级别日志
* slot_warn() - 警告级别日志
* slot_error() - 错误级别日志
*
\************************************************************/
class Log : public QObject
{
Q_OBJECT
public:
explicit Log(QObject *parent = nullptr);
signals:
public slots:
static Log * instance();
public slots:
void init(QString configFilePath);
public slots:
void debug(QString msg);
void info(QString msg);
void warn(QString msg);
void error(QString msg);
private:
static Log *_pInstance;
static QMutex _mutex;
static Log4Qt::Logger * _pLoggerDebug;
static Log4Qt::Logger * _pLoggerInfo;
static Log4Qt::Logger * _pLoggerWarn;
static Log4Qt::Logger * _pLoggerError;
static QString _configFilePath;
};
#endif
#include "Log.h"
#include "log4qt/log4qt.h"
#include "log4qt/propertyconfigurator.h"
Log * Log::_pInstance = 0;
QMutex Log::_mutex;
Log4Qt::Logger * Log::_pLoggerDebug = 0;
Log4Qt::Logger * Log::_pLoggerInfo = 0;
Log4Qt::Logger * Log::_pLoggerWarn = 0;
Log4Qt::Logger * Log::_pLoggerError = 0;
QString Log::_configFilePath;
Log::Log(QObject *parent) : QObject(parent)
{
// 一定要配置文件,不然运行起来会直接当掉
Log4Qt::BasicConfigurator::configure();
}
Log *Log::instance()
{
if(!_pInstance)
{
QMutexLocker mutexLocker(&_mutex);
if(!_pInstance)
{
Log *pInstance = new Log();
_pInstance = pInstance;
}
}
return _pInstance;
}
void Log::init(QString configFilePath)
{
_configFilePath = configFilePath;
Log4Qt::PropertyConfigurator::configure(_configFilePath);
_pLoggerDebug = Log4Qt::Logger::logger("debug");
_pLoggerInfo = Log4Qt::Logger::logger("info");
_pLoggerWarn = Log4Qt::Logger::logger("warn");
_pLoggerError = Log4Qt::Logger::logger("error");
}
void Log::debug(QString msg)
{
_pLoggerDebug->debug(msg);
}
void Log::info(QString msg)
{
_pLoggerInfo->info(msg);
}
void Log::warn(QString msg)
{
_pLoggerWarn->warn(msg);
}
void Log::error(QString msg)
{
_pLoggerError->error(msg);
}
3、测试代码
main.cpp
#include <QApplication>
#include <QTextCodec>
#include <QThread>
#include "Log.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QThread::currentThread()->setObjectName("主线程");
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
Log::instance()->init(QCoreApplication::applicationDirPath() + "/" +"etc/log.conf");
Log::instance()->debug("调试测试日志系统当中...");
Log::instance()->info("信息测试日志系统当中...");
Log::instance()->warn("警告测试日志系统当中...");
Log::instance()->error("错误测试日志系统当中...");
return a.exec();
}
4、配置文件
log.conf
log4j.logger.debug=trace,debug
log4j.appender.debug=org.apache.log4j.RollingFileAppender
log4j.appender.debug.appendFile=true
log4j.appender.debug.File=log/debug.txt
log4j.appender.debug.MaxFileSize=4096KB
log4j.appender.debug.MaxBackupIndex=7
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.zzz}[%t][%p] %m%n
log4j.logger.info=trace,info,debug
log4j.appender.info=org.apache.log4j.RollingFileAppender
log4j.appender.info.appendFile=true
log4j.appender.info.File=log/info.txt
log4j.appender.info.MaxFileSize=4096KB
log4j.appender.info.MaxBackupIndex=7
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.zzz}[%t][%p] %m%n
log4j.logger.warn=trace,info,debug,warn
log4j.appender.warn=org.apache.log4j.RollingFileAppender
log4j.appender.warn.appendFile=true
log4j.appender.warn.File=log/warn.txt
log4j.appender.warn.MaxFileSize=4096KB
log4j.appender.warn.MaxBackupIndex=2
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.zzz}[%t][%p] %m%n
log4j.logger.error=trace,info,debug,warn,error
log4j.appender.error=org.apache.log4j.RollingFileAppender
log4j.appender.error.appendFile=true
log4j.appender.error.File=log/error.txt
log4j.appender.error.MaxFileSize=4096KB
log4j.appender.error.MaxBackupIndex=2
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.zzz}[%t][%p] %m%n