1.首先 main初始化:
#include "mainwindow.h"
#include <QApplication>
#include "globalfunction.h"
#include "Log/EasyLogging++.h"
INITIALIZE_EASYLOGGINGPP
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
/* init easy log */
START_EASYLOGGINGPP(argc, argv);
.
.
.
.
.
.
.
}
2.封装调用:注意LOG()<< content.toStdString,之前是“拿来主义”,就没多想,使用的是toStdWString(),只支持英文, 修改QT编码,可是中文怎么都无法打印出来......................
void Log::insert(int type, int level, QString content)
{
QString strType;
if (type == Sys) {
strType = " Sys ";
} else if (type == Usr) {
strType = " Usr ";
}
if (level == Info) {
LOG(INFO) << strType.toStdWString() << content.toStdString();
} else if (level == Warning) {
LOG(WARNING) << strType.toStdWString() << content.toStdString();
} else if (level == Error) {
LOG(ERROR) << strType.toStdWString() << content.toStdString();
} else if (level == Debug) {
LOG(DEBUG) << strType.toStdWString() << content.toStdString();
}
if ((LogType)type == Usr)
{
emit sigUserLog(level, content);
}
}
3.使用easylogging++ 还可以i自定义日志格式,一篇博客不错,分享下: