c++boostzipboost-logboost-logging

C++ Boost Log: How to use configuration file to rotate and zip logs?


I need to use Boost Logging library to rotate logs and zip the rotated logs, and I want to define it in a configuration file like in here:

# Logging core settings section. May be omitted if no parameters specified within it.
[Core]
DisableLogging=false
Filter="%Severity% > 3"

# Sink settings sections
[Sinks.MySink1]
# Sink destination type
Destination=Console
# Formatter string. Optional, by default only log record message text is written.
Format="<%TimeStamp%> - %Message%"
FileName="%N.log"
RotationSize=1000000000

How can I implement this?

I couldn't find a way to combine rotating and compressing in a configuration file.


Solution

  • It is recommended to use an external service like logrotate to compress log files. Compression is a time-consuming process, and you don't want the logging library to block your application progress while it compresses log files. For this reason Boost.Log does not support compression out of the box.

    If for some reason you still want to perform compression within your process, you can do this by implementing a custom file collector that derives from the collector interface. The collector's store_file method has to perform all actions regarding moving and compressing files, removing the old files, etc. It will be called by the sink backend when it is time to rotate the currently written log file. You will set your collector to the sink backend by calling text_file_backend::set_file_collector method.

    To integrate with settings files, you will need to register a sink factory that will use the parsed settings container to create and configure a file sink with your custom file collector. This is described here.