log4cplus将日志文件名按日期创建

日志文件一般会带上日期,或者创建以日期命名名的文件夹,将日志文件创建于其中。log4cplus网上有不少教程,但是似乎没有人说这种方案。事实上源码里有这个类TimeBasedRollingFileAppender,就满足此需求。

#include <cstdio>
#include <string>
#include <pthread.h>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/fileappender.h>

using namespace std;

static log4cplus::Logger lgPltf = log4cplus::Logger::getInstance("loggerPlatform");

void *threadFun(void *arg) {
        char name[64] = {0};
        snprintf(name, sizeof(name)-1, "loggerMkt.%s", (const char *)arg);
        log4cplus::Logger loggerMkt1 = log4cplus::Logger::getInstance(name);
        //snprintf(name, sizeof(nameAppendFile)-1, "SQFront.%s.log", arg);
        //log4cplus::SharedAppenderPtr apd(new log4cplus::TimeBasedRollingFileAppender(nameAppendFile));
        //std::auto_ptr<log4cplus::Layout> _layout(new log4cplus::TTCCLayout());
        //apd->setLayout(_layout);
        //loggerCallback.addAppender(apd);

        LOG4CPLUS_TRACE_METHOD(loggerMkt1, LOG4CPLUS_TEXT(__FUNCTION__));
        LOG4CPLUS_DEBUG(loggerMkt1, "This is a DEBUG message");
        LOG4CPLUS_INFO(loggerMkt1, "This is a INFO message");
        LOG4CPLUS_WARN(loggerMkt1, "This is a WARN message");
        LOG4CPLUS_ERROR(loggerMkt1, "This is a ERROR message");
        LOG4CPLUS_FATAL(loggerMkt1, "This is a FATAL message");

        return NULL;
}

void printDebug() {
  LOG4CPLUS_TRACE_METHOD(lgPltf, LOG4CPLUS_TEXT(__FUNCTION__));
        LOG4CPLUS_DEBUG(lgPltf, "This is a DEBUG message");
        LOG4CPLUS_INFO(lgPltf, "This is a INFO message");
        LOG4CPLUS_WARN(lgPltf, "This is a WARN message");
        LOG4CPLUS_ERROR(lgPltf, "This is a ERROR message");
        LOG4CPLUS_FATAL(lgPltf, "This is a FATAL message");
}

int main() {
        pthread_t pthread1, pthread2;
        log4cplus::PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("SQFront.log.properties"));
        log4cplus::Logger loggerMktBase = log4cplus::Logger::getInstance("loggerMkt");
        pthread_create(&pthread1, NULL, threadFun, (void *)"tdf");
        pthread_create(&pthread2, NULL, threadFun, (void *)"gta");
        printDebug();
        pthread_join(pthread1, NULL);
        pthread_join(pthread2, NULL);

        return 0;
}

#SQFront.log.properties
---------------------------------------
#这里命名logger对象。我给logger对象的命名是loggerPlatform(表示输出程序基础运行平台的日志)和loggerMkt(表示输出行情业务相关的日志)
#每个logger对象可以使用多个appender对象(这里有apdPlatform,apdMkt)。类似于每次打日志的时候,会输出到多个文件。(ALL是我调试时用的)
log4cplus.logger.loggerPlatform=ALL, apdPlatform
#, apdStdout
log4cplus.logger.loggerMkt=ALL, apdMkt

#对appender对象apdStdout(这里我是调试时用的)进行设置。
#log4cplus.appender.apdStdout=log4cplus::ConsoleAppender
#log4cplus.appender.apdStdout.layout=log4cplus::PatternLayout
#log4cplus.appender.apdStdout.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S.%q} %-5p - %m [%l]%n

#对appender对象apdPlatform进行设置。TimeBasedRollingFileAppender可以按日期转储日志文件
log4cplus.appender.apdPlatform=log4cplus::TimeBasedRollingFileAppender
log4cplus.appender.apdPlatform.FilenamePattern=log/%d{yyyy-MM-dd}/SQFront.log
log4cplus.appender.apdPlatform.Append=true
log4cplus.appender.apdPlatform.MaxHistory=999
log4cplus.appender.apdPlatform.ImmediateFlush=true
log4cplus.appender.apdPlatform.RollOnClose=false
log4cplus.appender.apdPlatform.CreateDirs=true
log4cplus.appender.apdPlatform.layout=log4cplus::PatternLayout
log4cplus.appender.apdPlatform.layout.ConversionPattern=[%T] %D{%Y-%m-%d %H:%M:%S.%q} %-5p - %m [%c]%n

#可不配置。日志文件会按此等级范围显示
#log4cplus.appender.apdRoot.filters.1=log4cplus::spi::LogLevelRangeFilter
#log4cplus.appender.apdRoot.filters.1.LogLevelMin=ERROR
#log4cplus.appender.apdRoot.filters.1.LogLevelMax=FATAL

log4cplus.appender.apdMkt=log4cplus::TimeBasedRollingFileAppender
log4cplus.appender.apdMkt.FilenamePattern=log/%d{yyyy-MM-dd}/SQFront.mkt.log
log4cplus.appender.apdMkt.Append=true
log4cplus.appender.apdMkt.MaxHistory=999
log4cplus.appender.apdMkt.ImmediateFlush=true
log4cplus.appender.apdMkt.RollOnClose=false
log4cplus.appender.apdMkt.CreateDirs=true
log4cplus.appender.apdMkt.layout=log4cplus::PatternLayout
log4cplus.appender.apdMkt.layout.ConversionPattern=[%T] %D{%Y-%m-%d %H:%M:%S.%q} %-5p - %m [%c]%n


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章