Qt中第三方日志库QsLog
1.下载源码并编译,得到共享库文件libQsLog.so
qslog的下载地址:https://github.com/victronenergy/QsLog。
2.在测试工程目录中,创建QsLog文件夹,在QsLog文件夹下创建bin文件夹(放入上一步的共享库),在QsLog文件夹下创建include(放入第三方库的所有头文件)。
3.右键项目工程添加库,选择动态库,然后在Pro文件中包含QsLog的头文件。
4.配置日志功能
// 1. 启动日志记录机制 Logger& logger = Logger::instance(); logger.setLoggingLevel(QsLogging::TraceLevel); //设置log位置为exe所在目录 const QString sLogPath(QDir(QCoreApplication::applicationDirPath()).filePath("log.txt")); // 2. 添加两个destination DestinationPtr fileDestination(DestinationFactory::MakeFileDestination( sLogPath, EnableLogRotation, MaxSizeBytes(512), MaxOldLogCount(2))); DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination()); //DestinationPtr functorDestination(DestinationFactory::MakeFunctorDestination(&logFunction)); //这样和槽函数连接 //DestinationPtr sigsSlotDestination(DestinationFactory::MakeFunctorDestination(this, SLOT(logSlot(QString,int)))); logger.addDestination(debugDestination); logger.addDestination(fileDestination); //logger.addDestination(functorDestination); //logger.addDestination(sigsSlotDestination);
4.使用日志功能
// 3. 开始日志记录 QLOG_INFO() << "Program started"; QLOG_INFO() << "Built with Qt" << QT_VERSION_STR << "running on" << qVersion(); QLOG_TRACE() << "Here's a" << QString::fromUtf8("trace") << "message"; QLOG_DEBUG() << "Here's a" << static_cast<int>(QsLogging::DebugLevel) << "message"; QLOG_WARN() << "Uh-oh!"; qDebug() << "This message won't be picked up by the logger"; QLOG_ERROR() << "An error has occurred"; qWarning() << "Neither will this one"; QLOG_FATAL() << "Fatal error!";