以编程方式配置 Log4j 记录器
我第一次尝试使用 SLF4J(带有 log4j
绑定)。
我想配置 3 个不同的命名 Logger,它们可以由 LoggerFactory 返回,这将记录不同级别并将消息推送到不同的附加程序:
- Logger 1 "FileLogger" 记录 DEBUG 并附加到
DailyRollingFileAppender
- Logger 2 " TracingLogger”记录 TRACE+ 并附加到
JmsAppender
- Logger 3 “ErrorLogger”记录 ERROR+ 并附加到不同的 JmsAppender
此外,我希望以编程方式配置它们(在 Java 中,而不是 XML 或 log4j.properties 文件)。
我想,通常情况下,我会在一些引导代码中的某个地方定义这些 Logger,例如 init() 方法。但是,因为我想使用 slf4j-log4j,所以我对在哪里定义记录器并使它们可用于类路径感到困惑。
我不相信这违反了 SLF4J 的根本目的(作为外观),因为我使用 SLF4J API 的代码永远不会知道这些记录器的存在。我的代码只是对 SLF4J API 进行正常调用,然后将它们转发到在类路径上找到的 log4j 记录器。
但是如何在 Java 中的类路径上配置这些 log4j 记录器?!
I am trying to use SLF4J (with log4j
binding) for the first time.
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" 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
Furthermore I want them configured programmatically (in Java, as opposed to XML or a log4j.properties
file).
I imagine that, normally, I would define these Logger
s 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.
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.
But how do I configure those log4j Loggers on the classpath...in Java?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以以编程方式向 Log4j 添加/删除 Appender:
我建议您将其放入 init() 某个地方,您确信这将在其他任何事情之前执行。
然后,您可以删除根记录器上的所有现有附加程序
,并开始添加您自己的附加程序。当然,您需要在类路径中使用 log4j 才能正常工作。
备注:
您可以使用任何您喜欢的
Logger.getLogger(...)
添加附加程序。我只是采用了根记录器,因为它位于所有事物的底部,并将处理通过其他类别中的其他附加程序传递的所有内容(除非通过设置可加性标志进行其他配置)。如果您需要了解日志记录的工作原理以及如何决定日志写入位置阅读本手册以获取更多信息关于那个。
简而言之:
将为您提供“com.fizz”类别的记录器。
对于上面的示例,这意味着用它记录的所有内容都将被引用到根记录器上的控制台和文件附加程序。
如果您添加一个附加程序
Logger.getLogger("com.fizz").addAppender(newAppender)
那么来自
fizz
的日志记录将由来自根记录器和newAppender
的所有附加程序处理。您无需使用配置创建记录器,只需为系统中所有可能的类别提供处理程序。
You can add/remove Appender programmatically to Log4j:
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
and start with adding your own. You need log4j in the classpath of course for this to work.
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:
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 thenewAppender
.You don't create Loggers with the configuration, you just provide handlers for all possible categories in your system.
听起来您正在尝试从“两端”(消费者端和配置端)使用 log4j。
如果您想针对 slf4j api 进行编码,但提前(并以编程方式)确定类路径将返回的 log4j 记录器的配置,那么您绝对必须进行某种日志记录调整,以利用懒惰的建设。
通过这种方法,您无需担心 log4j 记录器的配置地点/时间。类路径第一次请求它们时,它们会被延迟构造、传回并通过 slf4j 提供。希望这有帮助!
It sounds like you're trying to use log4j from "both ends" (the consumer end and the configuration end).
If you want to code against the slf4j api but determine ahead of time (and programmatically) the configuration of the log4j Loggers that the classpath will return, you absolutely have to have some sort of logging adaptation which makes use of lazy construction.
With this approach, you don't need to worry about where/when your log4j loggers get configured. The first time the classpath asks for them, they get lazily constructed, passed back and made available via slf4j. Hope this helped!
如果有人想用 Java 以编程方式配置 log4j2,那么此链接可能会有所帮助:(https://www.studytonight.com/post/log4j2-programmatic-configuration-in-java-class)
这是配置控制台Appender的基本代码:
这将重新配置默认的rootLogger,并且还将创建一个新的appender。
If someone comes looking for configuring log4j2 programmatically in Java, then this link could help: (https://www.studytonight.com/post/log4j2-programmatic-configuration-in-java-class)
Here is the basic code for configuring a Console Appender:
This will reconfigure the default rootLogger and will also create a new appender.
如果您已在 log4j 属性中定义了附加程序并希望以编程方式更新它,请在 log4j 属性中设置名称并按名称获取它。
以下是 log4j.properties 条目的示例:
要更新它,请执行以下操作:
In the case that you have defined an appender in log4j properties and would like to update it programmatically, set the name in the log4j properties and get it by name.
Here's an example log4j.properties entry:
To update it, do the following: