在具有单独属性文件的多个Web应用程序之间共享Tomcat5中Tomcat5中的单个log4j jar文件 [英] Sharing a single log4j jar file in Tomcat5 between multiple webapps with separate property files

查看:81
本文介绍了在具有单独属性文件的多个Web应用程序之间共享Tomcat5中Tomcat5中的单个log4j jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在tomcat 5.5设置中使用单个log4j jar文件,该文件可以由多个Web应用程序使用,并且每个Web应用程序都有单独的日志记录?

Is it possible to use a single log4j jar file in an tomcat 5.5 setup, where it can be used by multiple webapps and have seperate logging for each webapp?

我编写了大约8种不同的Web应用程序,其中log4j属性文件之间的唯一真正区别是日志文件名.但是,如果我尝试将log4j从webapp WEB-INF/lib目录移动到tomcat5 shared/lib目录,则会遇到问题.

I have about 8 different webapps written where the only real difference between the log4j property files is the log filename. However if I attempt to move log4j from the webapp WEB-INF/lib directory to the tomcat5 shared/lib directory I run into problems.

所有属性文件基本上与下面的属性文件相同,在这里我只是在代码中使用System.setProperty("file.name",)来设置file.name.确实没有必要,但是我想对所有组件使用单个属性文件.

All the property files basically look the same as the one below, where I just set file.name using System.setProperty("file.name", ) in the code. Not necessary really, but I was toying with the idea of using a single properties file for all components.

log4j.rootLogger=DEBUG, LogFile
# stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

# LogFile
log4j.appender.LogFile=org.apache.log4j.RollingFileAppender
log4j.appender.LogFile.File=${file.name}
log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout
log4j.appender.LogFile.MaxFileSize=500KB
log4j.appender.LogFile.MaxBackupIndex=5
log4j.appender.LogFile.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

基本上,我想将每个组件记录到一个单独的文件中,但是问题是,如果我在shared/lib目录下包括log4j,则无论哪个Webapp首先被访问,都将有效地定义将被记录的日志文件.所有webapp都使用.即我不能使用单独的配置.

Basically for each of the components I'd like to log to a separate file, however the problem is that if I include log4j under the shared/lib directory, whichever webapp gets accessed first, effectively defines the log file that will be used by all of the webapps. i.e. I can't use a separate configuration.

我知道的替代方法: 将log4j放入每个war文件的WEB-INF/lib目录中,这样,我将为每个webapp获得单独的配置.

The alternatives that I'm aware of: Put log4j into the WEB-INF/lib directory of each of the war files, that way I'll get a separate configuration per webapp.

将上面的"LogFile"引用更改为特定于每个Web应用程序,以便有效地由每个属性文件定义一个单独的配置.这似乎避免了以下错误" log4j:ERROR"无法将"org.apache.log4j.RollingFileAppender"对象分配给"org.apache.log4j.Appender"变量."

Change "LogFile" reference above to be specific to each webapp, so that effectively a separate configuration is defined by each properties file. This seems to avoid the following error "log4j:ERROR A "org.apache.log4j.RollingFileAppender" object is not assignable to a "org.apache.log4j.Appender" variable."

即使用类似以下的内容:

i.e. use something like the following:

对于WebApp1

log4j.rootLogger=DEBUG, LogFileWebapp1
# stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

# LogFile
log4j.appender.LogFileWebapp1=org.apache.log4j.RollingFileAppender
log4j.appender.LogFileWebapp1.File=${file.name}
log4j.appender.LogFileWebapp1.layout=org.apache.log4j.PatternLayout
log4j.appender.LogFileWebapp1.MaxFileSize=500KB
log4j.appender.LogFileWebapp1.MaxBackupIndex=5
log4j.appender.LogFileWebapp1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

对于WebApp2

log4j.rootLogger=DEBUG, LogFileWebapp2
# stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

# LogFile
log4j.appender.LogFileWebapp2=org.apache.log4j.RollingFileAppender
log4j.appender.LogFileWebapp2.File=${file.name}
log4j.appender.LogFileWebapp2.layout=org.apache.log4j.PatternLayout
log4j.appender.LogFileWebapp2.MaxFileSize=500KB
log4j.appender.LogFileWebapp2.MaxBackupIndex=5
log4j.appender.LogFileWebapp2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

我宁愿坚持使用第一个属性文件的布局,并使其在Web应用程序之间尽可能地相似,并且也不必在每个Web应用程序中都包含单独的log4j副本.理想情况下,我希望只使用从tomcat共享lib目录到log4j的系统副本的符号链接.

I'd prefer to stick with the layout of the first properties file and keep it as similar as possible between the webapps, and would also perfer not to have to include a separate copy of log4j in each of the webapps. Ideally I was hoping to just use a symlink from the tomcat shared lib directory to the system copy of log4j.

还有其他选择吗?

推荐答案

我不会这样做.让每个Web应用程序都有自己的log4j.jar和自己的log4j.properties文件.即使您在其他所有方面都可以做到这一点,也永远无法仅在一个Web应用程序上升级log4j,并且您将很难在不影响其他应用程序的情况下修改一个应用程序的日志记录.

I wouldn't do this. Let each webapp have its own log4j.jar and its own log4j.properties file. Even if you get this working in all other respects, you will never be able to upgrade log4j on just one webapp, and you will have a hard time modifying the logging for one application without affecting others.

除了维护痛苦和痛苦之外,如果log4j保留类引用,则有可能导致巨大的内存泄漏.每次重新部署Tomcat时,所有类的新版本都会创建一个新的应用程序类加载器,而由于log4j不知道要释放它们,因此无法摆脱旧的引用,回收内存的唯一方法是重新启动Tomcat实例.

Over and above maintenance pain and suffering, there's the possibility of causing a huge memory leak if log4j holds onto class references. Every time you redeploy Tomcat creates a new application classloader with new versions of all the classes, with no way to get rid of the old references because log4j doesn't know to let go of them, the only way to reclaim the memory is to restart the Tomcat instance.

这里是有关使用容器级日志记录的有趣文章.它描述了您遇到的问题:

Here's an interesting article on using container-level logging. It describes the problem you're seeing:

当您在容器的lib路径中部署log4j时, 不是"log4j感知"的,并且您没有设置上下文存储库" log4j的选择器"会丢失一个主要功能-日志记录配置 在容器中的所有组件之间变得完全全球化.仅有的 读取一个日志记录配置文件(从容器的 classpath),容器中的每个组件都会看到相同的日志记录 配置.这根本无法与Commons Logging相提并论. 在大多数情况下,这是不可接受的行为;然而 如果发生以下情况,commons-logging可能会提供以这种方式运行的功能 实际上任何人都认为这很有用.

When you deploy log4j in the container's lib path for a container that is not "log4j-aware" and you don't set up a "Context Repository Selector" for log4j you lose a major feature - logging configuration becomes totally global across all components in the container. Only one logging configuration file is read (from the container's classpath) and every component in the container sees the same logging configuration. This is just not comparable to commons-logging at all. In most cases this is not acceptable behaviour; however commons-logging might provide a feature to behave in this manner if anybody actually thinks this is useful.

在容器的lib路径中部署log4j并使用上下文 存储库选择器"行为以获取每个组件的日志记录 配置和log4j也在您获得类的组件路径中 强制转换例外(目前,公共日志记录也遭受相同的影响 问题).这确实是一个单独的问题.

When you deploy log4j in the container's lib path and use the "Context Repository Selector" behaviour to get per-component logging configuration AND log4j is also in the component's path you get class cast exceptions (commons-logging currently suffers from the same issue). This is really a separate problem.

在容器的lib路径中部署log4j并使用上下文 存储库选择器"行为以获取每个组件的日志记录 配置和log4j不在组件的路径中,那么您将 获取内存泄漏的方式与发生泄漏的方式完全相同 公地记录,并且出于完全相同的原因.

When you deploy log4j in the container's lib path and use the "Context Repository Selector" behaviour to get per-component logging configuration and log4j is not in the component's path then you will get memory leaks in exactly the same way as occurs for commons-logging, and for exactly the same reasons.

最简单的解决方案是通过将log4j保留在每个应用程序的WEB-INF/lib中来避免该问题.

The easiest solution is to avoid the problem by keeping log4j in each app's WEB-INF/lib.

这篇关于在具有单独属性文件的多个Web应用程序之间共享Tomcat5中Tomcat5中的单个log4j jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆