Question

I'm using log4cplus in my project to do logging.

I created logger.conf and I will load it in the beginning of my application.

This is my logger.conf:

log4cplus.appender.Developer=log4cplus::RollingFileAppender
log4cplus.appender.Developer.DatePattern = ".yyyy-MM-dd"
log4cplus.appender.Developer.Schedule = HOURLY
log4cplus.appender.Developer.File=log/developer.log
log4cplus.appender.Developer.MaxFileSize=3MB
log4cplus.appender.Developer.MaxBackupIndex=10
log4cplus.appender.Developer.layout=log4cplus::PatternLayout
log4cplus.appender.Developer.layout.ContextPrinting=enabled
log4cplus.appender.Developer.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S,%Q} [%t] %p - %m%n 
log4cplus.appender.Developer.Threshold=TRACE
log4cplus.logger.DEVELOPER=TRACE, Developer

This is how I load my logger.conf:

QString log_path = qApp->applicationDirPath() + "/log";
QDir().mkpath(log_path);
PropertyConfigurator logger(L"configs/logger.conf", Logger::getDefaultHierarchy());
logger.configure();

And whenever I want to log, I use the following line:

Logger::getInstance(L"DEVELOPER").log(INFO_LOG_LEVEL, L"..............");

I'd like to know two things:

  1. How can I tell Log4Cplus to compress the backup logs?

    In some post I saw this reply:

    I need to create my own appender, inheriting from RollingFileAppender and then add a compression steps.

    If it's possible, can anyone tell me how to do it, please? I don't know how to implement this.

  2. How can I add a pattern to the name of these backup logs?

    At the moment, Log4Cplus makes my back up like this:

    developer.log.1
    developer.log.2
    developer.log.3
    ...
    

    I'd like to add date and time to it.

Was it helpful?

Solution

You will need to implement you own Appender like this:

class NewFileAppender : public ::log4cplus::RollingFileAppender
{

void 
newFileAppender::rollover()
{
    helpers::LogLog & loglog = getLogLog();

    // Close the current file
    out.close();
    out.clear(); // reset flags since the C++ standard specified that all the
                 // flags should remain unchanged on a close

    // If maxBackups <= 0, then there is no file renaming to be done.
    if (maxBackupIndex > 0)
    {
        rolloverCompressedFiles(filename, maxBackupIndex);

        // Rename fileName to fileName.DATE
        tstring target = filename + DATE;

        int ret;

        ret = file_rename (filename, target);
        //TODO: compress using zlib
    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top