QtWebApp
stefanfrings::FileLogger Class Reference

Logger that uses a text file for output. More...

#include <filelogger.h>

Inheritance diagram for stefanfrings::FileLogger:
Collaboration diagram for stefanfrings::FileLogger:

Public Member Functions

 FileLogger (QSettings *settings, const int refreshInterval=10000, QObject *parent=nullptr)
 Constructor. More...
 
virtual ~FileLogger ()
 Destructor. More...
 
virtual void write (const LogMessage *logMessage)
 Write a message to the log file. More...
 
- Public Member Functions inherited from stefanfrings::Logger
 Logger (QObject *parent)
 Constructor. More...
 
 Logger (const QString msgFormat="{timestamp} {type} {msg}", const QString timestampFormat="dd.MM.yyyy hh:mm:ss.zzz", const QtMsgType minLevel=QtDebugMsg, const int bufferSize=0, QObject *parent=nullptr)
 Constructor. More...
 
virtual ~Logger ()
 Destructor. More...
 
virtual void log (const QtMsgType type, const QString &message, const QString &file="", const QString &function="", const int line=0)
 Decorate and log the message, if type>=minLevel. More...
 
void installMsgHandler ()
 Installs this logger as the default message handler, so it can be used through the global static logging functions (e.g. More...
 
virtual void clear (const bool buffer=true, const bool variables=true)
 Clear the thread-local data of the current thread. More...
 

Protected Member Functions

void timerEvent (QTimerEvent *event)
 Handler for timer events. More...
 
virtual void write (const LogMessage *logMessage)
 Decorate and write a log message to stderr. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from stefanfrings::Logger
static void set (const QString &name, const QString &value)
 Sets a thread-local variable that may be used to decorate log messages. More...
 
- Protected Attributes inherited from stefanfrings::Logger
QString msgFormat
 Format string for message decoration. More...
 
QString timestampFormat
 Format string of timestamps. More...
 
QtMsgType minLevel
 Minimum level of message types that are written out directly or trigger writing the buffered content. More...
 
int bufferSize
 Size of backtrace buffer, number of messages per thread. More...
 
- Static Protected Attributes inherited from stefanfrings::Logger
static QMutex mutex
 Used to synchronize access of concurrent threads. More...
 

Detailed Description

Logger that uses a text file for output.

Settings are read from a config file using a QSettings object. Config settings can be changed at runtime.

Example for the configuration settings:

fileName=logs/QtWebApp.log
maxSize=1000000
maxBackups=2
bufferSize=0
minLevel=WARNING
msgformat={timestamp} {typeNr} {type} thread={thread}: {msg}
timestampFormat=dd.MM.yyyy hh:mm:ss.zzz  

  • Possible log levels are: ALL/DEBUG=0, INFO=4, WARN/WARNING=1, ERROR/CRITICAL=2, FATAL=3
  • fileName is the name of the log file, relative to the directory of the settings file. In case of windows, if the settings are in the registry, the path is relative to the current working directory.
  • maxSize is the maximum size of that file in bytes. The file will be backed up and replaced by a new file if it becomes larger than this limit. Please note that the actual file size may become a little bit larger than this limit. Default is 0=unlimited.
  • maxBackups defines the number of backup files to keep. Default is 0=unlimited.
  • bufferSize defines the size of the ring buffer. Default is 0=disabled.
  • minLevel If bufferSize=0: Messages with lower level are discarded.
    If buffersize>0: Messages with lower level are buffered, messages with equal or higher level (except INFO) trigger writing the buffered messages into the file.
    Defaults is 0=debug.
  • msgFormat defines the decoration of log messages, see LogMessage class. Default is "{timestamp} {type} {msg}".
  • timestampFormat defines the format of timestamps, see QDateTime::toString(). Default is "yyyy-MM-dd hh:mm:ss.zzz".
See also
set() describes how to set logger variables
LogMessage for a description of the message decoration.
Logger for a descrition of the buffer.

Definition at line 56 of file filelogger.h.

Constructor & Destructor Documentation

◆ FileLogger()

FileLogger::FileLogger ( QSettings *  settings,
const int  refreshInterval = 10000,
QObject *  parent = nullptr 
)

Constructor.

Parameters
settingsConfiguration settings, usually stored in an INI file. Must not be 0. Settings are read from the current group, so the caller must have called settings->beginGroup(). Because the group must not change during runtime, it is recommended to provide a separate QSettings instance that is not used by other parts of the program. The FileLogger does not take over ownership of the QSettings instance, so the caller should destroy it during shutdown.
refreshIntervalInterval of checking for changed config settings in msec, or 0=disabled
parentParent object

Definition at line 78 of file filelogger.cpp.

◆ ~FileLogger()

FileLogger::~FileLogger ( )
virtual

Destructor.

Closes the file.

Definition at line 94 of file filelogger.cpp.

Member Function Documentation

◆ timerEvent()

void FileLogger::timerEvent ( QTimerEvent *  event)
protected

Handler for timer events.

Refreshes config settings or synchronizes I/O buffer, depending on the event. This method is thread-safe.

Parameters
eventused to distinguish between the two timers.

Definition at line 193 of file filelogger.cpp.

◆ write()

void FileLogger::write ( const LogMessage logMessage)
virtual

Write a message to the log file.

Reimplemented from stefanfrings::Logger.

Definition at line 100 of file filelogger.cpp.


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