Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to configure log4j when calling utility class from two different programs?

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two programs in the same folder (let's call it "test" and "test2"), both have main methods.

These are my log4j.

My problem is the logging for com.utility which is called in both programs seem to mixed-up between testing1.html and testing2.html.
I guess it's because I have "All,F,G".
Is there anyway I can fix this? I want to display the utility call for "test" in testing1.html and the utility call for "test2" in testing2.html?


 
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Susan,

I'm not aware of a way to do this unfortunately as your utility class is a shared class. I don't think Log4j has a way of knowing what class that utility class was called from...
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use different config files instead of the same config file.

[Edit] I am assuming since you said they are two different programs with main methods that they are run separately.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol,

Thanks for your reply.

I thought there could be only one log4j file in a folder: "log4j.properties"?
I just started using log4j so I might be just don't know that we could have two files and can specify which log4j property file to use in the program. How to do this?
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of ways to do this.

One would be to put the two different log4j.properties files in different folders/directories and add them separately to the classpath for the program of interest

Or you can pass a system property on the command-line to specify the config file to use:

 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol.

Thanks, I tried your second suggestion and it works.
Is it possible to do this inside the program itself?

Thanks again for your help.
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, make sure it doesn't have log4j.properties or log4j.xml on the classpath or it will configure with those first.

PropertyConfigurator.configure(filename);

Described in log4j short manual

org.apache.log4j.PropertyConfigurator Javadoc
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works great. Thanks for your help.
 
Martijn Verburg
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carol Enderlin wrote:You could use different config files instead of the same config file.

[Edit] I am assuming since you said they are two different programs with main methods that they are run separately.



, well spotted Carol, it appears I don't read carefully enough! Apologies Susan for misleading you!
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!