Log4j 记录到共享日志文件

发布于 2024-07-08 15:03:22 字数 90 浏览 2 评论 0原文

有没有办法将 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 技术交流群。

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

发布评论

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

评论(5

相守太难 2024-07-15 15:03:22

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.

黑寡妇 2024-07-15 15:03:22

最好的选择可能是让每个应用程序单独记录,然后安排一个预定作业,根据时间将文件“压缩”在一起。 如果您确实需要对完整日志进行最新访问,则可以每小时运行一次。

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.

半世蒼涼 2024-07-15 15:03:22

我有以下两种方法的经验:

  1. 使用数据库进行日志记录而不是纯文本文件 - 由于性能问题,它可能会令人望而却步,但另一方面,分析日志、创建报告却非常容易。 数据库负责处理所有并发问题。
  2. 另一种方法涉及使用 JBoss 服务器,该服务器可用于从其他来源读取日志信息。 JBoss 可以在最小配置下运行,因此它非常轻量(启动时间为 2 秒)。 详细信息可以在这里找到 http://docs.jboss.org/process -guide/en/html/logging.html(记录到单独的服务器)。 Log4J 处理所有锁定/并发问题。

如果您不打算使用 JBoss,则可以使用第二种方法作为您自己的日志记录解决方案的基础。

I have a experience with the following two approaches:

  1. Use database for logging instead of plain text file - it can be prohibitive because of performance issues, on the other hand it is very easy to analyze logs, create reports. Database takes care for all concurrency problems.
  2. The other approach involves usage of JBoss server, which can be used to read logging information from other sources. JBoss can be run in the minimal configuration and thanks to that it is really lightweight (2 seconds startup time). Details can be found here http://docs.jboss.org/process-guide/en/html/logging.html (Logging to a Seperate Server). Log4J takes care of all locking/concurrency issues.

If you are not planning to use JBoss you can use the second approach as a base of your own logging solution.

蘑菇王子 2024-07-15 15:03:22

我认为默认的 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.

⒈起吃苦の倖褔 2024-07-15 15:03:22

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.

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