QLogger
Loading...
Searching...
No Matches
QLogger::LogFormatter Class Reference

Use to format log. More...

Public Types

using CbFormatter = std::function<QString(const LogEntry &log)>
 Callback used when formatting log message.
 

Static Public Member Functions

static LogBinary format (const LogEntry &log)
 
static void setCustomFormat (CbFormatter formatter)
 Register a custom formatter.
 

Detailed Description

Use to format log.

This class allow to format QLogger::LogEntry to a QLogger::LogBinary using a custom formatter.
By default, log entries will be formatted to:

  • Without context informations: [utc-date][type-log] log-msg
  • With context informations: [utc-date][type-log] log-msg (cpp-file:cpp-line, cpp-function)

For more details about context informations, please refer to QLogger::LogEntry

To use a custom formatter, use setCustomFormat()

Member Typedef Documentation

◆ CbFormatter

using QLogger::LogFormatter::CbFormatter = std::function<QString(const LogEntry &log)>

Callback used when formatting log message.

We can register a custom callback format by using:

#include "qlogger/qloggerfactory.h"
/*****************************/
/* Custom log formatter */
/*****************************/
QString logFormatter(const QLogger::LogEntry &log)
{
QString fmt = QString("[%1][%2] %3").arg(
QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs),
log.getMsg()
);
/* Can we add context informations ? */
if(log.contextIsAvailable()){
fmt += QString(" (%1:%2, %3)")
.arg(log.getCtxFile().fileName())
.arg(log.getCtxLine())
.arg(log.getCtxFctName());
}
/* Terminate log line */
fmt += '\n';
return fmt;
}
/*****************************/
/* Main method */
/*****************************/
int main(int argc, char *argv[])
{
/* Set logs */
QLogger::LogFormatter::setCustomFormat(logFormatter); // Set it before choosing sink
QLogger::QLoggerFactory::instance().initChoosenSink();
/* Manage application properties */
[...]
return app.exec();
}
Used to store all log entry informations.
Definition logentry.h:13
QString getTypeString() const
Get string type.
Definition logentry.cpp:88
const QString & getMsg() const
Get log message.
Definition logentry.cpp:221
bool contextIsAvailable() const
Use to know if context informations are available.
Definition logentry.cpp:110
QString getCtxFctName() const
Get function name.
Definition logentry.cpp:200
int getCtxLine() const
Get line of source code associated to log entry.
Definition logentry.cpp:146
const QFileInfo & getCtxFile() const
Get file of source code associated to log entry.
Definition logentry.cpp:128
static void setCustomFormat(CbFormatter formatter)
Register a custom formatter.
Definition logformatter.cpp:79
static QLoggerFactory & instance()
Use to get current unique instance of QLoggerFactory.
Definition qloggerfactory.cpp:155
Parameters
[in]logEntryLog entry to format.
See also
setCbStarted()
startDownload()

Member Function Documentation

◆ setCustomFormat()

void QLogger::LogFormatter::setCustomFormat ( CbFormatter formatter)
static

Register a custom formatter.

Parameters
formatterFormatter to use.

The documentation for this class was generated from the following files: