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

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

问题描述

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

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

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

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
  • Logger 2 "TracingLogger" 记录 TRACE+ 并附加到 JmsAppender
  • Logger 3 "ErrorLogger" 记录 ERROR+ 并附加到不同的 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 记录器.

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.我只是使用了根记录器,因为它位于所有事物的底部,并将处理通过其他类别中的其他附加程序传递的所有内容(除非通过设置可加性标志进行了其他配置).

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"类别的记录器.
对于上面的示例,这意味着使用它记录的所有内容都将被引用到根记录器上的控制台和文件附加程序.
如果你添加一个 appenderLogger.getLogger("com.fizz").addAppender(newAppender)然后来自 fizz 的日志将由根记录器和 newAppender 的所有 appender 处理.
您无需使用配置创建记录器,您只需为系统中所有可能的类别提供处理程序.

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天全站免登陆