Back to Index

FileLogger

Inherits from: LoggerTaskSignalSlot

Contents

The FileLogger class extends the Logger class to provide persistent file-based logging with advanced features like log rotation, formatting options, and log level filtering. It integrates seamlessly with the Task framework's signal-slot system while adding the ability to write log messages to files with configurable naming patterns, size limits, and rotation policies.

Features

FileLoggerConfig

The FileLoggerConfig structure provides extensive configuration options for the FileLogger:

Log Levels

FileLogger supports different severity levels for log messages:

Level Description Usage
Debug Detailed debugging information Development-time troubleshooting
Info General information about system operation Normal operational logging
Warning Potential issues that aren't critical Conditions that might need attention
Error Error conditions that affect operation Issues that need immediate attention
Fatal Severe errors that may cause program termination Critical failures that prevent execution

Log level filtering allows you to control the verbosity of logging by setting a minimum level. Any messages with a severity below the minimum level are not recorded.

Class Interface

Signals

FileLogger inherits signals from Logger and adds the following:

Signal Type Description Arguments
fileRotated Simple Emitted when a log file is rotated None
fileError Data Emitted when an error occurs during file operations std::string (error message)
log (inherited) Data Information log entries std::string (message)
warn (inherited) Data Warning log entries std::string (message)
error (inherited) Data Error log entries std::string (message)

Usage Examples

Basic FileLogger Setup
Setting Log Levels
Connecting to Task Components
Structured Logging

File Rotation

FileLogger supports automatic log file rotation based on file size limits and can maintain a configurable number of historical log files.

File Rotation Configuration
Rotation Callbacks
Note: When a log file is rotated, FileLogger properly closes the current file, creates a new file with the configured naming pattern, and can optionally delete old log files to maintain the configured maximum number of files.

Thread Safety

The FileLogger implementation is thread-safe and can handle concurrent logging requests from multiple threads:

Multi-threaded Logging

Lifecycle

A FileLogger goes through the following lifecycle:

  1. Construction: Initialize with configuration and optionally set minimum log level
  2. File Initialization: Create log directory if needed and open initial log file
  3. Operation: Log messages to file according to configuration and log level filtering
  4. File Rotation: Rotate log files when size limits are reached
  5. Reconfiguration: Optionally update configuration during runtime
  6. Destruction: Properly close log file and release resources

Best Practices

Performance Note: In high-throughput applications, consider setting flushAfterEachWrite to false and using a higher minimum log level to reduce I/O overhead. For critical sections, you can explicitly call flush() when needed.