Qt HTTP Server ロギング

Qt HTTP ServerQLoggingCategory クラスを使ってログを記録します。"qt.httpserver" で始まるロギングカテゴリは、Qt HTTP Server のさまざまな部分で使用されます。これらはQLoggingCategory で説明されているように、有効にしたり無効にしたりすることができます。

ログの有効/無効を動的に切り替えるには、QLoggingCategory::setFilterRules() を呼び出します。以下のように、QHttpServer::route() 関数を使用することで、フィルタールールを変更するための URL をサーバーに追加できます。

#include <QCoreApplication>
#include <QHttpServer>
#include <QLoggingCategory>

int main(int argc, char** argv)
{
    QCoreApplication app(argc, argv);
    QHttpServer server;
    auto tcpserver = std::make_unique<QTcpServer>();
    if (!tcpserver->listen(QHostAddress::LocalHost, 8000) || !server.bind(tcpserver.get()))
        return -1;
    tcpserver.release();

    server.route("/loggingFilter", [] (const QHttpServerRequest &request) {
        QString filter;
        QTextStream result(&filter);
        for (auto pair : request.query().queryItems()) {
            if (!filter.isEmpty())
                result << "\n";
            result << pair.first << "=" << pair.second;
        }
        QLoggingCategory::setFilterRules(filter);
        return filter;
    });

    return app.exec();
}

フィルタ・ルールは、"http://127.0.0.1:8000/loggingFilter?qt.httpserver=true&appname.access=true" を使用して設定できます。この場合、すべてのQt HTTP Server ロギングが有効になり、さらに仮説的なロギングカテゴリーappname.access が有効になります。

QLoggingCategoryQHttpServerも参照してください

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.