将日志写入其他文件夹

时间:2019-07-05 08:30:16

标签: c++ log4j log4cxx

我想每分钟将日志记录到csv文件中,这些csv文件需要保存在不同的文件夹中,例如,第一个小时的文件夹中有60分钟的日志文件,而下一个小时的文件夹中又需要60分钟。 / p>

当我使用DailyRollingFileAppender时,在完成特定的小时后会导致异常,直到异常发生为止,文件才能正常创建。 如果我从12:14开始记录日志并在12:59之前正常运行,则会发生异常。

#include <unistd.h>
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>

using namespace log4cxx;
using namespace log4cxx::helpers;

// Define static logger variable
LoggerPtr loggerMyMain(Logger::getLogger(""));
LoggerPtr loggerFunctionA(Logger::getLogger(""));

void functionA() {
    LOG4CXX_INFO(loggerFunctionA, "Executing functionA.");
    usleep(2000);
}

int main() {
    int value = 5;

    // Load properties style configuration file using PropertyConfigurator
    PropertyConfigurator::configure("log4j.properties");
    for(int j = 1; j < 10000; j++) {
        LOG4CXX_TRACE(
            loggerMyMain,
            "this is a debug message for detailed code discovery. Value=" << j);
        LOG4CXX_DEBUG(loggerMyMain, "this is a debug message.");
        LOG4CXX_INFO(loggerMyMain, "this is a info message, ignore. Value=" << j);
        LOG4CXX_WARN(loggerMyMain, "this is a warn message, not too bad.");
        LOG4CXX_ERROR(loggerMyMain,
                      "this is a error message, something serious is happening.");
        LOG4CXX_FATAL(loggerMyMain, "this is a fatal message!!!");

        functionA();
        if(j > 9998) j = 1;
    }
    return 0;
}

配置文件log4j.properties

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender

# Set the name of the file
#log4j.appender.FILE.File=${'folder'/}

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=false

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, should not overwrite
log4j.appender.FILE.Append=false

# Set the DatePattern
log4j.appender.FILE.DatePattern=yyyy-MM/yyyy-MM-dd/yyyy-MM-dd-HH/yyyy-MM-dd_HH:mm'.''csv'

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

输出为:

 g++ file.cpp  -lapr-1 -laprutil-1 -llog4cxx -o kk
 kk@deb:~/logs/logcfg$ ./kk
 log4cxx: Exception during rollover

预期的输出结构

   day01-|
         |----hour1 |----min1.csv
         |          |----min2.csv
         |          |----min60.csv
         |----hour-N
                |----min1.csv
                |----min60.csv

0 个答案:

没有答案
相关问题