[英]Log4j: How to configure multiple appenders: one directed to the console and a file and the other strictly to a file?
For my application, I have managed to configure log4j to generate multiple logs. 对于我的应用程序,我已经设法将log4j配置为生成多个日志。
Both of the appenders output to the console and to a file. 这两个追加程序都输出到控制台和文件。
But since the first log is my main log, I feel that this log should be the only log outputed to the console. 但是由于第一个日志是我的主日志,因此我认为该日志应该是输出到控制台的唯一日志。
Would it be possible to disable the second log so that log4j does not use the console but still write to the file? 是否可以禁用第二个日志,以便log4j不使用控制台,但仍写入文件?
log4j.rootLogger=DEBUG, stdout
# stdout is set to be ConsoleAppender sending its output to System.out
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss SSS}ms %-5p [%t] - %m%n
log4j.appender.X=org.apache.log4j.FileAppender
log4j.appender.X.File=X.log
log4j.appender.X.Append=false
log4j.appender.X.layout=org.apache.log4j.PatternLayout
log4j.appender.X.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss SSS}ms %-5p [%t] - %m%n
log4j.logger.X=DEBUG,X
the second appender 'Y' is configured the same way as 'X' . 第二个附加项'Y'的配置方式与'X'相同。
I was also thinking of disabling altogether the console for both appenders and use: tail -f X.log inside a shell window to view the logs but it is not very practical when working inside Eclipse. 我还考虑过完全禁用两个附加程序的控制台,而不能使用:在shell窗口中的tail -f X.log来查看日志,但是在Eclipse中进行操作不是很实用。
Any tips would be greatly appreciated. 任何提示将非常感谢。
Regards, 问候,
The only way I know of is disabling additivity for certain loggers (categories), at the point where you direct them into one appender or the other. 我知道的唯一方法是在将某些记录器(类别)定向到一个附加器或另一个附加器中时,将其禁用。 Eg
例如
log4j.logger.com.foo.bar=INFO, X
log4j.additivity.com.foo.bar=false
What about not outputting the root logger to stdout and instead send your X logger to both X and stdout appenders? 不将根记录程序输出到stdout,而是将X记录程序发送到X和stdout追加程序,该怎么办? This way your Y logger would not output to stdout as well.
这样,您的Y记录器也不会输出到stdout。
log4j.logger.X=DEBUG,X,stdout
Both of the appenders output to the console and to a file.
这两个追加程序都输出到控制台和文件。
I think you are confused about the difference between a logger and an appender. 我认为您对记录器和附加器之间的区别感到困惑。
An appender only goes to one place - in your configuration you have declared a ConsoleAppender and a FileAppender. 附加程序仅到达一个位置-在配置中,您已声明ConsoleAppender和FileAppender。 These are two separate entities.
这是两个单独的实体。 Neither of these appenders outputs to more than one location.
这些附加器均未输出到多个位置。
Loggers can be configured to send their output to zero to many appenders, and you have configured all of your loggers to send their output to the console by virtue of the rootLogger
. 可以将记录器配置为将其输出发送给零个许多追加器,并且您已经配置了所有记录器以借助
rootLogger
将其输出发送到控制台。
If you would like to have only certain loggers send output to the console, then don't configure the root logger to use the console, but only the specific logger names to do so. 如果您只希望某些记录器将输出发送到控制台,则不要将根记录器配置为使用控制台,而只需配置特定的记录器名称即可。
If you would like to have all loggers except X
send their output to the console, then you need to disable additivity for X
so that it does not inherit from the root logger. 如果要让除
X
以外的所有记录器将其输出发送到控制台,则需要禁用X
加性,以便它不会从根记录器继承。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.