• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.util.logging: how2 separate output from loggers with the same name

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi !
there are two web applications running in my tomcat server. Both applications use an instance of the class "com.lpip.db.pool.PooledConnection".
In this class, I define a logger as follows:
protected static Logger logger;
static {
logger = Logger.getLogger("com.lpip.db.pool");
}
I'm using a separate log file for each application - but how can I accomplish this ? both "PooledConnection" objects log into a logger named "com.lpip.db.pool" and run within the same JVM, so the log messages of both applications get merged and are the same in both log files !
can i solve this problem without changing the PooledConnection class ?
thanks a lot,
Dennis
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can i solve this problem without changing the PooledConnection class ?
Mmmmmaybe. I think that the proper solution, if at all possible, is to change the PooledConnection class to make logger an instance variable, and make sure each logger gets created with a different name (unique to the PooledConnection instance). A simple way to do that is:

Technically the identityHashCode is not guaranteed to be unique, but I'd be pretty surprised if it isn't. Or you can use some other method to ensure a unique name.
Now if you really can't change the PooledConnection code, it's tough. You can configure the Logger, adding two (or more) separate Handlers, each one intended to respond only to messages originating from a particular PooledConnection. How can a given Handler tell where a message originated? Looking at the information available in the LogRecord class, I don't see much that's useful to use here. Stuff like getSourceClassName() doesn't give us any info about which instance issued a logging request. The only thing I see that might work is getThreadID(). IF the two instances are running in different threads, then each Handler can respond only to a particular Thread. Of course it's also possible that there are multiple Threads associated with each PooledConnection, so this could get messy. I'd say your best bet is to study the threading situation to see if this is a viable way to tell instances apart. Good luck.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jim !
I see I have to include a distinctive bit in the logger name so I'll go for your hashcode solution.
It's okay for me to change the code, but I don't want to change it for each application because this is an asset which I'd like to have in a jar.
thanks again !
 
Seriously? That's what you're going with? I prefer this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic