Log to different file with log4cxx

后端 未结 4 1139
南笙
南笙 2021-01-18 19:22

I want to log to different files in my code.

How can i do that in Log4cxx with xml configuration or programatically in code...

  • Suppose that I have 1.k,
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-18 19:39

    You do need to create separate appenders for you loggers. In my example i've created afile1 and afile2 appenders. I have also created two loggers : my.logger1 and my.logger2. When you use my.logger1 it logs to mylog1 file, When you use my.logger2 it logs to mylog2 file.

    Here is my log.properties file:

    log4cplus.appender.afile1.layout=log4cplus::PatternLayout
    log4cplus.appender.afile1.layout.ConversionPattern=%d [ %c{1} ] [ %-5p ] %m%n
    log4cplus.appender.afile1=log4cplus::RollingFileAppender
    log4cplus.appender.afile1.File=mylog1.log
    log4cplus.appender.afile1.MaxFileSize=5MB
    log4cplus.appender.afile1.MaxBackupIndex=2
    
    log4cplus.appender.afile2.layout=log4cplus::PatternLayout
    log4cplus.appender.afile2.layout.ConversionPattern=%d [ %c{1} ] [ %-5p ] %m%n
    log4cplus.appender.afile2=log4cplus::RollingFileAppender
    log4cplus.appender.afile2.File=mylog2.log
    log4cplus.appender.afile2.MaxFileSize=5MB
    log4cplus.appender.afile2.MaxBackupIndex=2
    
    log4cplus.logger.my.logger1=INHERIT, afile1
    log4cplus.additivity.my.logger1=false
    log4cplus.logger.my.logger2=INHERIT, afile2
    log4cplus.additivity.my.logger2=false 
    

    Here is an example programm:
    example.cpp:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    #define MYLOG1_INFO(logEvent) LOG4CPLUS_INFO (log4cplus::Logger::getInstance("my.logger1"), logEvent)
    #define MYLOG2_INFO(logEvent) LOG4CPLUS_INFO (log4cplus::Logger::getInstance("my.logger2"), logEvent)
    
    int main(int argc, char**argv)
    {
        try
        {
            log4cplus::PropertyConfigurator::doConfigure("log.properties");
        }
        catch( ... )
        {
        std::cerr<<"Exception occured while opening log.properties\n";
        return -1;
        }
        MYLOG1_INFO("hello world!");
        MYLOG2_INFO("hello world!");
        return 0;
    }
    

    Here is Makefile( i suppose my log4cplus is built in parent dirrectory):

    CXXFLAGS+=-I$(abspath ../log4cplus-1.0.4/include)
    all: example.o
        $(CXX) $^ $(abspath ../log4cplus-1.0.4/src/.libs/liblog4cplus.a) -lpthread -o test
    

    Try this example and you should understand how appenders works

    Log4cplus is mostly like log4j. so you can read basic principles log4j. And To get classes names you got to visit log4cplus.sourceforge.net

    About log format. Documentation for log4cplus is available only in doxygen . so here you can read about formating in pattern layout
    And if you want to log process id , than you should use %i in your layout conversion pattern
    example:

    ...
    log4cplus.appender.afile2.layout.ConversionPattern=[%i] %m%n
    ...
    

    It will log process id and message

提交回复
热议问题