EZLog
Loading...
Searching...
No Matches
简易使用说明

本库是为了更方便地使用spdlog日志系统,使用前请先打开src文件夹下的Logger.pro进行编译,编译产生的结果放在lib文件夹中。使用时请包含Logger.h及对应的库文件。感谢顾工提供代码参考。

本库默认开启多线程支持,如果你的程序为单线程,建议通过LOGGER_SINGLE_THREAD宏开启单线程模式优化。

示例

在 main.cpp 中初始化日志系统:

#include "MainWindow.h"
#include <QApplication>
#include "Logger.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LOG_INIT(DEMO); // Logger初始化,当日志系统初始化失败时可以用Logger::errMsg方法获取错误信息
MainWindow w;
w.show();
return a.exec();
}
#define LOG_INIT(name)
log initialization, must call before using the logging system (e.g. LogInit(MyProject))
Definition Logger.h:373

在任意需要同时打印Debug信息和日志信息的地方使用 lDebug :

QList<int> lsInt{1, 2, 3};
lDebug() << "lsInt:" << lsInt;
lDebug() << "have 3?" << lsInt.contains(3);
lDebug().timestamp() << "what time?";
#define lDebug
Predefined macro that can output content in both the console and log files. Use this the same way you...
Definition Logger.h:392

控制台显示内容:

[debug] lsInt: (1, 2, 3)
[debug] have 3? true
[21:14:58] [debug] what time?

日志显示内容:

[2022-02-20 21:14:58.457] [DEMO] [debug] lsInt: (1, 2, 3)
[2022-02-20 21:14:58.457] [DEMO] [debug] have 3? true
[2022-02-20 21:14:58.457] [DEMO] [debug] what time?

常用宏

LOG_INIT

对应接口Logger::Initialize,日志系统初始化,可指定项目名称、日志文件前缀、日志保存位置。

LOG_INIT(MyProject); // 日志系统初始化,项目名称MyProject,保存在执行文件根目录

LOGGER_SINGLE_THREAD

当程序为单线程时建议开启,开启方法可以在Logger.h文件中解除注释,或者在<tt>.pro文件中加上DEFINES

DEFINES += LOGGER_SINGLE_THREAD

TAKE_OVER_QDEBUG

对应接口Logger::InstallMessageHandler,用于接管qDebug输出。

TAKE_OVER_QDEBUG; // 接管qDebug输出
#define TAKE_OVER_QDEBUG
Take over qDebug output.
Definition Logger.h:382

lDebug

对应接口Logger::Debug().output,用于在日志文件和控制台同时输出日志内容。

lDebug() << "hello world"; // 将在日志文件和控制台同时输出"hello world"

LogDebug,LogInfo ,LogWarn, LogError,LogCritical

分别对应接口Logger::Debug()Logger::Info()Logger::Warn()Logger::Error()Logger::Critical用于常规日志记录。

LogDebug << "some debug message"; // 在日志中以debug等级记录
LogInfo << "some info message"; // 在日志中以info等级记录
LogWarn << "some warn message"; // 在日志中以warn等级记录
LogError << "some error message"; // 在日志中以error等级记录
LogCritical << "some critical message"; // 在日志中以critical等级记录
#define LogInfo
Predefined macro to use Logger::Info method.
Definition Logger.h:410
#define LogDebug
Predefined macro to use Logger::Debug method.
Definition Logger.h:401
#define LogCritical
Predefined macro to use Logger::Critical method.
Definition Logger.h:437
#define LogError
Predefined macro to use Logger::Error method.
Definition Logger.h:428
#define LogWarn
Predefined macro to use Logger::Warn method.
Definition Logger.h:419

其他使用方法请查看Logger.h中的注释说明