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

时间:2023-12-31 19:44:08

使用多个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. log4j.logger.logger0 = TRACE, ap0
  4. log4j.logger.logger1 = TRACE, ap1
  5. log4j.logger.logger2 = TRACE, ap2
  6. log4j.logger.logger3 = TRACE, ap3
  7. log4j.logger.logger4 = TRACE, ap4
  8. log4j.logger.logger5 = TRACE, ap5
  9. log4j.logger.logger6 = TRACE, ap6
  10. log4j.logger.logger7 = TRACE, ap7
  11. log4j.logger.logger8 = TRACE, ap8
  12. log4j.additivity.logger0=false
  13. log4j.additivity.logger1=false
  14. log4j.additivity.logger2=false
  15. log4j.additivity.logger3=false
  16. log4j.additivity.logger4=false
  17. log4j.additivity.logger5=false
  18. log4j.additivity.logger6=false
  19. log4j.additivity.logger7=false
  20. log4j.additivity.logger8=false
  21. log4j.appender.logfile.encoding=UTF-8
  22. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  23. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  24. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
  25. log4j.appender.R.MaxFileSize=100KB
  26. log4j.appender.ap0=org.apache.log4j.RollingFileAppender
  27. log4j.appender.ap0.ImmediateFlush=true
  28. log4j.appender.ap0.File=./0.log
  29. log4j.appender.ap0.MaxBackupIndex=10
  30. log4j.appender.ap0.layout=org.apache.log4j.PatternLayout
  31. log4j.appender.ap0.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n
  32. log4j.appender.ap1=org.apache.log4j.RollingFileAppender
  33. log4j.appender.ap1.ImmediateFlush=true
  34. log4j.appender.ap1.File=./1.log
  35. log4j.appender.ap1.MaxBackupIndex=10
  36. log4j.appender.ap1.layout=org.apache.log4j.PatternLayout
  37. log4j.appender.ap1.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n
  38. log4j.appender.ap2=org.apache.log4j.RollingFileAppender
  39. log4j.appender.ap2.ImmediateFlush=true
  40. log4j.appender.ap2.File=./2.log
  41. log4j.appender.ap2.MaxBackupIndex=10
  42. log4j.appender.ap2.layout=org.apache.log4j.PatternLayout
  43. log4j.appender.ap2.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n
  44. log4j.appender.ap3=org.apache.log4j.RollingFileAppender
  45. log4j.appender.ap3.ImmediateFlush=true
  46. log4j.appender.ap3.File=./3.log
  47. log4j.appender.ap3.MaxBackupIndex=10
  48. log4j.appender.ap3.layout=org.apache.log4j.PatternLayout
  49. log4j.appender.ap3.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n
  50. log4j.appender.ap4=org.apache.log4j.RollingFileAppender
  51. log4j.appender.ap4.ImmediateFlush=true
  52. log4j.appender.ap4.File=./4.log
  53. log4j.appender.ap4.MaxBackupIndex=10
  54. log4j.appender.ap4.layout=org.apache.log4j.PatternLayout
  55. log4j.appender.ap4.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n
  56. log4j.appender.ap5=org.apache.log4j.RollingFileAppender
  57. log4j.appender.ap5.ImmediateFlush=true
  58. log4j.appender.ap5.File=./5.log
  59. log4j.appender.ap5.MaxBackupIndex=10
  60. log4j.appender.ap5.layout=org.apache.log4j.PatternLayout
  61. log4j.appender.ap5.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n
  62. log4j.appender.ap6=org.apache.log4j.RollingFileAppender
  63. log4j.appender.ap6.ImmediateFlush=true
  64. log4j.appender.ap6.File=./6.log
  65. log4j.appender.ap6.MaxBackupIndex=10
  66. log4j.appender.ap6.layout=org.apache.log4j.PatternLayout
  67. log4j.appender.ap6.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n
  68. log4j.appender.ap7=org.apache.log4j.RollingFileAppender
  69. log4j.appender.ap7.ImmediateFlush=true
  70. log4j.appender.ap7.File=./7.log
  71. log4j.appender.ap7.MaxBackupIndex=10
  72. log4j.appender.ap7.layout=org.apache.log4j.PatternLayout
  73. log4j.appender.ap7.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n
  74. log4j.appender.ap8=org.apache.log4j.RollingFileAppender
  75. log4j.appender.ap8.ImmediateFlush=true
  76. log4j.appender.ap8.File=./8.log
  77. log4j.appender.ap8.MaxBackupIndex=10
  78. log4j.appender.ap8.layout=org.apache.log4j.PatternLayout
  79. log4j.appender.ap8.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n