Logs don't rotate when using `RollingFileAppender`
This is my log4cplus.properties file:
# Root logger option
log4cplus.rootLogger=DEBUG, stdout
# Console
log4cplus.appender.stdout=log4cplus::ConsoleAppender
log4cplus.appender.stdout.layout=log4cplus::PatternLayout
log4cplus.appender.stdout.layout.ConversionPattern=%d{%Y/%m/%d %H:%M:%S}-[%-5p][%c{1}][%t] - %m -[%l]%n
# TEST
log4cplus.logger.test=DEBUG, test
log4cplus.appender.test=log4cplus::RollingFileAppender
log4cplus.appender.test.CreateDirs=true
log4cplus.appender.test.MaxFileSize=3KB
log4cplus.appender.test.MaxBackupIndex=1
log4cplus.appender.test.DatePattern=%Y_%m_%d.%H_%M_%S
log4cplus.appender.test.layout=log4cplus::PatternLayout
log4cplus.appender.test.filePattern=%i.log.zip
log4cplus.appender.test.File=C:\\Program Files\\test\\logs\\test.log
log4cplus.appender.test.layout.ConversionPattern=%D{%Y/%m/%d %H:%M:%S} - [%-5p][%-3t][%c{1}] - %m - [%l]%n
And this is my simple application to test rotating feature:
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>
int
main()
{
log4cplus::initialize();
log4cplus::Logger root = log4cplus::Logger::getRoot();
log4cplus::BasicConfigurator config;
config.configure();
log4cplus::PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT ("C:\\Program Files\\test\\log4cplus.properties"));
log4cplus::Logger testLogger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("test"));
LOG4CPLUS_DEBUG(testLogger , LOG4CPLUS_TEXT("FOO."));
LOG4CPLUS_INFO(testLogger , LOG4CPLUS_TEXT("BAR."));
log4cplus::Logger::shutdown();
return 0;
}
I run the application many times to reach 3KB, but it doesn't rotate file(i don't see any .zip file in log directory).
There is a minimum file size that you cannot go below. It is 200 KiB.
Oh, interesting..
It works but the rotated file name is: test.log.1
I expected it to be zipped and it's name should be like this:
test.log.2021_08_21.10_14_15.zip
RollingFileAppender is only for rolling by size. If you want time based rolling, you would use TimeBasedRollingFileAppender. But TimeBasedRollingFileAppender does not roll by size.
No i don't want time based rolling. I just wanted to have a zipped file.(something like: test.log.zip is enough for now: )
Look at this thread response: https://stackoverflow.com/a/51865893
Seems log4cplus doesn't have rollingPolicy.