简体   繁体   English

Log4j - 让多个appender写入同一个文件,并始终记录

[英]Log4j - Have multiple appenders write to the same file with one that always logs

I have a log4j appender defined like: 我有一个log4j appender定义如下:

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

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. 如果我在DEBUG或INFO中写这些,如果更改了日志级别,我将看不到它们。 I think I can accomplish this using another appender that writes to the same file. 我想我可以使用写入同一文件的另一个appender来完成此操作。

Is this possible to have two appenders write to the same file? 这可能有两个appender写入同一个文件? How would I get the logger instance where I want to use the normal debug appender and the transactional appender in the same class? 我如何获得我想在同一个类中使用普通调试appender和事务性appender的logger实例? 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: 我是否必须让这些appender写入不同的文件,或者我可以在代码中检索它们并具有以下内容:

Log alwaysLogger = LogFactory.getLog(ALWAYS);
alwaysLogger.debug("This message will always be written regardless of the level set on the filelog appender");

Update I 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. 我不想将一个包/类配置为始终使用一个appender而不是另一个包,因为类必须在正常运行期间记录信息/错误消息和事务性“始终”消息。 Is there a way to accomplish what I need even if it write to two different log files? 有没有办法完成我需要的东西,即使它写入两个不同的日志文件?

I don't think log4j really supports two appenders writing to the same file because of synchronization issues. 我不认为log4j真的支持两个写入同一文件的appender,因为同步问题。 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. 如果可能的话,最好的办法是使用Logback作为后端日志记录系统切换到slf4j,因为Logback是log4j的后续版本,并且还支持多个appender写入一个文件。 Check out FileAppender and it's prudent mode. 查看FileAppender ,它是谨慎的模式。

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". 您只需要一个记录器实例,并且您可以使用标记获得更精细的控制,因为有了它们,您基本上会说“此日志语句具有特殊用途,并且必须使用适当的appender进行记录”。 You can also check log appender additivity , which is usually used when you use two or more appenders for one log statement. 您还可以检查log appender additivity ,这通常在您为一个日志语句使用两个或更多appender时使用。

I got this to work by using AsyncAppender s. 我通过使用AsyncAppenderAsyncAppender这一点。 I had my main appender which specified a file to log to, and had my other appenders refer to it. 我有我的主要appender指定了一个要登录的文件,并让我的其他appender引用它。

eg 例如

<appender name="top" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="file" value="myLog.log" />
</appender>

<appender name="other" class="org.apache.log4j.AsyncAppender">
    <appender-ref ref="top" />
</appender>

<logger name="com.example.MyCLass">
    <appender-ref ref="other" />
</logger>

You can add other filters, or whatever to the other appenders. 您可以向其他appender添加其他过滤器或其他任何过滤器。 As far as I can tell with my own app, the logs seem to roll, and the appender filters work as expected. 据我所知,我自己的应用程序,日志似乎滚动,并且appender过滤器按预期工作。

As far as I'm aware, the instantiation of loggers with a package & class identifier is just a convention. 据我所知,带有包和类标识符的记录器的实例化只是一种约定。 You are free to create multiple logger instances in any class and give them any identifier. 您可以在任何类中自由创建多个记录器实例,并为其提供任何标识符。

Log alwaysLogger = LogFactory.getLog("a.b.c.ALWAYS");
Log sometimesLogger = LogFactory.getLog("a.b.c.SOMETIMES");

As for writing to the same file, I have not tried it but it should be possible, at least from a single thread. 至于写入同一个文件,我没有尝试过,但它应该是可能的,至少从单个线程开始。 You may need to write your own appender rather than relying on one of the default ones. 您可能需要编写自己的appender而不是依赖于其中一个默认的appender。

Yes. 是。 You can "workaround" to have 2 appenders writing to the same file. 你可以“解决”让2个appender写入同一个文件。

Checkout this code + example : 查看此代码+示例

Any questions welcome. 欢迎任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM