以编程方式配置Log4j记录器 [英] Configuring Log4j Loggers Programmatically

查看:102
本文介绍了以编程方式配置Log4j记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用SLF4J(带 log4j 绑定)。

I am trying to use SLF4J (with log4j binding) for the first time.

我想要配置可以由LoggerFactory返回的3个不同的命名Logger,它们将记录不同的级别并将消息推送到不同的appender:

I would like to configure 3 different named Loggers that can be returned by a LoggerFactory which will log different levels and push the messages to different appenders:


  • Logger 1 FileLogger记录DEBUG并附加到 DailyRollingFileAppender

  • 记录器2TracingLogger记录TRACE +并附加到 JmsAppender

  • Logger 3ErrorLogger记录ERROR +并追加到另一个 JmsAppender

  • Logger 1 "FileLogger" logs DEBUG and appends to DailyRollingFileAppender
  • Logger 2 "TracingLogger" logs TRACE+ and appends to a JmsAppender
  • Logger 3 "ErrorLogger" logs ERROR+ and appends to a different JmsAppender

此外,我希望它们以编程方式配置(在Java中,而不是XML或 log4j.properties 文件)。

Furthermore I want them configured programmatically (in Java, as opposed to XML or a log4j.properties file).

我想,通常,我会在某些引导代码中的某处定义这些 Logger ,例如一个 init()方法。但是,因为我想使用 slf4j-log4j ,我很困惑我可以在哪里定义记录器并使它们可用于类路径。

I imagine that, normally, I would define these Loggers somewhere in some bootstrapping code, like an init() method. However, because I want to use slf4j-log4j, I'm confused about where I could define loggers and make them available to the classpath.

我不相信这违反了SLF4J的基本目的(作为外观),因为我使用SLF4J API的代码永远不会知道这些记录器存在。我的代码只是对SLF4J API进行正常调用,然后将它们转发到它在类路径上找到的log4j Loggers。

I don't believe this is a violation of SLF4J's underlying purpose (as a facade), because my code using the SLF4J API won't ever know that these loggers exist. My code just makes normal calls to the SLF4J API, which then forwards them on to the log4j Loggers it finds on the classpath.

但是如何配置这些log4j在类路径中记录...在Java中?!

推荐答案

您可以通过编程方式向Log4j添加/删除Appender :

You can add/remove Appender programmatically to Log4j:

  ConsoleAppender console = new ConsoleAppender(); //create appender
  //configure the appender
  String PATTERN = "%d [%p|%c|%C{1}] %m%n";
  console.setLayout(new PatternLayout(PATTERN)); 
  console.setThreshold(Level.FATAL);
  console.activateOptions();
  //add appender to any Logger (here is root)
  Logger.getRootLogger().addAppender(console);

  FileAppender fa = new FileAppender();
  fa.setName("FileLogger");
  fa.setFile("mylog.log");
  fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
  fa.setThreshold(Level.DEBUG);
  fa.setAppend(true);
  fa.activateOptions();

  //add appender to any Logger (here is root)
  Logger.getRootLogger().addAppender(fa);
  //repeat with all other desired appenders

我建议你把它放入init()某处,你确定,这将在其他任何事情之前执行。
然后,您可以使用

I'd suggest you put it into an init() somewhere, where you are sure, that this will be executed before anything else. You can then remove all existing appenders on the root logger with

 Logger.getRootLogger().getLoggerRepository().resetConfiguration();

并开始添加自己的。你需要在类路径中使用log4j才能使用它。

and start with adding your own. You need log4j in the classpath of course for this to work.

备注:

你可以带任何 Logger.getLogger (...)你想添加appender。我只是拿了根记录器,因为它位于所有东西的底部,并将处理通过其他类别中的其他appender传递的所有内容(除非通过设置additivity标志另外配置)。

Remark:
You can take any Logger.getLogger(...) you like to add appenders. I just took the root logger because it is at the bottom of all things and will handle everything that is passed through other appenders in other categories (unless configured otherwise by setting the additivity flag).

如果您需要了解日志记录的工作原理以及如何确定日志的写入位置阅读本手册了解更多相关信息。

简而言之:

If you need to know how logging works and how is decided where logs are written read this manual for more infos about that.
In Short:

  Logger fizz = LoggerFactory.getLogger("com.fizz")

将为您提供记录器类别com.fizz。

对于上面的例子,这意味着用它记录的所有内容都将被引用到根记录器上的控制台和文件appender。

如果你添加一个appender到
Logger.getLogger(com.fizz)。addAppender(newAppender)
然后从 fizz 登录将由app appenders处理来自根记录器和 newAppender

您不使用配置创建记录器,只需为系统中的所有可能类别提供处理程序。

will give you a logger for the category "com.fizz".
For the above example this means that everything logged with it will be referred to the console and file appender on the root logger.
If you add an appender to Logger.getLogger("com.fizz").addAppender(newAppender) then logging from fizz will be handled by alle the appenders from the root logger and the newAppender.
You don't create Loggers with the configuration, you just provide handlers for all possible categories in your system.

这篇关于以编程方式配置Log4j记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆