log4qt

文章目录


一、log4qt编译

  1. 下载log4qt源码
    1. https://sourceforge.net/projects/log4qt/
    2. https://github.com/devbean/log4qt
    3. https://gitee.com/mirrors/log4qt
  2. 编译源码:
    使用qt打开下载文件的pro文件,选择要编译的环境,在编译器内选中src文件,右键选择运行,编译完成可在编译的文件目录中找到库文件。
    (具体编译方式网上很多,不会的自己找找)

二、使用步骤

1.引入库

在项目文件右键:添加库>>外部库>>浏览(库文件)>>静态>>去掉Linux >>去掉为debug版本添加d作为后缀>>下一步>>完成

2.封装一个线程安全的日志接口

cpp 复制代码
#include <QObject>
#include <QMutex>
#include <QMutexLocker>
#include <QCoreApplication>
#include "log4qt/logger.h"

class LogMaster : public QObject
{
    Q_OBJECT
public:
    explicit LogMaster(QObject *parent = nullptr);

public:
    static LogMaster * instance();
    
signals:

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 LogMaster *pInstance;
    static QMutex m_mutex;
    static Log4Qt::Logger * m_pLoggerDebug;
    static Log4Qt::Logger * m_pLoggerInfo;
    static Log4Qt::Logger * m_pLoggerWarn;
    static Log4Qt::Logger * m_pLoggerError;
    static QString m_configFilePath;
};
cpp 复制代码
#include "logmaster.h"
#include <QDebug>
#include "log4qt/propertyconfigurator.h"
#include "log4qt/basicconfigurator.h"

LogMaster * LogMaster::pInstance = 0;
QMutex LogMaster::m_mutex;
Log4Qt::Logger * LogMaster::m_pLoggerDebug = 0;
Log4Qt::Logger * LogMaster::m_pLoggerInfo = 0;
Log4Qt::Logger * LogMaster::m_pLoggerWarn = 0;
Log4Qt::Logger * LogMaster::m_pLoggerError = 0;
QString LogMaster::m_configFilePath;

LogMaster::LogMaster(QObject *parent) : QObject(parent)
{
    // 一定要配置文件,不然运行起来会直接当掉
    Log4Qt::BasicConfigurator::configure();
}

LogMaster *LogMaster::instance()
{
    if(!pInstance)
    {
        QMutexLocker mutexLocker(&m_mutex);
        if(!pInstance)
        {
            LogMaster *p = new LogMaster();
            pInstance = p;
        }
    }
    return pInstance;
}

void LogMaster::init(QString configFilePath)
{
    m_configFilePath = configFilePath;
    Log4Qt::PropertyConfigurator::configure(m_configFilePath);
    m_pLoggerDebug = Log4Qt::Logger::logger("debug");
    m_pLoggerInfo = Log4Qt::Logger::logger("info");
    m_pLoggerWarn = Log4Qt::Logger::logger("warn");
    m_pLoggerError = Log4Qt::Logger::logger("error");
}

void LogMaster::debug(QString msg)
{
    m_pLoggerDebug->debug(msg);
}

void LogMaster::info(QString msg)
{
    m_pLoggerInfo->info(msg);
}

void LogMaster::warn(QString msg)
{
    m_pLoggerWarn->warn(msg);
}

void LogMaster::error(QString msg)
{
    m_pLoggerError->error(msg);
}

3.使用

cpp 复制代码
#include <QApplication>
#include <QTextCodec>
#include <QThread>
#include "logmaster.h"

void init_log_conf()
{
    //
    QDir dir;
    if(!dir.exists("./Conf")) dir.mkdir("./Conf");
    if(!dir.exists(QCoreApplication::applicationDirPath() + "/" +"Conf")) dir.mkdir(QCoreApplication::applicationDirPath() + "/" +"Conf");

    //!
    QFile file(QCoreApplication::applicationDirPath() + "/" +"Conf/log.conf");
    if(!file.exists())
    {
        if(file.open(QIODevice::WriteOnly))
        {
            QString text = "";

            text += "logpath1=./log/debug\r\n";
            text += "logpath2=./log/info\r\n";
            text += "logpath3=./log/warn\r\n";
            text += "logpath4=./log/error\r\n";
            text += "\r\n";
            text += "# 给"LoggerObjectPrio"这个类的Logger定义log输出等级为Error\r\n";
            text += "log4j.logger.LoggerObjectPrio= error, warn, debug, info\r\n";
            text += "\r\n";
            text += "log4j.logger.debug=debug\r\n";
            text += "log4j.appender.debug=org.apache.log4j.DailyFileAppender\r\n";
            text += "log4j.appender.debug.file=${logpath1}/.log\r\n";
            text += "log4j.appender.debug.datePattern=yyyy-MM-dd-Debug\r\n";
            text += "log4j.appender.debug.keepDays=30\r\n";
            text += "log4j.appender.debug.Threshold=DEBUG\r\n";
            text += "log4j.appender.debug.appendFile=true\r\n";
            text += "log4j.appender.debug.Encoding=UTF-8\r\n";
            text += "log4j.appender.debug.layout=org.apache.log4j.PatternLayout\r\n";
            text += "log4j.appender.debug.layout.ConversionPattern=%d{[yyyy-MM-dd HH:mm:ss.zzz]} %m%n\r\n";
            text += "\r\n";
            text += "log4j.logger.info=debug,info\r\n";
            text += "log4j.appender.info=org.apache.log4j.DailyFileAppender\r\n";
            text += "log4j.appender.info.file=${logpath2}/.log\r\n";
            text += "log4j.appender.info.datePattern=yyyy-MM-dd-Info\r\n";
            text += "log4j.appender.info.keepDays=30\r\n";
            text += "log4j.appender.info.Threshold=DEBUG\r\n";
            text += "log4j.appender.info.appendFile=true\r\n";
            text += "log4j.appender.info.Encoding=UTF-8\r\n";
            text += "log4j.appender.info.layout=org.apache.log4j.PatternLayout\r\n";
            text += "log4j.appender.info.layout.ConversionPattern=%d{[yyyy-MM-dd HH:mm:ss.zzz]} %m%n\r\n";
            text += "\r\n";
            text += "log4j.logger.warn=info,debug,warn\r\n";
            text += "log4j.appender.warn=org.apache.log4j.DailyFileAppender\r\n";
            text += "log4j.appender.warn.file=${logpath3}/.log\r\n";
            text += "log4j.appender.warn.datePattern=yyyy-MM-dd-Warn\r\n";
            text += "log4j.appender.warn.keepDays=30\r\n";
            text += "log4j.appender.warn.Threshold=DEBUG\r\n";
            text += "log4j.appender.warn.appendFile=true\r\n";
            text += "log4j.appender.warn.Encoding=UTF-8\r\n";
            text += "log4j.appender.warn.layout=org.apache.log4j.PatternLayout\r\n";
            text += "log4j.appender.warn.layout.ConversionPattern=%d{[yyyy-MM-dd HH:mm:ss.zzz]} %m%n\r\n";
            text += "\r\n";
            text += "log4j.logger.error=info,debug,error\r\n";
            text += "log4j.appender.error=org.apache.log4j.DailyFileAppender\r\n";
            text += "log4j.appender.error.file=${logpath4}/.log\r\n";
            text += "log4j.appender.error.datePattern=yyyy-MM-dd-Error\r\n";
            text += "log4j.appender.error.keepDays=30\r\n";
            text += "log4j.appender.error.Threshold=DEBUG\r\n";
            text += "log4j.appender.error.appendFile=true\r\n";
            text += "log4j.appender.error.Encoding=UTF-8\r\n";
            text += "log4j.appender.error.layout=org.apache.log4j.PatternLayout\r\n";
            text += "log4j.appender.error.layout.ConversionPattern=%d{[yyyy-MM-dd HH:mm:ss.zzz]} %m%n\r\n";

            file.write(text.toLatin1());
            file.close();
        }
        else
        {
            QMessageBox::warning(NULL, "log4qt(log.conf)加载失败", file.errorString());
        }
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //! 判断有没有日志配置文件, 没有则生成配置文件
    init_log_conf();
    //! 设置配置文件
    LogMaster::instance()->init(QCoreApplication::applicationDirPath() + "/" +"Conf/log.conf");
    //
    MainWindow w;
    w.show();
    //
    return a.exec();
}

4.日志配置文件

cpp 复制代码
log4j.logger.debug=debug
log4j.appender.debug=org.apache.log4j.RollingFileAppender
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.appendFile=true
log4j.appender.debug.Encoding=UTF-8
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=debug,info
log4j.appender.info=org.apache.log4j.RollingFileAppender
log4j.appender.info.Threshold=DEBUG
log4j.appender.info.appendFile=true
log4j.appender.info.Encoding=UTF-8
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=info,debug,warn
log4j.appender.warn=org.apache.log4j.RollingFileAppender
log4j.appender.warn.Threshold=DEBUG
log4j.appender.warn.appendFile=true
log4j.appender.warn.Encoding=UTF-8
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=info,debug,error
log4j.appender.error=org.apache.log4j.RollingFileAppender
log4j.appender.error.Threshold=DEBUG
log4j.appender.error.appendFile=true
log4j.appender.error.Encoding=UTF-8
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

总结

相关推荐
Black_Friend21 分钟前
关于在VS中使用Qt不同版本报错的问题
开发语言·qt
CSUC29 分钟前
【Qt】QTreeView 和 QStandardItemModel的关系
qt
冷凝女子1 小时前
【QT】海康视频及openCv抓拍正脸接口
qt·opencv·音视频·海康
苏三有春3 小时前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt
Vanranrr4 小时前
C++ QT
java·c++·qt
兆。4 小时前
掌握 PyQt5:从零开始的桌面应用开发
开发语言·爬虫·python·qt
徒步僧15 小时前
ThingsBoard规则链节点:RPC Call Reply节点详解
qt·microsoft·rpc
可峰科技16 小时前
斗破QT编程入门系列之一:认识Qt:初步使用(四星斗师)
开发语言·qt
我喜欢就喜欢16 小时前
基于qt vs下的视频播放
开发语言·qt·音视频
CP-DD17 小时前
Qt的架构设计
qt