1、下载Log4Qt 源码:https://sourceforge.net/projects/log4qt/
2、添加到工程:
3、创建一个继承QObject的单列,关键步骤是在一个名字为 LOG4QT_DECLARE_QCLASS_LOGGER的宏,这样才可以使用log4qt 否则不可使用 ,在构造函数里面加载log4qt的配置文件log4qt.conf
*******************************qlog.h*****************************************
#ifndef QLOG_H
#define QLOG_H
#include <QObject>
#include <QMutex>
#include <QMutexLocker>
#include "log4qt/logger.h"
#include "log4qt/log4qt.h"
class QLog : public QObject
{
Q_OBJECT
LOG4QT_DECLARE_QCLASS_LOGGER
public:
explicit QLog(QObject *parent = 0);
static QLog * instance();
virtual void debug(const QString& log){logger()->debug(log);}
virtual void info (const QString& log){logger()->info(log);}
virtual void warn (const QString& log){logger()->warn(log);}
virtual void error(const QString& log){logger()->error(log);}
virtual void fatal(const QString& log){logger()->fatal(log);}
private:
static QAtomicPointer<QLog> _instance;
static QMutex _mutex;
};
#endif // QLOG_H
**************************************qlog.cpp*****************************************
#include "qlog.h"
#include <QApplication>
#include "log4qt/propertyconfigurator.h"
QAtomicPointer<QLog> QLog::_instance = 0;
QMutex QLog::_mutex;
QLog::QLog(QObject *parent) : QObject(parent)
{
QString configpath = QApplication::applicationDirPath()+"/log4qt.conf";
Log4Qt::PropertyConfigurator::configure(configpath);
logger()->info("start used log!!!");
}
QLog *QLog::instance()
{
#ifndef Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
if(!QAtomicPointer::isTestAndSetNative())//运行时检测
qDebug() << "Error: TestAndSetNative not supported!";
#endif
//使用双重检测。
/*! testAndSetOrders操作保证在原子操作前和后的的内存访问
* 不会被重新排序。
*/
if(_instance.testAndSetOrdered(0, 0))//第一次检测
{
QMutexLocker locker(&_mutex);//加互斥锁。
_instance.testAndSetOrdered(0, new QLog);//第二次检测。
}
return _instance;
}
4、配置文件如下 log4qt.conf
log4j.rootLogger=info,error
log4j.additivity.org.apache=true
log4j.appender.error=Log4Qt::RollingFileAppender
log4j.appender.error.ImmediateFlush=true
log4j.appender.error.AppendFile=true
log4j.appender.error.File=log/TD.trace
log4j.appender.error.MaxFileSize=4096KB
log4j.appender.error.MaxBackupIndex=50
log4j.appender.error.layout=Log4Qt::PatternLayout
log4j.appender.error.layout.ConversionPattern=[%p] %d(%r) --> %m %n
5最简单使用如下:
QLog::instance()->debug("debug");
QLog::instance()->info("debug");
QLog::instance()->warn("debug");
QLog::instance()->error("debug");
QLog::instance()->fatal("debug");
6.扩展使用
#ifndef DEBUG_H_
#define DEBUG_H_
#include "qlog.h"
#if 1 // 写入日志系统
// 以文件行列记录信息,非以类名对象形式
#define LOG_DEBUG(msg) QLog::instance()->debug(QString("[%1] Message:%2").arg(QString("###FILE:%1 ###LINE:%2 ###FUNC:%3").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__)).arg(msg));
#define LOG_INFO(msg) QLog::instance()->info(QString("[%1] Message:%2").arg(QString("###FILE:%1 ###LINE:%2 ###FUNC:%3").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__)).arg(msg));
#define LOG_WARN(msg) QLog::instance()->warn(QString("[%1] Message:%2").arg(QString("###FILE:%1 ###LINE:%2 ###FUNC:%3").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__)).arg(msg));
#define LOG_ERROR(msg) QLog::instance()->error(QString("[%1] Message:%2").arg(QString("###FILE:%1 ###LINE:%2 ###FUNC:%3").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__)).arg(msg));
// 以类名对象形式记录信息
#define LOG_OBJECT_DEBUG(msg) QLog::instance()->debug(QString("[%1]%2").arg(QString("%1::%2:%3").arg(this->metaObject()->className()).arg(__FUNCTION__).arg(__LINE__)).arg(msg));
#define LOG_OBJECT_INFO(msg) QLog::instance()->info(msg);
#define LOG_OBJECT_WARN(msg) QLog::instance()->warn(msg);
#define LOG_OBJECT_ERROR(msg) QLog::instance()->error(msg);
#endif
#endif
7.使用
LOG_INFO("加油中国"):