java Log4j 记录到共享日志文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/285081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:46:13  来源:igfitidea点击:

Log4j Logging to a Shared Log File

javalog4j

提问by Paul Croarkin

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?

有没有办法将 log4j 日志事件写入其他应用程序也正在写入的日志文件。其他应用程序可以是非 Java 应用程序。有什么缺点?锁定问题?格式化?

回答by erickson

Log4j has a SocketAppender, which will send events to a service, which you can implement yourself or use the simple implementation bundled with Log4j.

Log4j 有一个 SocketAppender,它将向服务发送事件,您可以自己实现或使用与 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.

它还支持 syslogd 和 Windows 事件日志,这在尝试将日志输出与来自非 Java 应用程序的事件统一起来时可能很有用。

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.

如果性能是一个问题,那么您需要单个服务来写入日志文件,而不是尝试在不同的日志记录应用程序之间协调一致的锁定策略。

回答by Dana the Sane

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.

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

回答by Piotr Kochański

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

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

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

回答by skiphoppy

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.

我认为默认的 log4j 附加程序不会执行任何文件锁定或同步。如果没有这样的锁定,您很可能会丢失日志消息或收到它们被破坏的消息。

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.

我不确定在 Java 中进行文件锁定有多么容易,但是要直接进行这项工作,我认为您需要对适当的 Appender 进行子类化并覆盖日志记录方法,并使用锁定和解锁文件的同步代码对其进行包装. 这可能会影响性能,具体取决于您的系统负载。

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.

Log4perl 有一个同步 Appender,Log::Log4perl::Appender::Synchronized,它包装了一个 Appender 并实现了这一点,并且似乎还在其 Log::Log4perl::Appender::File 中提供了一个标志来防止交错行。您可以查看它们的作用,看看它是否可以在 Java 中复制。

回答by Dillie-O

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.

Log4j 足够灵活,您可以以与文件中已有的记录兼容的格式创建日志条目。只需查看 appender 如何格式化特定数据字段。

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.

您主要关心的是锁定,很可能是其他应用程序。确保所有应用程序都没有对文件进行独占锁定,您应该没问题。