简体   繁体   中英

Redirect QtDebug to Logstash

Are there best practice to redirecting QtDebug to logstash?

My mobile devices are write log by qDebug() .
I want to aggregate all my logs to logstash then Google big query.

Current my logging is as below.

void SignUpDialog::signUp(QString email, QString password) {
    qDebug() << "singUp " << email

I'm looking for appender for qt such as.

Take a look into the official docs about the QtMsgHandler

You need to install a message handler using qInstallMsgHandler function, and then, redirect it.

#include <QtGlobal>
#include <stdio.h>
#include <stdlib.h>

void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
    QByteArray localMsg = msg.toLocal8Bit();
    switch (type) {
    case QtDebugMsg:
        // Do whatever you need
        break;
    case QtInfoMsg:
        // Do whatever you need
        break;
    ...
    case QtFatalMsg:
        // Do whatever you need
        abort();
    }
}

int main(int argc, char **argv) {
    qInstallMessageHandler(myMessageHandler); 
    QApplication app(argc, argv);
    ...
    return app.exec();
}

To redirect your request you will need to use an existing client or write your own to send the different events to the Logstash API.

I think one solution is to to install message handler in you Qt app main (here with logic to prevent log from growing infinitely, running number and timestamp):

void msgHandler(QtMsgType type, const QMessageLogContext &/*context*/, const QString &msg)
{
    static int i = 0;
    static QString fp = "/tmp/qtapp.log";
    static QFile f(fp);
    if (!f.isOpen()) {
        f.open(QIODevice::WriteOnly | QIODevice::Append);
    }
    static QTextStream ts(&f);
    QString t;
    switch (type) {
    case QtDebugMsg:
        t = QString("Debug (%1): %2").arg(++i, 0, 10).arg(msg);
        break;
    case QtInfoMsg:
        t = QString("Info (%1): %2").arg(++i, 0, 10).arg(msg);
        break;
    case QtWarningMsg:
        t = QString("Warning (%1): %2").arg(++i, 0, 10).arg(msg);
        break;
    case QtCriticalMsg:
        t = QString("Critical (%1): %2").arg(++i, 0, 10).arg(msg);
        break;
    case QtFatalMsg:
        t = QString("Fatal (%1): %2").arg(++i, 0, 10).arg(msg);
        break;
    default:
        break;
    }

    QTime time = QDateTime::currentDateTime().time();
    t = time.toString("hh:mm:ss.zzz") + " " + t;

    ts << t << endl;
    fprintf(stderr, "%s\n", t.toStdString().c_str());

    QString fpo(fp + QString(".old"));
    if (f.size() > 65011712) {
        f.close();
        QFile::remove(fpo);
        QFile::rename(fp, fpo);
    }
}

int main(int argc, char *argv[])
{
    qInstallMessageHandler(msgHandler);

Logstash file input (just googled this, didn't try):

input {
  file {
    path => "/tmp/qtapp.log"
    type => "qtapp-access"
    start_position => "beginning"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM