java 如何使用相同的 log4j 记录器将不同的信息写入两个不同的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4707847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:37:44  来源:igfitidea点击:

how to write different information to two different files using same logger of log4j?

javaloggingfilelog4j

提问by RanZilber

I'd like to write to two different files using my logger, which is declared like this:

我想使用我的记录器写入两个不同的文件,声明如下:

public static final Logger logger = Logger.getLogger(Adapt.class);
PropertyConfigurator.configure("log4j.properties");

the file log4j contains:

文件 log4j 包含:

log4j.rootLogger=DEBUG, FA

#File Appender
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=temp.ppr
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.append=false
log4j.appender.FA.layout.ConversionPattern= %m%n

Is it possible at all to use logger to write different text to two different files easily?

是否有可能使用记录器轻松地将不同的文本写入两个不同的文件?

If not, is there a way to do that with two loggers? (I tried that and got problems because of the function configure which is static.)

如果没有,有没有办法用两个记录器来做到这一点?(我尝试过,但由于静态的 configure 函数而遇到问题。)

Thanks.

谢谢。

回答by Axel Fontaine

Just define a second logger variable:

只需定义第二个记录器变量:

Logger otherLogger = Logger.getLogger("OTHER_LOGGER");

define a configuration for it (notice the log4j.logger.OTHER_LOGGER syntax cf. log4j.rootLogger, as pointed out by user623395and venkatesh Dodla):

定义一个配置它(通知log4j的。记录器.OTHER_LOGGER语法比照log4j.rootLogger,如通过指出user623395卡塔斯Dodla):

log4j.logger.OTHER_LOGGER=DEBUG, OtherAppender

log4j.additivity.OTHER_LOGGER = false

#File Appender
log4j.appender.OtherAppender=org.apache.log4j.FileAppender
log4j.appender.OtherAppender.File=temp2.ppr
log4j.appender.OtherAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.OtherAppender.append=false
log4j.appender.OtherAppender.layout.ConversionPattern= %m%n

and log your different text as usual:

并像往常一样记录不同的文本:

logger.debug("My normal log");
otherLogger.info("My special text");

回答by user623395

To create temp2.ppr, change from

要创建 temp2.​​ppr,请从

log4j.OTHER_LOGGER=DEBUG, OtherAppender

to

log4j.**logger**.OTHER_LOGGER=DEBUG, OtherAppender

回答by venkatesh Dodla

it worked for me. My Log file:

它对我有用。我的日志文件:

# log4j.properties
log4j.rootLogger=DEBUG,hfis,stdout
#For second log
log4j.logger.OTHER_LOGGER=DEBUG, OtherAppender
log4j.additivity.OTHER_LOGGER = false 

#File Appender 
log4j.appender.OtherAppender=org.apache.log4j.FileAppender 
log4j.appender.OtherAppender.File=C:\Legacy.log
log4j.appender.OtherAppender.ImmediateFlush=true 
log4j.appender.OtherAppender.layout=org.apache.log4j.PatternLayout 
log4j.appender.OtherAppender.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss.SSS} [%t] %-5p %c - %m%n 

log4j.rootCategory=ERROR
log4j.rootLogger.additivity=false

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.hfis=org.apache.log4j.RollingFileAppender
log4j.appender.hfis.Threshold=DEBUG
log4j.appender.hfis.file=C:\hfis.log 
log4j.appender.hfis.ImmediateFlush=true
log4j.appender.hfis.MaxFileSize=5MB
log4j.appender.hfis.layout=org.apache.log4j.PatternLayout
log4j.appender.hfis.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss.SSS} [%t] %-5p %c - %m%n
# R is the RollingFileAppender that outputs to a rolling log
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=DEBUG

# Define a pattern layout for the file.
log4j.appender.mystdout.layout=org.apache.log4j.PatternLayout
log4j.appender.mystdout.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss.SSS} %-5p: %m%n<br>

and my log variables are

我的日志变量是

final static Logger log = Logger.getLogger("hfis");
final static Logger log2 = Logger.getLogger("OTHER_LOGGER");