编译
从网上下载Log4Qt源码
使用QtVisualStudioTools工具导入Vs项目中
修改相关配置,生成静态库
使用
这边进行了二次封装
class CMyLog4Qt
{
public:
//************************************
// 作者:
// 日期: 2020/06/05
// 函数: initLog
// 参数: const QString & logpath
// 返回: void
// 功能: 初始化日志
//************************************
static void initLog(const QString& logconfig = LibPublic::GetCurrentPath() + "/Config/Log4Qt.conf")
{
Log4Qt::PropertyConfigurator::configure(logconfig);
};
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//Log4Qt::PropertyConfigurator::configure(QString(QCoreApplication::applicationDirPath()) + "/Config/Log4Qt.conf");
CMyLog4Qt::initLog();
int i = 0;
while (1)
{
i++;
qDebug() << QString::fromLocal8Bit("测试日志qDebug %1")
.arg(i)
.toUtf8().data();
qInfo() << QString::fromLocal8Bit("测试日志qInfo %1")
.arg(i)
.toUtf8().data();
qWarning() << QString::fromLocal8Bit("测试日志qWarning %1")
.arg(i)
.toUtf8().data();
qCritical() << QString::fromLocal8Bit("测试日志qCritical %1")
.arg(i)
.toUtf8().data();
}
return a.exec();
}
配置文件
网络整理的配置文件
logpath=./Logs
logConversionPattern=[ %d{yyyy-MM-dd HH:mm:ss.zzz} ] [ %p ] --> %m %n
log4j.handleQtMessages=true
log4j.rootLogger=ALL, console, daily, rolling
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=${logConversionPattern}
log4j.appender.daily=org.apache.log4j.DailyFileAppender
log4j.appender.daily.file=${logpath}/Log.log
log4j.appender.daily.datePattern=_yyyy_MM_dd
log4j.appender.daily.appendFile=true
log4j.appender.daily.keepDays=30
log4j.appender.daily.layout=org.apache.log4j.PatternLayout
log4j.appender.daily.layout.ConversionPattern=${logConversionPattern}
log4j.appender.rolling=org.apache.log4j.RollingFileAppender
log4j.appender.rolling.file=${logpath}/Log_Rolling.log
log4j.appender.rolling.appendFile=false
log4j.appender.rolling.MaxFileSize=50KB
log4j.appender.rolling.MaxBackupIndex=10
log4j.appender.rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.rolling.layout.ConversionPattern=${logConversionPattern}