多个log4j实例配置

发布于 2024-10-04 11:38:22 字数 315 浏览 0 评论 0原文

我想使用 log4j Logger 的多个实例。我需要将不同的 Properties 对象附加到每个 log4j Logger 实例。

以下是为一个实例配置的代码:

LOG4J = org.apache.log4j.Logger.getLogger(Logger.class);

Properties log4jProps = new Properties();

...

PropertyConfigurator.configure(log4jProps);

如果我想要两个 log4j 实例并且每个实例都有不同的属性怎么办?

I want to use multiple instances of the log4j Logger. I need to attach different Properties objects to each of these log4j Logger instances.

Here is the code to configure for one instance:

LOG4J = org.apache.log4j.Logger.getLogger(Logger.class);

Properties log4jProps = new Properties();

...

PropertyConfigurator.configure(log4jProps);

What if I want to have two log4j instance and each of them has different properties?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恰似旧人归 2024-10-11 11:38:22

您能更详细地解释一下为什么需要多个记录器吗?我认为不可能有多个 log4j 实例。

如果您只想要多个附加程序,请查看此处:

这是上面链接中的 log4j.properties:

# logj4.properties
log4j.rootCategory = WARN, A
log4j.category.com.lewscanon = WARN, F
log4j.category.com.lewscanon.mouser = DEBUG, X

log4j.appender.A = org.apache.log4j.ConsoleAppender
log4j.appender.A.layout = org.apache.log4j.PatternLayout

log4j.appender.F = org.apache.log4j.RollingFileAppender
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.File = /projects/mouser/logs/lewscanon.log
log4j.appender.F.MaxFileSize = 512KB
log4j.appender.F.MaxBackupIndex = 2

log4j.appender.X = org.apache.log4j.RollingFileAppender
log4j.appender.X.layout = org.apache.log4j.PatternLayout
log4j.appender.X.File = /projects/mouser/logs/mouser.log
log4j.appender.X.MaxFileSize = 512KB
log4j.appender.X.MaxBackupIndex = 2

log4j.appender.A.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
log4j.appender.F.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
log4j.appender.X.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n

这些行:

log4j.rootCategory = WARN, A
log4j.category.com.lewscanon = WARN, F
log4j.category.com.lewscanon.mouser = DEBUG, X

说出以下内容:

  • 将所有内容记录到附加程序 A(即控制台)。仅记录警告及以上内容
  • 将 com.lewscanon 包中的所有内容记录到附加程序 F(写入文件 lewscanon.log)。仅记录警告及以上
  • 内容 记录从 com.lewscannon.mouser 包到附加器 X 的所有内容(进入文件 mouser.log)。仅记录 debug 及以上级别

也可以提供完整的类名:

log4j.category.com.lewscanon.SomeClass = WARN, F

表示记录从 com.lewscanon.SomeClass 类到附加程序 F、级别 warn 及以上的所有内容。

我想上面的内容已经足够细化了,但是如果您绝对需要更细化(但我认为这不够实用),请按照:

Logger.getLogger(String name) 接受名称。因此,您可以执行以下操作:

log4j.category.myFancyLogger = INFO, F

并使用 Logger.getLogger("myFancyLogger") 获取使用级别信息记录到附加程序 F 的记录器。

Can you explain why you want multiple loggers in more detail? I don't think it's possible to have multiple log4j instances.

If you just want multiple appenders, look here:

Here's the log4j.properties from the above link:

# logj4.properties
log4j.rootCategory = WARN, A
log4j.category.com.lewscanon = WARN, F
log4j.category.com.lewscanon.mouser = DEBUG, X

log4j.appender.A = org.apache.log4j.ConsoleAppender
log4j.appender.A.layout = org.apache.log4j.PatternLayout

log4j.appender.F = org.apache.log4j.RollingFileAppender
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.File = /projects/mouser/logs/lewscanon.log
log4j.appender.F.MaxFileSize = 512KB
log4j.appender.F.MaxBackupIndex = 2

log4j.appender.X = org.apache.log4j.RollingFileAppender
log4j.appender.X.layout = org.apache.log4j.PatternLayout
log4j.appender.X.File = /projects/mouser/logs/mouser.log
log4j.appender.X.MaxFileSize = 512KB
log4j.appender.X.MaxBackupIndex = 2

log4j.appender.A.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
log4j.appender.F.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
log4j.appender.X.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n

These lines:

log4j.rootCategory = WARN, A
log4j.category.com.lewscanon = WARN, F
log4j.category.com.lewscanon.mouser = DEBUG, X

say the following:

  • Log everything to appender A (which is console). Only log warnings and above
  • Log everything from com.lewscanon package to appender F (which goes to file lewscanon.log). Only log warnings and above
  • Log everything from com.lewscannon.mouser package to appender X (which goes to file mouser.log). Only log debug and above

It's also possible to supply full class names:

log4j.category.com.lewscanon.SomeClass = WARN, F

means log everything from com.lewscanon.SomeClass class to appender F, level warn and above.

I suppose the above is granular enough, but if you absolutely need it more granular (I don't think that's practical enough, though), per:

Logger.getLogger(String name) accepts names. So you can do something like:

log4j.category.myFancyLogger = INFO, F

and use Logger.getLogger("myFancyLogger") to get the logger that logs to appender F with level info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文