当前位置: 首页 > 工具软件 > log4cxx > 使用案例 >

log4cxx 使用代码进行配置

於意蕴
2023-12-01

(1)官网的一个例子

  • #include <log4cxx/logger.h>
    #include <log4cxx/helpers/pool.h>
    #include <log4cxx/basicconfigurator.h>
    #include <log4cxx/fileappender.h>
    #include <log4cxx/simplelayout.h>
    
    int main() {
       log4cxx::FileAppender * fileAppender = new
    log4cxx::FileAppender(log4cxx::LayoutPtr(new log4cxx::SimpleLayout()),
    "logfile", false);
    
       log4cxx::helpers::Pool p;
       fileAppender->activateOptions(p);
    
       log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(fileAppender));
       log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug());
       log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("logger");
    
       LOG4CXX_INFO(logger,"Created FileAppender appender");
    
       return 0;
    }


(2)一个只使用代码的例子dailyrollingfileappender(经过验证)

 

 ostringstream oss;
  oss << getpid();
  string pid= oss.str();
  MDC::put("pid", oss.str()); //使得layout中可以携带进程号信息

 PatternLayoutPtr layout = new PatternLayout();
        string conversionPattern = "[%p] %d %c %M - %m%n";
        layout->setConversionPattern(conversionPattern);
        // creates daily rolling file appender
        DailyRollingFileAppenderPtr rollingAppenderptr = new DailyRollingFileAppender();
        rollingAppenderptr->setFile("/opt/bre/LrSortService/log/LrSortService.log."+pid);
        rollingAppenderptr->setDatePattern("'.'yyyy-MM-dd");
        rollingAppenderptr->setLayout(layout);
        log4cxx::helpers::Pool p;
        rollingAppenderptr->activateOptions(p);

        // configures the root logger
        log4cxx::LoggerPtr logger = log4cxx::Logger::getRootLogger();
        logger->setLevel(log4cxx::Level::getDebug());
        logger->addAppender(rollingAppenderptr);

        LOG4CXX_INFO(logger,"Created FileAppender appender");

(3)一个例子。使用RollingFileAppender和 TimeBasedRollingPolicy(未经验证)

private static void log4jConfig(String actionName) {

    org.apache.log4j.Logger rootLogger = LogManager.getRootLogger();
    RollingFileAppender fileAppender = (RollingFileAppender)rootLogger.getAppender("fileAppender");

    // <param name="FileNamePattern" value="/var/log/Launcher.log.%d{yyyy-MM-dd}.gz"/>
    String currentLogFile = fileAppender.getFile();
    String newLogPattern = currentLogFile.replace(LOG4J_ROLLING_FILE_NAME_TOKEN, actionName);
    fileAppender.setFile(newLogPattern);

    TimeBasedRollingPolicy timeBasedRollingPolicy = (TimeBasedRollingPolicy) fileAppender.getRollingPolicy();
    String fileNamePattern = timeBasedRollingPolicy.getFileNamePattern();
    String newFileNamePattern = fileNamePattern.replace(LOG4J_ROLLING_FILE_NAME_TOKEN, actionName);;
    timeBasedRollingPolicy.setFileNamePattern(newFileNamePattern);
    timeBasedRollingPolicy.activateOptions();

    fileAppender.activateOptions();

    LOG.info("  Redirected launcher log output to log pattern: " + newFileNamePattern);
}
(4)一个使用xml配置文件和代码协同工作的例子。(有问题)

参考:http://stackoverflow.com/questions/29769018/log4cxx-timebasedrollingpolicy-wont-keep-old-log-when-used-programtically

i'm trying use log4cxx combined with xml file and code in multiprocess, hope each process create a log and can rollback in TimeBasedRollingPolicy. here is my xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="bfd_recommend_appender" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
      <param name="FileNamePattern" value="/opt/bre/LrSortService/log/LrSortService.log.%d{yyyyMMdd}" />
    </rollingPolicy>

    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="[%-5p %d (%F:%L:%c) @ %X{pid}] - %m%n" />
    </layout>
  </appender>
  <!-- Specify the level for some specific categories -->
  <root>
    <level value="debug" />
    <appender-ref ref="bfd_recommend_appender"/>
  </root>
</log4j:configuration>

then my code:

void LrSortService::InitLog(const std::string& logconf) {
  ostringstream oss;
  oss << getpid();
  MDC::put("pid", oss.str());
  DOMConfigurator::configure(logconf);
  LoggerPtr logger(log4cxx::Logger::getRootLogger());
  FileAppenderPtr oldappenderptr;
  oldappenderptr = logger->getAppender("bfd_recommend_appender");
    string oldfile = oldappenderptr->getFile();
    string pid = oss.str();
    string newfile1 = oldfile + ".";
    string newfile  = newfile1 + pid;
    oldappenderptr->setFile(newfile);
    log4cxx::helpers::Pool p;
    oldappenderptr->activateOptions(p);
  cout<<"set log OK , log filename is: "<<newfile<<endl;
}

what i got:

-rw-rw-r-- 1 bre bre  1809500 Apr 20 23:59 LrSortService.log.20150420
-rw-rw-r-- 1 bre bre 26807477 Apr 21 17:37 LrSortService.log.20150420.2297
-rw-rw-r-- 1 bre bre 23429289 Apr 21 17:37 LrSortService.log.20150420.2374
-rw-rw-r-- 1 bre bre 23546280 Apr 21 17:37 LrSortService.log.20150420.2451
-rw-rw-r-- 1 bre bre 22566813 Apr 21 17:37 LrSortService.log.20150420.2528

and yesterday i got:

  LrSortService.log.20150420.2297
    LrSortService.log.20150420.2374
    LrSortService.log.20150420.2451
    LrSortService.log.20150420.2528

what i meant to get is (today is 20150421):

LrSortService.log.20150420.2297
    LrSortService.log.20150420.2374
    LrSortService.log.20150420.2451
    LrSortService.log.20150420.2528
    LrSortService.log.20150421.2297
    LrSortService.log.20150421.2374
    LrSortService.log.20150421.2451
    LrSortService.log.20150421.2528

any help will be appreciated ,thanks !


 类似资料: