开发者

LOG4J as a singleton, anyway to avoid log4j logging conflicts in web environment

开发者 https://www.devze.com 2023-01-24 01:20 出处:网络
We have a j2ee web environment.The server is configured to share session and possibly classloaders across multiple webapps.Basically, one classloader could server multiple web apps.

We have a j2ee web environment. The server is configured to share session and possibly classloaders across multiple webapps. Basically, one classloader could server multiple web apps.

This seems to cause issues with log4j. Different webapps could have different log4j configurations but the logging will move to the same file.

Reading online, it looks like log4j uses singletons a lot, in terms of the appenders and other functionality.

Is there a way to completely separate the log4j configurations from one webapp from the other.

Server: websphere6+

Log4j: 1.4.2

Java: 1.5

Example log4j.properties (weba开发者_JS百科pp1):

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/usr/local/file1.log
log4j.additivity.com.app=false

Example log4j.properties (webapp2):

log4j.appender.Z=org.apache.log4j.RollingFileAppender
log4j.appender.Z.File=/usr/local/file2.log
log4j.additivity.com.app=false

Right now, logging from webapp2 may appear in the webapp1 logs and vice verse. We don't want that.

Possible Solution:

  • It might be possible to add a custom file appender? Would that fix the issue and what code would I add to the custom appender?

  • Is it possible to change the log4j initialization. E.g., could I use some startup servlet to load logj4 for each webapp.


You have two ways to solve your problem:

  1. Configure your app. server so it doesn't share classloaders across multiple webapps. When it does do that, because of log4j's nature, only one log4j.properties file will be loaded.

  2. If you leave the app. server so it shares classloaders, then use one "master" log4j.properties file. In it, define appenders for root of every one of your applications (example com.mycompany.webapp1, com.mycompany.webapp2)

No solution is perfect. Particularly, the second one will be problematic if your web apps share some classes that use log4j. In that case, logs from both apps will end up in the same file.

Your problem is a common one. To understand more about this topic, google for "log4j and j2ee".

Edit: since solutions 1 and 2 aren't feasible, you could try something else:

  • Use log4j.properties file per application. In every one of them, define an appender for their root (as explained in solution 2), and set additivity to false. This also won't be perfect if there is any class sharing between them.

  • Configure log4j programatically for every application. Since they're legacy applications, this could be tough. One way to do it is to use ServletContextListener for every application and configure log4j on application startup. I haven't personally tried this, so I'm not 100% sure if there will be clashes due to shared classloaders.

Log4j won't be really updated anymore. Ceki Gülcü, who created log4j, stated that he will focus his efforts on slf4j and logback, to correct some mistakes he did during development of log4j.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号