Log4j - 让多个附加程序写入同一个文件,其中一个始终记录日志

发布于 2024-09-29 05:27:47 字数 1072 浏览 2 评论 0原文

我有一个 log4j 附加程序定义如下:

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

在我的班级中,我得到的记录器如下:

Log logger = LogFactory.getLog(getClass());

这工作正常。我想要一个记录器总是记录某些消息(不是错误,而是事务花费的时间等)。如果我在 DEBUG 或 INFO 处写入这些内容,那么如果日志级别发生更改,我将看不到它们。我想我可以使用另一个写入同一文件的附加程序来完成此操作。

是否可以让两个附加程序写入同一个文件?如何获取要在同一类中使用正常调试附加程序和事务附加程序的记录器实例?这些消息不会全部位于同一个包中,因此我无法将某个包配置为始终记录日志。我是否必须让这些附加程序写入不同的文件,或者我可以在代码中检索它们并具有类似以下内容的内容:

Log alwaysLogger = LogFactory.getLog(ALWAYS);
alwaysLogger.debug("This message will always be written regardless of the level set on the filelog 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());

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.

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

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. Is there a way to accomplish what I need even if it write to two different log files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲喜皆因你 2024-10-06 05:27:47

由于同步问题,我认为 log4j 并不真正支持两个附加程序写入同一文件。如果可能的话,最好的选择是使用 slf4j 切换到 slf4j /logback.qos.ch/index.html" rel="noreferrer">Logback 作为您的后端日志记录系统,因为 Logback 是 log4j 的后继者,并且还支持多个附加程序写入一个文件。查看 FileAppender ,它是谨慎模式。

更新:从您的描述来看,您似乎还应该查看 标记。您只需要一个记录器实例,并且可以使用标记获得更细粒度的控制,因为使用它们您基本上会说“此日志语句有特殊用途,必须使用适当的附加程序进行记录”。您还可以检查日志附加程序 additivity,通常在您使用两个或一个日志语句有更多附加程序。

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.

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.

只想待在家 2024-10-06 05:27:47

据我所知,使用包和记录器的实例化是可行的。类标识符只是一个约定。您可以在任何类中自由创建多个记录器实例并为它们提供任何标识符。

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

至于写入同一个文件,我还没有尝试过,但应该是可能的,至少是从单个线程。您可能需要编写自己的附加程序,而不是依赖默认的附加程序之一。

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.

篱下浅笙歌 2024-10-06 05:27:47

我通过使用 AsyncAppender 来实现这一点。我的主附加程序指定了要登录的文件,并让其他附加程序引用它。

例如,

<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>

您可以添加其他过滤器或其他附加程序。据我自己的应用程序所知,日志似乎在滚动,并且附加器过滤器按预期工作。

I got this to work by using AsyncAppenders. I had my main appender which specified a file to log to, and had my other appenders refer to it.

e.g.

<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. As far as I can tell with my own app, the logs seem to roll, and the appender filters work as expected.

舂唻埖巳落 2024-10-06 05:27:47

是的。您可以“解决方法”让 2 个附加程序写入同一个文件。

查看此代码+示例:

欢迎任何问题。

Yes. You can "workaround" to have 2 appenders writing to the same file.

Checkout this code + example:

Any questions welcome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文