从库代码记录到应用程序日志的策略?

时间:2021-07-02 21:51:37

Where I work we use Log4j for web application logging. The log4j.jar is at the application level, not the container level. We're using a daily rolling file appender. Our log4j.properties files define appenders based on the app package name, so only classes in our application's package and below get logged to our app's log file.

我在哪里工作,我们使用Log4j进行Web应用程序日志记录。 log4j.jar位于应用程序级别,而不是容器级别。我们正在使用每日滚动文件追加器。我们的log4j.properties文件根据应用程序包名称定义了appender,因此只有我们应用程序包及其下面的类才会记录到我们应用程序的日志文件中。

In my application, I'm extending our framework with some supporting classes. These classes are not in the application's package, as they are not exclusive to my application and will eventually be made into a jar library for use with other applications. Because of this, my logging statements are not picked up by my application's appender, and are thus not logged to my application's log files.

在我的应用程序中,我正在使用一些支持类来扩展我们的框架。这些类不在应用程序的包中,因为它们不是我的应用程序所独有的,最终会被制作成jar库以供其他应用程序使用。因此,我的日志记录语句不会被我的应用程序的appender选中,因此不会记录到我的应用程序的日志文件中。

I want to allow the classes in my jar to log to the log file of the application using the classes. However, if I create an appender in my application's log4j properties file based on my classnames, I suspect that when multiple applications are using my jar, because of the identical class names in the log4j.properties files, only one application log file will receive my jar's logging statements, and that it will receive ALL the logging statements from EVERY application using that jar. I think this is the case, since we're using a static Logger.getLogger() call to retrieve the logger.

我想允许jar中的类使用类登录到应用程序的日志文件。但是,如果我根据我的类名在应用程序的log4j属性文件中创建一个appender,我怀疑当多个应用程序使用我的jar时,由于log4j.properties文件中的类名相同,只有一个应用程序日志文件会收到我的jar的日志记录语句,它将从使用该jar的每个应用程序接收所有日志记录语句。我认为情况就是这样,因为我们使用静态Logger.getLogger()调用来检索记录器。

I first want to know if my fears are valid, if this is really what would happen when multiple web applications in the same or different containers are using my jar simultaneously.

我首先想知道我的恐惧是否有效,如果真的是当同一个或不同容器中的多个Web应用程序同时使用我的jar时会发生什么。

I'd also like to know if there are "boundaries" on which this behavior changes. For example, does this problem exist regardless of whether log4j.jar is a container-level jar or an app-level jar, or if each container is running in a separate JVM?

我还想知道这种行为是否存在“边界”变化。例如,无论log4j.jar是容器级jar还是app级jar,或者每个容器是否在单独的JVM中运行,是否存在此问题?

Lastly, if this IS the case, I'd like to know what strategies I should use to overcome the problem.

最后,如果是这种情况,我想知道我应该用什么策略来解决这个问题。

Thanks in advance!

提前致谢!

2 个解决方案

#1


If log4j.jar is only in the web-app then the logs will stay separate so each web-app should have its own log4j.jar and log4j.properties so that all logs stay separate.

如果log4j.jar仅在web-app中,则日志将保持独立,因此每个web-app都应具有自己的log4j.jar和log4j.properties,以便所有日志保持独立。

#2


The problem is that the preffered pattern for log4j is to use static *Configurator methods, which don't go very well with application containers.

问题是log4j的优先模式是使用static * Configurator方法,这些方法与应用程序容器的关系不太好。

This article solved this problem for me when i came across it ...

当我遇到它时,这篇文章为我解决了这个问题......

#1


If log4j.jar is only in the web-app then the logs will stay separate so each web-app should have its own log4j.jar and log4j.properties so that all logs stay separate.

如果log4j.jar仅在web-app中,则日志将保持独立,因此每个web-app都应具有自己的log4j.jar和log4j.properties,以便所有日志保持独立。

#2


The problem is that the preffered pattern for log4j is to use static *Configurator methods, which don't go very well with application containers.

问题是log4j的优先模式是使用static * Configurator方法,这些方法与应用程序容器的关系不太好。

This article solved this problem for me when i came across it ...

当我遇到它时,这篇文章为我解决了这个问题......