• log4cxx第三篇----使用多个logger


    使用多个logger时,所有logger的配置写在一个配置文件里面

    两个例子:

    1 一个继承的例子(http://logging.apache.org/log4cxx/

    //  file com/foo/bar.h
    #include "log4cxx/logger.h"
    
    namespace com {
       namespace foo {
          class Bar {
              static log4cxx::LoggerPtr logger;
              
          public:
              void doIt();
           }
        }
    }
    
    // file bar.cpp
    #include "com/foo/bar.h"
    
    using namespace com::foo;
    using namespace log4cxx;
    
    LoggerPtr Bar::logger(Logger::getLogger("com.foo.bar"));
    
    void Bar::doIt() {
       LOG4CXX_DEBUG(logger, "Did it again!")
    }

    log4j.rootLogger=DEBUG, A1
    log4j.appender.A1=org.apache.log4j.ConsoleAppender
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout
    
    # Print the date in ISO 8601 format
    log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
    
    # Print only messages of level WARN or above in the package com.foo.
    log4j.logger.com.foo=WARN

    // file MyApp2.cpp
    
    #include "com/foo/bar.h"
    using namespace com::foo;
    
    // include log4cxx header files.
    #include "log4cxx/logger.h"
    #include "log4cxx/basicconfigurator.h"
    #include "log4cxx/propertyconfigurator.h"
    #include "log4cxx/helpers/exception.h"
    
    using namespace log4cxx;
    using namespace log4cxx::helpers;
    // Define a static logger variable so that it references the
    // Logger instance named "MyApp".
    LoggerPtr logger(Logger::getLogger("MyApp"));
    
    int main(int argc, char **argv)
    {
            int result = EXIT_SUCCESS;
            try
            {
                    if (argc > 1)
                    {
                            // BasicConfigurator replaced with PropertyConfigurator.
                            PropertyConfigurator::configure(argv[1]);
                    }
                    else
                    {
                            BasicConfigurator::configure();
                    }
    
                    LOG4CXX_INFO(logger, "Entering application.")
                    Bar bar
                    bar.doIt();
                    LOG4CXX_INFO(logger, "Exiting application.")
            }
            catch(Exception&)
            {
                    result = EXIT_FAILURE;
            }
    
            return result;
    }

    The output of MyApp configured with this file is shown below.

    2000-09-07 14:07:41,508 [12345] INFO  MyApp - Entering application.
    2000-09-07 14:07:41,529 [12345] INFO  MyApp - Exiting application.

    2 使用多个logger

    前置知识

     

    • log4cxx可以使用配置文件进行设置,并且其设置方式与log4j兼容
    • log4cxx库对日志流设置,只需更改配置文件中的appender属性;
    • log4cxx支持Logger继承机制,默认会继承父Loggerappender,由于要将日志流输出到多个文件中,因此需要将子Logger的继承属性设置为false.

     

    配置子Logger

    为了最大程度的灵活性,log4cxx引入了子Logger这个概念,即拥有与父Logger不同的行为,和编程语言中的OOP概念一致。

    下面是在配置文件中定义一个子Logger的代码,其中ap0logger0使用的appender

     

    1. log4j.logger.logger0 = TRACE, ap0  

     

     

    设置子Logger不继承父Loggerappender

     

    1. log4j.additivity.logger0=false  
    设置子Logger所使用的日志文件,下述代码将logger0的日志文件设定为当前程序目录下的0.log

     

     

    1. log4j.appender.ap0.File=./0.log  

     

     

    在程序中获取子Logger实例

    首先是加载配置文件:

     

     

    1. log4cxx::PropertyConfigurator::configure(配置文件名称);  
    获取子Logger实例:

     

     

    1. log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(子Logger名[例如上文中的"logger0"]);  

     

    FreeJudger项目中使用的完整配置文件

     

     

    下述代码配置了8个子Logger,用于FreeJudger项目中判题线程的日志:

     

     

     

    1. log4j.additivity.gather = false  
    2. log4j.rootLogger=TRACE  
    3.   
    4. log4j.logger.logger0 = TRACE, ap0  
    5. log4j.logger.logger1 = TRACE, ap1  
    6. log4j.logger.logger2 = TRACE, ap2  
    7. log4j.logger.logger3 = TRACE, ap3  
    8. log4j.logger.logger4 = TRACE, ap4  
    9. log4j.logger.logger5 = TRACE, ap5  
    10. log4j.logger.logger6 = TRACE, ap6  
    11. log4j.logger.logger7 = TRACE, ap7  
    12. log4j.logger.logger8 = TRACE, ap8  
    13.   
    14. log4j.additivity.logger0=false  
    15. log4j.additivity.logger1=false  
    16. log4j.additivity.logger2=false  
    17. log4j.additivity.logger3=false  
    18. log4j.additivity.logger4=false  
    19. log4j.additivity.logger5=false  
    20. log4j.additivity.logger6=false  
    21. log4j.additivity.logger7=false  
    22. log4j.additivity.logger8=false  
    23.   
    24. log4j.appender.logfile.encoding=UTF-8  
    25.   
    26. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
    27. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
    28. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n  
    29.   
    30. log4j.appender.R.MaxFileSize=100KB  
    31.   
    32. log4j.appender.ap0=org.apache.log4j.RollingFileAppender  
    33. log4j.appender.ap0.ImmediateFlush=true   
    34. log4j.appender.ap0.File=./0.log  
    35. log4j.appender.ap0.MaxBackupIndex=10  
    36. log4j.appender.ap0.layout=org.apache.log4j.PatternLayout  
    37. log4j.appender.ap0.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n  
    38.   
    39. log4j.appender.ap1=org.apache.log4j.RollingFileAppender  
    40. log4j.appender.ap1.ImmediateFlush=true   
    41. log4j.appender.ap1.File=./1.log  
    42. log4j.appender.ap1.MaxBackupIndex=10  
    43. log4j.appender.ap1.layout=org.apache.log4j.PatternLayout  
    44. log4j.appender.ap1.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n  
    45.   
    46. log4j.appender.ap2=org.apache.log4j.RollingFileAppender  
    47. log4j.appender.ap2.ImmediateFlush=true   
    48. log4j.appender.ap2.File=./2.log  
    49. log4j.appender.ap2.MaxBackupIndex=10  
    50. log4j.appender.ap2.layout=org.apache.log4j.PatternLayout  
    51. log4j.appender.ap2.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n  
    52.   
    53. log4j.appender.ap3=org.apache.log4j.RollingFileAppender  
    54. log4j.appender.ap3.ImmediateFlush=true   
    55. log4j.appender.ap3.File=./3.log  
    56. log4j.appender.ap3.MaxBackupIndex=10  
    57. log4j.appender.ap3.layout=org.apache.log4j.PatternLayout  
    58. log4j.appender.ap3.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n  
    59.   
    60. log4j.appender.ap4=org.apache.log4j.RollingFileAppender  
    61. log4j.appender.ap4.ImmediateFlush=true   
    62. log4j.appender.ap4.File=./4.log  
    63. log4j.appender.ap4.MaxBackupIndex=10  
    64. log4j.appender.ap4.layout=org.apache.log4j.PatternLayout  
    65. log4j.appender.ap4.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n  
    66.   
    67. log4j.appender.ap5=org.apache.log4j.RollingFileAppender  
    68. log4j.appender.ap5.ImmediateFlush=true   
    69. log4j.appender.ap5.File=./5.log  
    70. log4j.appender.ap5.MaxBackupIndex=10  
    71. log4j.appender.ap5.layout=org.apache.log4j.PatternLayout  
    72. log4j.appender.ap5.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n  
    73.   
    74. log4j.appender.ap6=org.apache.log4j.RollingFileAppender  
    75. log4j.appender.ap6.ImmediateFlush=true   
    76. log4j.appender.ap6.File=./6.log  
    77. log4j.appender.ap6.MaxBackupIndex=10  
    78. log4j.appender.ap6.layout=org.apache.log4j.PatternLayout  
    79. log4j.appender.ap6.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n  
    80.   
    81. log4j.appender.ap7=org.apache.log4j.RollingFileAppender  
    82. log4j.appender.ap7.ImmediateFlush=true   
    83. log4j.appender.ap7.File=./7.log  
    84. log4j.appender.ap7.MaxBackupIndex=10  
    85. log4j.appender.ap7.layout=org.apache.log4j.PatternLayout  
    86. log4j.appender.ap7.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n  
    87.   
    88. log4j.appender.ap8=org.apache.log4j.RollingFileAppender  
    89. log4j.appender.ap8.ImmediateFlush=true   
    90. log4j.appender.ap8.File=./8.log  
    91. log4j.appender.ap8.MaxBackupIndex=10  
    92. log4j.appender.ap8.layout=org.apache.log4j.PatternLayout  
    93. log4j.appender.ap8.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n  
  • 相关阅读:
    Redis常用命令手册:键值相关命令
    Redis常用命令手册:服务器相关命令
    预览图片插件
    Hibernate的generator属性之意义
    CentOS开机自动运行程序的脚本
    64位Win7安装Oracle12C临时位置权限错误解决方案
    ORACLE基本语法
    linux图形界面安装
    因xhost命令和DISPLAY环境变量操作不当导致无法启动Oracle图形化安装界面
    Linux 配置:Xmanager连接Linux图形界面
  • 原文地址:https://www.cnblogs.com/For-her/p/3922536.html
Copyright © 2020-2023  润新知