FileLogger
Inherits from: Logger → Task → SignalSlot
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
- Persistent File Logging: Writes log messages to files for permanent record-keeping
- Log Level Filtering: Controls verbosity by filtering messages based on severity levels
- Automatic File Rotation: Manages log file size by rotating files when they exceed
configured limits
- Customizable Formatting: Configures timestamps, log levels, and other formatting
options
- Signal-Based Notifications: Emits signals for file rotation and error events
- Thread Safety: Safely handles concurrent logging from multiple threads
- Rotation Callbacks: Supports registration of callbacks for when log files are rotated
- Task Integration: Seamlessly connects to other Task components for centralized logging
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
File Rotation
FileLogger supports automatic log file rotation based on file size limits and can maintain a configurable
number of historical log files.
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:
- All file operations are protected by a mutex
- Log message formatting and writing is atomic
- Configuration changes are thread-safe
- Rotation operations are synchronized to prevent interference
Lifecycle
A FileLogger goes through the following lifecycle:
- Construction: Initialize with configuration and optionally set minimum log level
- File Initialization: Create log directory if needed and open initial log file
- Operation: Log messages to file according to configuration and log level filtering
- File Rotation: Rotate log files when size limits are reached
- Reconfiguration: Optionally update configuration during runtime
- Destruction: Properly close log file and release resources
Best Practices
- Log Level Selection: Use appropriate log levels (Debug for development, Warning+ for
production)
- File Sizing: Configure reasonable file size limits based on application logging volume
- Directory Management: Use absolute paths for log directories in production environments
- Signal Connections: Connect FileLogger to all relevant Task components for
comprehensive logging
- Error Handling: Connect to the fileError signal to monitor logging system issues
- Structured Formatting: Consider using JSON or another structured format for
machine-parseable logs
- Regular Rotation: Implement time-based rotation in addition to size-based for
long-running applications
- Performance Tuning: Adjust flushAfterEachWrite based on reliability vs. performance
needs
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.