This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Log4j Appenders

 
Mike C Bennett
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the "Other Open Source Projects" forum, where log4j is discussed.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike C Bennett
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic