Log4j 记录到共享日志文件
有没有办法将 log4j 日志记录事件写入其他应用程序也正在写入的日志文件中。 其他应用程序可能是非 Java 应用程序。 有什么缺点? 锁定问题? 格式化?
Is there a way to write log4j logging events to a log file that is also being written to by other applications. The other applications could be non-java applications. What are the drawbacks? Locking issues? Formatting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Log4j 有一个 SocketAppender,它将事件发送到服务,您可以自己实现该服务,也可以使用与 Log4j 捆绑的简单实现。
它还支持 syslogd 和 Windows 事件日志,这对于尝试将日志输出与来自非 Java 应用程序的事件统一起来可能很有用。
如果性能确实是一个问题,那么您希望由单个服务写入日志文件,而不是尝试在不同的日志记录应用程序之间协调一致的锁定策略。
Log4j has a SocketAppender, which will send events to a service, which you can implement yourself or use the simple implementation bundled with Log4j.
It also supports syslogd and the Windows event log, which may be useful in trying to unify your log output with events from non-Java applications.
If performance is an issue at all, you want a single service writing the log file, rather than trying to coordinate a consistent locking strategy among diverse logging applications.
最好的选择可能是让每个应用程序单独记录,然后安排一个预定作业,根据时间将文件“压缩”在一起。 如果您确实需要对完整日志进行最新访问,则可以每小时运行一次。
Your best bet might be to let each application log separately, then put a scheduled job in place to 'zipper' the files together based on time. If you need really up-to-date access to the full log, you could have this run every hour.
我有以下两种方法的经验:
如果您不打算使用 JBoss,则可以使用第二种方法作为您自己的日志记录解决方案的基础。
I have a experience with the following two approaches:
If you are not planning to use JBoss you can use the second approach as a base of your own logging solution.
我认为默认的 log4j 附加程序不会执行任何文件锁定或同步。 如果没有这样的锁定,您可能会丢失日志消息或收到损坏的日志消息。
我不确定在 Java 中进行文件锁定有多容易,但要直接进行此工作,我认为您需要对适当的 Appender 进行子类化并重写日志记录方法,用锁定和解锁文件的同步代码将其包装起来。 这可能会对性能产生影响,具体取决于您的系统负载。
Log4perl 有一个同步 Appender,Log::Log4perl::Appender::Synchronized,它包装了一个 Appender 并完成了此任务,并且似乎还在其 Log::Log4perl::Appender::File 中提供了一个标志来防止行交错。 您可以看看它们做了什么,看看它是否可以在 Java 中复制。
I don't think the default log4j appenders do any file locking or synchronization. Without such locking, you're likely to lose log messages or receive them mangled.
I'm not sure how easy it is to do file locking in Java, but to make this work directly, I think you would need to subclass the appropriate Appender and override the logging method, wrapping it with synchronization code that locks and unlocks the file. This might have performance implications, depending on your system load.
Log4perl has a synchronizing Appender, Log::Log4perl::Appender::Synchronized, which wraps an Appender and accomplishes this, and also seems to provide a flag in its Log::Log4perl::Appender::File that prevents interleaved lines. You might look at what those do to see if it could be replicable in Java.
Log4j 足够灵活,您可以以与文件中已有记录兼容的格式创建日志条目。 只需查看附加程序即可了解如何格式化特定数据字段。
您主要关心的是锁定,很可能是由其他应用程序锁定。 确保所有应用程序都没有对该文件具有独占锁,这样就可以了。
Log4j is flexible enough that you can create log entries in a format that is compatible with the records already in the file. Just look into the appenders for how to format specific data fields.
Your main concern will be with locking, most likely by other applications. Make sure all of the applications do not have an exclusive lock on the file and you should be fine.