简体   繁体   English

Log4j:如何配置多个附加程序:一个附加到控制台和一个文件,另一个附加到文件?

[英]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.

相关问题 log4j控制台和SMTPAppender:如何使用多个appender - log4j Console and SMTPAppender: How use multiple appenders 如何为不同的附加程序配置log4j - how to configure log4j for different appenders log4j:如何设置更多程序包登录到两个文件追加器,一个具有INFO阈值,另一个具有DEBUG阈值 - log4j: how to set more packages logging into two file appenders, one with INFO threshold, the other with DEBUG threshold Log4j - 让多个appender写入同一个文件,并始终记录 - Log4j - Have multiple appenders write to the same file with one that always logs 如何使用属性文件配置 log4j - How to configure log4j with a properties file log4j属性文件:如何配置? - log4j properties file: how to configure? 从log4j 1.2迁移到log4j 2 - 如何获取所有appender列表和滚动文件策略 - Migrating from log4j 1.2 to log4j 2 - how to get list of all appenders and rolling file strategy log4j-找不到附加程序,自定义配置文件 - log4j - no appenders could be found, custom config file Log4j:如何为多个appender定义公共布局和ConversionPattern - Log4j:how to define a common layout and ConversionPattern for multiple appenders 如何配置log4j2的log4j2.properties文件,使其具有带有2个不同级别的附加程序的记录器? - How to configure log4j2.properties file for log4j2 to have a logger with 2 appenders with different levels?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM