Qt 实现 Logger 日志----轻量级开源库QsLog的使用

github的下载地址:https://github.com/victronenergy/QsLog
下载后,解压到非中文目录,用qt creator打开(qt5.8.0,windows平台上,亲测可用),如图:
Qt 实现 Logger 日志----轻量级开源库QsLog的使用
编译:
(1)编译QsLogSharedLibrary,在windows平台上,会生成QsLog2.lib和QsLog2.dll
(2)编译官方给的demo,log_example_shared,将生成的,复制到build-QsLogExample目录中。
(3)编译log_example。
运行demo:
在build-QsLogExample文件夹中,双击log_example.exe,即刻看到生成是log。或者在qt createor中运行该工程,可以看到调试台中输出的信息。

将编译的QsLog2.lib和QsLog2.dll应用到我们的项目中。
新建一个MyQsLogDemo工程,将头文件添加到工程搜索目录中:

INCLUDEPATH += $$PWD/include/QsLog/QsLog.h \
               $$PWD/include/QsLog/QsLogDest.h \
               $$PWD/include/QsLog/QsLogDestConsole.h \
               $$PWD/include/QsLog/QsLogDestFile.h \
               $$PWD/include/QsLog/QsLogDestFunctor.h \
               $$PWD/include/QsLog/QsLogDisableForThisFile.h \
               $$PWD/include/QsLog/QsLogLevel.h

添加lib库:

LIBS += $$PWD/lib/QsLog2.lib

在main.cpp文件中添加测试代码:

#include "widget.h"

#include "include/QsLog/QsLog.h"
#include "include/QsLog/QsLog.h"
#include "include/QsLog/QsLogDest.h"

#include <QApplication>
#include <QDir>
#include <iostream>

void logFunction(const QString &message, QsLogging::Level level)
{
    std::cout << "From log function: " << qPrintable(message) << " " << static_cast<int>(level)
              << std::endl;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    using namespace QsLogging;

    // 1. init the logging mechanism
    Logger& logger = Logger::instance();
    logger.setLoggingLevel(QsLogging::TraceLevel);
    const QString sLogPath(QDir(a.applicationDirPath()).filePath("log.txt"));

    // 2. add two destinations
    DestinationPtr fileDestination(DestinationFactory::MakeFileDestination(
      sLogPath, EnableLogRotation, MaxSizeBytes(512), MaxOldLogCount(2)));
    DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination());
    DestinationPtr functorDestination(DestinationFactory::MakeFunctorDestination(&logFunction));
    logger.addDestination(debugDestination);
    logger.addDestination(fileDestination);
    logger.addDestination(functorDestination);

    // 3. start logging
    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!";

    logger.setLoggingLevel(QsLogging::OffLevel);
    for (int i = 0;i < 10000000;++i) {
        QLOG_ERROR() << QString::fromUtf8("this message should not be visible");
    }
    logger.setLoggingLevel(QsLogging::TraceLevel);

    QLOG_DEBUG() << "Program ending";
    QsLogging::Logger::destroyInstance();

    return a.exec();
}

github的下载地址:https://github.com/victronenergy/QsLog
参考: