aspose file tools*
The moose likes Other Open Source Projects and the fly likes Log4j Appenders Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Log4j Appenders" Watch "Log4j Appenders" New topic
Author

Log4j Appenders

Mike C Bennett
Greenhorn

Joined: Jun 20, 2004
Posts: 7
Hi there,

We have an application (3rd party) that has its own log4j properties file. My code will be running in the same JVM and will therefore use the same properties file. Ideally, I want to define my appenders in a separate file and have their properties file include that file (just so it�s easier to maintain).

I don't know if this is possible and if not, I can simply add my appenders to the bottom of the existing log4j.properties.

Has anyone done anything like this or know if it can be done?

Cheers,



Mike
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41466
    
  51
Moved to the "Other Open Source Projects" forum, where log4j is discussed.


Ping & DNS - my free Android networking tools app
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10033
    
163

If i understand your question correctly, what you are trying to do is to use your own log4j.properties file for your application and let the 3rd party application use its own log4j.properties file. If this is what you are looking for, then you may create a file named something like "myOwnLog4j.properties" and place it in the classpath and use the following statement in your application to let log4j use this as the configuration file.



The other application(3rd party) will continue using the log4j.properties file.

I havent tried this before, but i guess this should work for you


[My Blog] [JavaRanch Journal]
Mike C Bennett
Greenhorn

Joined: Jun 20, 2004
Posts: 7
Hi Jaikiran,

Thanks for your suggestion.

I hadn't tried that before, but I have just run through a quick test. The problem with that approach is that the property configurator configures the static log4j instance for the running JVM. As my code is running in the same JVM as the 3rd party code, this causes my properties file to take priority over the 3rd party code one stopping their logging from being output (as my file doesn't contain their appenders).

I can easily sort out my logging problem by adding my appenders to their properties file, I was just wondering if it was possible within a properties file to say - include this file at the bottom (a bit like the JSP include directive), just so I can keep things separate.

I know that you can pick up system properties using the ${} syntax, but can't think of a way to use that to include my properties!

Cheers,




Mike
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10033
    
163

The problem with that approach is that the property configurator configures the static log4j instance for the running JVM


You are right. I failed to remember that the log4j uses a singleton instance.

I was just wondering if it was possible within a properties file to say - include this file at the bottom (a bit like the JSP include directive), just so I can keep things separate.


As far as i know, this isnt possible with log4j.
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
    
    1
If subsequent configuration is additive, you may just configure log4j in such a way that it won't conflict with the 3rd party app's configuration.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10033
    
163

Here's what i found after googling for a while. This article talks about using RepositorySelector in Servlet Containers:

Supporting the log4j RepositorySelector in Servlet Containers
Mike C Bennett
Greenhorn

Joined: Jun 20, 2004
Posts: 7
I thought I would add an update to this post...

The application we have now in production works alongside a third-party system and both run in the same JVM. As my code is loaded by the third-party classes at the end of the startup procedure, I am able to control the final log4j configuration.

In order to get around the issue of having to merge my log4j properties in with the 3rd party properties, I have created a simple method to load each of the properties files into a Properties object and configure log4j using this.

To make life easy, I have renamed my log4j.properties so that it is unique to my application and ensured that it is on the classpath along with the locations of the other properties files used by the applications.

I'm loading each of the properties files in a loop as an inputstream and using the load() method on the Properties object to load each of the log4j settings. I then call:

PropertyConfigurator.configure(properties); where properties contains the concatenated results all the log4j configurations.

Thanks for all your replies. Feel free to comment on the solution if you have a better way of doing this!

Cheers,



Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Log4j Appenders