Qt+QtWebApp開發筆記(二):http伺服器日誌系統介紹、新增日誌系統至Demo測試
前言
Demo
下載地址
日誌系統
生產環境需要檢視舊的日誌訊息,例如兩天前的日誌訊息。
include(../QtWebApp/QtWebApp/logging/logging.pri)
[logging]minLevel=WARNING bufferSize=100fileName=../logs/webapp1.log maxSize=1000000maxBackups=2timestampFormat=dd.MM.yyyy hh:mm:ss.zzz msgFormat={timestamp} {typeNr} {type} {thread} {msg}
{file}:Filename of source code where the message was generated{function}:Function where the message was generated{line}:Line number where the message was generated
qCritical("An error occured in {file}: out of disk space");
#include "httpsessionstore.h"#include "staticfilecontroller.h"#include "templatecache.h"#include "filelogger.h"using namespace stefanfrings;/** Storage for session cookies */extern HttpSessionStore* sessionStore;/** Controller for static files */extern StaticFileController* staticFileController;/** Cache for template files */extern TemplateCache* templateCache;/** Redirects log messages to a file */extern FileLogger* logger;#endif // GLOBAL_H
#include "global.h"HttpSessionStore* sessionStore;StaticFileController* staticFileController;TemplateCache* templateCache;FileLogger* logger;
int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); QString configFileName=searchConfigFile(); // Configure logging QSettings* logSettings=new QSettings(configFileName,QSettings::IniFormat,&app); logSettings->beginGroup("logging"); logger=new FileLogger(logSettings,10000,&app); logger->installMsgHandler(); // Log the library version qDebug("QtWebApp has version %s",getQtWebAppLibVersion()); // Session store QSettings* sessionSettings=new QSettings(configFileName,QSettings::IniFormat,&app); sessionSettings->beginGroup("sessions"); sessionStore=new HttpSessionStore(sessionSettings,&app); // Static file controller QSettings* fileSettings=new QSettings(configFileName,QSettings::IniFormat,&app); fileSettings->beginGroup("files"); staticFileController=new StaticFileController(fileSettings,&app); // Configure template cache QSettings* templateSettings=new QSettings(configFileName,QSettings::IniFormat,&app); templateSettings->beginGroup("templates"); templateCache=new TemplateCache(templateSettings,&app); // HTTP server QSettings* listenerSettings=new QSettings(configFileName,QSettings::IniFormat,&app); listenerSettings->beginGroup("listener"); new HttpListener(listenerSettings,new RequestMapper(&app),&app); return app.exec();}
記錄器變數
void RequestMapper::service(HttpRequest& request, HttpResponse& response) { QByteArray path=request.getPath(); qDebug("RequestMapper: path=%s",path.data()); HttpSession session=sessionStore->getSession(request,response,false); QString username=session.get("username").toString(); logger->set("currentUser",username); ...}
msgFormat={timestamp} {typeNr} {type} {thread} User:{currentUser} {msg}
日誌緩衝區和執行緒池
void RequestMapper::service(HttpRequest& request, HttpResponse& response) { ... else { response.setStatus(404,"Not found"); response.write("The URL is wrong, no such document."); } qDebug("RequestMapper: finished request"); logger->clear(true,true);}
雙檔案記錄器
minLevel=INFObufferSize=0
minLevel=ERROR (or WARNING)bufferSize=100
總結
Demo增量:新增logging日誌模組
步驟一:準備程式碼模板
步驟二:複製logging模組
# logging模組,QtWebApp自帶的三方模組include ($$PWD/modules/logging/logging.pri)
步驟三:新增配置logging的配置檔案
步驟四:新增logging日誌程式碼
步驟五:執行結果
步驟六:日誌配置調整
Demo原始碼
HttpServerManager.h
#ifndef HTTPSERVERMANAGER_H#define HTTPSERVERMANAGER_H#include <QObject>#include <QMutex>#include "httplistener.h"#include "filelogger.h"#include "HelloWorldRequestHandler.h"class HttpServerManager : public QObject{ Q_OBJECTprivate: explicit HttpServerManager(QObject *parent = 0);public: static HttpServerManager *getInstance();public slots: void slot_start(); // 開啟執行緒 void slot_stop(); // 停止執行緒private: static HttpServerManager *_pInstance; static QMutex _mutex;private: bool _running; // 執行狀態private: HttpListener *_pHttpListener; // http服務監聽器 QSettings *_pHttpListenerSettings; // http伺服器配置檔案 FileLogger *_pFileLogger; // 日誌記錄 QSettings *_pFileLoggerSettings; // 日誌配置檔案private: QString _ip; // 伺服器監聽ip(若為空,則表示監聽所有ip) quint16 _port; // 伺服器監聽埠 int _minThreads; // 空閒最小執行緒數 int _maxThreads; // 負載最大執行緒數 int _cleanupInterval; // 空執行緒清空間隔(單位:毫秒) int _readTimeout; // 保持連線空載超時時間(單位:毫秒) int _maxRequestSize; // 最大請求數 int _maxMultiPartSize; // 上載檔案最大數(單位:位元組)};#endif // HTTPSERVERMANAGER_H
HttpServerManager.cpp
#include "HttpServerManager.h"#include <QApplication>#include <QDir>#include <QDebug>#include <QDateTime>//#define LOG qDebug()<<__FILE__<<__LINE__//#define LOG qDebug()<<__FILE__<<__LINE__<<__FUNCTION__//#define LOG qDebug()<<__FILE__<<__LINE__<<QThread()::currentThread()//#define LOG qDebug()<<__FILE__<<__LINE__<<QDateTime::currentDateTime().toString("yyyy-MM-dd")#define LOG qDebug()<<__FILE__<<__LINE__<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")HttpServerManager *HttpServerManager::_pInstance = 0;QMutex HttpServerManager::_mutex;HttpServerManager::HttpServerManager(QObject *parent) : QObject(parent), _pHttpListener(0), _pHttpListenerSettings(0), _pFileLogger(0), _pFileLoggerSettings(0), _running(false), _port(8088), _minThreads(2), _maxThreads(10), _cleanupInterval(60000), _readTimeout(60000), _maxRequestSize(100), _maxMultiPartSize(1024*1024*1024){}HttpServerManager *HttpServerManager::getInstance(){ if(!_pInstance) { QMutexLocker lock(&_mutex); if(!_pInstance) { _pInstance = new HttpServerManager(); } } return _pInstance;}void HttpServerManager::slot_start(){ if(_running) { LOG << "It's running!!!"; return; } _running = true; LOG << "Succeed to run"; QString httpServerPath = QString("%1/etc/httpServer.ini").arg(qApp->applicationDirPath()); LOG << httpServerPath << "exit:" << QFile::exists(httpServerPath); // 啟動日誌幾里路 { if(!_pFileLoggerSettings) { _pFileLoggerSettings = new QSettings(httpServerPath, QSettings::IniFormat); } _pFileLoggerSettings->beginGroup("logging"); // 日誌不會主動建立資料夾,這裡需要補全 { QFileInfo fileInfo(httpServerPath); QString dirPath = fileInfo.dir().absolutePath(); dirPath = QString("%1/%2") .arg(dirPath) .arg(_pFileLoggerSettings->value("fileName").toString()); dirPath = dirPath.mid(0, dirPath.lastIndexOf("/")); QDir dir; dir.mkpath(dirPath); } _pFileLogger = new FileLogger(_pFileLoggerSettings); _pFileLogger->installMsgHandler(); } // 啟動http的監聽 { if(!_pHttpListenerSettings) { _pHttpListenerSettings = new QSettings(httpServerPath, QSettings::IniFormat); } _pHttpListenerSettings->beginGroup("listener"); _pHttpListener = new HttpListener(_pHttpListenerSettings, new HelloWorldRequestHandler); } LOG;}void HttpServerManager::slot_stop(){ if(!_running) { LOG <<"It's not running!!!"; return; } _running = false; LOG << "Succeed to stop";}
工程模板v1.1.0
入坑
入坑一:日誌一直不出來
問題
原因
解決
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70010283/viewspace-2953389/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 分散式日誌傳輸系統Databus(一)--系統介紹分散式
- 日誌系統
- docker筆記48-部署EFK日誌系統Docker筆記
- Linux系統級日誌系統Linux
- AIX系統日誌AI
- Rsyslog日誌系統
- Qt+QtWebApp開發筆記(一):QtWebApp介紹、下載和搭建基礎封裝http輕量級伺服器DemoQTWebAPP筆記封裝HTTP伺服器
- 日誌收集系統PLG(Promtail+Loki+Grafana)介紹及部署AILokiGrafana
- X-Admin&ABP框架開發-系統日誌框架
- 日誌審計系統
- ELK日誌分析系統
- 日誌系統相關
- ElasticSearch實戰系列九: ELK日誌系統介紹和安裝Elasticsearch
- Kubernetes日誌系統新貴Loki-StackLoki
- Windows系統搭建ELK日誌收集伺服器Windows伺服器
- ELK-日誌分析系統
- 檢視系統的日誌
- windows10系統怎麼刪系統日誌_win10如何徹底刪除系統日誌WindowsWin10
- Win10系統日誌怎麼檢視_win10系統日誌在哪裡Win10
- 日誌: 分散式系統的核心分散式
- Centos部署Loki日誌聚合系統CentOSLoki
- ELK+FileBeat日誌分析系統
- 部署Sentry日誌監控系統
- MySQL 日誌系統 redo log、binlogMySql
- mysql日誌系統簡單使用MySql
- ELK日誌分析系統詳解
- ELK日誌系統之通用應用程式日誌接入方案
- 開源元件ELK日誌系統配置與管理元件
- 日誌篇:模組日誌總體介紹
- linux系統伺服器,nginx日誌切割儲存教程。Linux伺服器Nginx
- 基於AWS-ELK部署系統日誌告警系統
- SpringBoot開發案例構建分散式日誌處理系統Spring Boot分散式
- 結構化日誌記錄 - 更好地理解系統
- ELK日誌系統之使用Rsyslog快速方便的收集Nginx日誌Nginx
- FLOWERS開發日誌(二)
- Rust 日誌系統實踐總結Rust
- Linux檔案系統與日誌分析Linux
- Elasticsearch+Kibana日誌分析系統搭建Elasticsearch