问题描述
我有一个log4j appender定义如下:
I have a log4j appender defined like:
log4j.logger.com.example = DEBUG, filelog
log4j.appender.filelog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.filelog.File=c:/app.log
log4j.appender.filelog.layout=org.apache.log4j.PatternLayout
log4j.appender.filelog.layout.ConversionPattern=%d | %m%n
log4j.appender.filelog.DatePattern=.dd-MM-yyyy
在我的班上,我得到了这样的记录器:
In my class, I get the logger like:
Log logger = LogFactory.getLog(getClass());
这是正常的。我希望有一个记录器,它总是记录某些消息(不是错误,而是事务所花费的时间)。如果我在DEBUG或INFO中写这些,如果更改了日志级别,我将看不到它们。我想我可以使用写入同一文件的另一个appender来完成此任务。
This works properly. I want to have a logger that always logs certain messages (not errors, but things like how long transactions took). If I write these at DEBUG or INFO, I won't see them if the log level is changed. I think I can accomplish this using another appender that writes to the same file.
这可能有两个appender写入同一个文件吗?我如何获得我想在同一个类中使用普通调试appender和事务性appender的logger实例?这些消息不会都在同一个包中,因此我无法将某个包配置为始终记录。我是否必须让这些appender写入不同的文件,或者我可以在代码中检索它们并具有类似的内容:
Is this possible to have two appenders write to the same file? How would I get the logger instance where I want to use the normal debug appender and the transactional appender in the same class? These messages won't all be in the same package, so I can't configure a certain package to always log. Will I have to have these appenders write to different files, or can I retrieve them both in the code and have something like:
Log alwaysLogger = LogFactory.getLog(ALWAYS);
alwaysLogger.debug("This message will always be written regardless of the level set on the filelog appender");
更新
我可以写入两个不同的日志文件必要的,但我怎样才能在课堂上获得记录器实例?我不想将一个包/类配置为始终使用一个appender而不是另一个apixder,因为类必须在正常运行期间记录信息/错误消息和事务性始终消息。有没有办法完成我需要的东西,即使它写入两个不同的日志文件?
UpdateI could write to two different log files if necessary, but how would I get the logger instance in my class? I don't want to configure one package/class to always use one appender over the other as the classes will have to log information/error messages and the transactional "always" messages during a normal run. Is there a way to accomplish what I need even if it write to two different log files?
推荐答案
我不认为log4j由于同步问题,实际上支持两个写入同一文件的appender。如果可能的话,最好的选择是使用 /logback.qos.ch/index.htmlrel =noreferrer> Logback 作为后端日志记录系统,因为Logback是log4j的后续版本,并且还支持多个appender写入一个文件。查看,这是审慎的模式。
I don't think log4j really supports two appenders writing to the same file because of synchronization issues. Your best bet would be, if possible, to make a switch to slf4j using Logback as your backend logging system since Logback is log4j's successor and also supports multiple appenders writing to one file. Check out FileAppender and it's prudent mode.
更新:从您的描述中看来,您还应该查看的。您只需要一个记录器实例,并且您可以使用标记获得更精细的控制,因为有了它们,您基本上会说此日志语句具有特殊用途,并且必须使用适当的appender进行记录。您还可以检查日志附加程序,这通常在您使用两个时使用或一个日志语句的更多appender。
Update: from your description, it seems you should also check out markers. You only need one logger instance, and you can get more fine grained control using markers, because with them you basically say "this log statement has a special purpose, and has to be logged using an appropriate appender". You can also check log appender additivity, which is usually used when you use two or more appenders for one log statement.
这篇关于Log4j - 让多个appender写入同一个文件,并始终记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!