使用Qt C++封装设计一个spdlog日志系统,要求功能齐全,在打印到控制台的内容要详细具体到那个文件哪一行,颜色区分,可自由选择打印到控制台还是打印到日志文件,也可以同时打印,程序运行时每隔2秒刷新一次日志文件,保证日志文件的实时性。并且限制文件的大小和数量达到自动轮换效果
时间: 2024-02-18 10:01:56 浏览: 133
基于Qt环境下开发的日志log打印
以下是一个使用Qt C++封装的spdlog日志系统的示例代码:
```cpp
#include <QObject>
#include <QTimer>
#include <QString>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
class QtSpdLogger : public QObject
{
Q_OBJECT
public:
explicit QtSpdLogger(QObject *parent = nullptr, QString logFileName = "example.log",
qint64 logFileSize = 1048576, int maxLogFiles = 5);
~QtSpdLogger();
void logDebug(QString message);
void logInfo(QString message);
void logWarn(QString message);
void logError(QString message);
private:
std::shared_ptr<spdlog::logger> m_logger;
QTimer m_timer;
QString m_logFileName;
qint64 m_logFileSize;
int m_maxLogFiles;
void initLogger();
void rotateLogFiles();
};
QtSpdLogger::QtSpdLogger(QObject *parent, QString logFileName, qint64 logFileSize, int maxLogFiles) :
QObject(parent),
m_logFileName(logFileName),
m_logFileSize(logFileSize),
m_maxLogFiles(maxLogFiles)
{
initLogger();
connect(&m_timer, &QTimer::timeout, this, &QtSpdLogger::rotateLogFiles);
m_timer.start(2000);
}
QtSpdLogger::~QtSpdLogger()
{
m_logger->flush();
}
void QtSpdLogger::initLogger()
{
// Create console sink with color
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
consoleSink->set_level(spdlog::level::trace);
// Create file sink
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(m_logFileName.toStdString(),
m_logFileSize, m_maxLogFiles);
fileSink->set_level(spdlog::level::trace);
// Create combined logger with console and file sink
spdlog::sinks_init_list sinkList = { consoleSink, fileSink };
m_logger = std::make_shared<spdlog::logger>("logger", sinkList);
m_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%L%$] [%s:%#] %v");
m_logger->set_level(spdlog::level::trace);
}
void QtSpdLogger::logDebug(QString message)
{
m_logger->debug(message.toStdString());
}
void QtSpdLogger::logInfo(QString message)
{
m_logger->info(message.toStdString());
}
void QtSpdLogger::logWarn(QString message)
{
m_logger->warn(message.toStdString());
}
void QtSpdLogger::logError(QString message)
{
m_logger->error(message.toStdString());
}
void QtSpdLogger::rotateLogFiles()
{
m_logger->flush();
spdlog::rotating_logger_mt("logger", m_logFileName.toStdString(), m_logFileSize, m_maxLogFiles);
}
```
使用该日志系统的示例代码:
```cpp
#include <QCoreApplication>
#include "QtSpdLogger.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QtSpdLogger logger(nullptr, "example.log", 1048576, 5);
logger.logDebug("This is a debug message.");
logger.logInfo("This is an info message.");
logger.logWarn("This is a warning message.");
logger.logError("This is an error message.");
return a.exec();
}
```
该日志系统将在控制台和日志文件中输出相同的日志信息,控制台的日志信息将使用颜色进行区分,日志文件将每隔2秒钟刷新一次,同时受限制的文件大小和数量将自动轮换。日志信息的格式如下:
```
[2021-07-27 11:05:19.327] [DEBUG] [main.cpp:12] This is a debug message.
[2021-07-27 11:05:19.327] [INFO] [main.cpp:13] This is an info message.
[2021-07-27 11:05:19.327] [WARN] [main.cpp:14] This is a warning message.
[2021-07-27 11:05:19.327] [ERROR] [main.cpp:15] This is an error message.
```
阅读全文