Logger is a simple way to write the history of your application lifecycle to any target logging device (which is called Appender and may write to any target you will implement with it: console, text file, XML or something - you choose) and to map logging message to a class, function, source file and line of code which it is called from.
Some simple appenders (which may be considered an examples) are provided with the Logger itself: see ConsoleAppender and FileAppender documentation.
It supports using it in a multithreaded applications, so all of its functions are thread safe.
Simple usage example:
#include <QCoreApplication>
#include <ConsoleAppender.h>
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
...
consoleAppender->
setFormat(
"[%{type:-7}] <%{Function}> %{message}\n");
...
int result = app.exec();
...
if (result)
LOG_WARNING() <<
"Something went wrong." <<
"Result code is" << result;
return result;
}
A file containing the description of Logger class and and additional useful macros for logging.
#define cuteLogger
Macro returning the current instance of Logger object.
Definition Logger.h:30
#define LOG_WARNING
Write the warning log record.
Definition Logger.h:36
#define LOG_INFO
Writes the info log record.
Definition Logger.h:35
void setFormat(const QString &)
Sets the logging format for writing strings to the log target with this appender.
Definition AbstractStringAppender.cpp:116
ConsoleAppender is the simple appender that writes the log records to the std::cerr output stream.
Definition ConsoleAppender.h:22
Logger internally uses the lazy-initialized singleton object and needs no definite initialization, but you may consider registering a log appender before calling any log recording functions or macros.
The library design of Logger allows you to simply mass-replace all occurrences of qDebug and similar calls with similar Logger macros (e.g. LOG_DEBUG())
- Note
- Logger uses a singleton global instance which lives through all the application life cycle and self-destroys destruction of the QCoreApplication (or QApplication) instance. It needs a QCoreApplication instance to be created before any of the Logger's functions are called.
- See also
- cuteLogger
-
LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL
-
LOG_CTRACE, LOG_CDEBUG, LOG_CINFO, LOG_CWARNING, LOG_CERROR, LOG_CFATAL
-
LOG_ASSERT
-
LOG_TRACE_TIME, LOG_DEBUG_TIME, LOG_INFO_TIME
-
AbstractAppender