一、easylogging++ 是封装好的类,无需依赖库,用起来非常方便,如何加载到QT的工程中,有以下几点:
#include <QCoreApplication>
#include "common/easylogging++.h"
INITIALIZE_EASYLOGGINGPP
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
LOG(INFO) << "My first info log using default logger";
el::Configurations conf("C:/Program Files/my_log.conf");
el::Loggers::reconfigureAllLoggers(conf);
uint64_t i = 0;
while(i<100)
{
LOG(DEBUG) << "***** debug log" <<i<<" "<<__FILE__<<" "<<__LINE__;
i++;
}
//LOG(TRACE) << "***** trace log *****";
LOG(DEBUG) << "***** debug log *****";
LOG(ERROR) << "***** error log *****";
LOG(WARNING) << "***** warning log *****";
LOG(INFO) << "***** info log *****";
system("pause");
return a.exec();
}
1、在工程中加入easylogging++.h 和 easylogging++.cc;
2、在QT工程的头文件中加入 #include "common/easylogging++.h"和INITIALIZE_EASYLOGGINGPP;
3、配置my_log.conf即可;
二、easylogging支持日志滚动输出,但是不支持日志的删除;时间长了,就会产生很多的冗余垃圾文件,需要定期删除,如下所示
//删除log日志 机制预留3天日志;
QString dirpath = "C:/logpath";
if(!dirpath.isEmpty()){
//设置要遍历的目录
QDir dir(dirpath);
//设置文件过滤器
QStringList nameFilters;
//设置文件过滤格式
nameFilters << "*.log";
//将过滤后的文件名称存入到files列表中
QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Time);
while(files.count() > 9){
QString tmp = files.back();
QFile::remove(dirpath + QString("/") + tmp);
files.pop_back();
}
}