Qt5的日志框架相对于Qt4模块加载的来说还是挺方便的,适合刚入门的程序员使用。下面粘贴一下自己写的代码,与君共勉。
///main.cpp
#include "widget.h"
#include <QApplication>
#include <QLoggingCategory>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLoggingCategory::setFilterRules(QStringLiteral("qt.custom.log.debug=false"));
qSetMessagePattern("%{category} %{message}");
Widget w;
QLoggingCategory category("qt.custom.log");
qCDebug(category) << "main a debug message";
w.show();
return a.exec();
}
///Widget.h
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QLoggingCategory>namespace Ui {class Widget;}class Widget : public QWidget{Q_OBJECTpublic:
explicit Widget(QWidget *parent = 0);~Widget();private:
Ui::Widget *ui;};Q_DECLARE_LOGGING_CATEGORY(QT_CUSTOM_LOG)#endif // WIDGET_H
///Widget.cpp
#include "widget.h"#include "ui_widget.h"Q_LOGGING_CATEGORY(QT_CUSTOM_LOG, "qt.custom.log")Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget){ui->setupUi(this);QLoggingCategory custom("construct");qCDebug(custom) << "Widget initialzation ...";}Widget::~Widget(){delete ui;}
///*.pro
QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = CQTLoggerTEMPLATE = app#
SOURCES += main.cpp\widget.cpp
HEADERS += widget.hFORMS += widget.uiQt5 提供的日志框架可以对日志进行分类,外部文件配置日志选项等其它功能。