多个log4j实例配置 [英] multiple log4j instance configuration

查看:156
本文介绍了多个log4j实例配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

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

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

推荐答案

你能解释为什么你想要更详细的多个记录器吗?我认为不可能有多个log4j实例。

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

如果你只想要多个appender,请看这里:

If you just want multiple appenders, look here:

  • http://www.velocityreviews.com/forums/t721794-log4j-different-logger-instances.html

以下是上述链接中的log4j.properties:

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

这些行:

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

说如下:


  • 将所有内容记录到appender A(这是控制台)。仅记录警告及以上

  • 将所有内容从com.lewscanon包记录到appender F(转到文件lewscanon.log)。仅记录警告及以上

  • 将所有内容从com.lewscannon.mouser包记录到appender X(转到文件mouser.log)。只记录调试及以上

还可以提供完整的班级名称:

It's also possible to supply full class names:

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

表示从 com.lewscanon.SomeClass 类到appender 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:

  • http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Logger.html

记录器。 getLogger(String name)接受名称。所以你可以这样做:

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

log4j.category.myFancyLogger = INFO, F

并使用 Logger.getLogger(myFancyLogger)来获取记录到appender的记录器F带有关卡信息。

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

这篇关于多个log4j实例配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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