my dog learned polymorphism*
The moose likes Other Open Source Projects and the fly likes Multiple log4j configurations in a single classloader ? 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 "Multiple log4j configurations in a single classloader ?" Watch "Multiple log4j configurations in a single classloader ?" New topic
Author

Multiple log4j configurations in a single classloader ?

Dev Das
Greenhorn

Joined: Apr 05, 2009
Posts: 5
Hi, I have a requirement where in I want to use log4j logging for a single component(read java class) in my application, but the problem is that a log4j configuration is already in place for other third party components in the same classloader scope. I tried configuring the log4j properties programmatically. It logs fine for the target log file, which I had set for the my appender, for sometime, but later on the logs get printed in the file specified by the root logger appender(which is specified in the core log4j.properties). How can I circumvent the situation? Any suggestions ?

I forgot mentioning that once the logs get printed in the root appender file, they no longer get printed to the file appender which I have set.

TIA
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

How are you setting the log4j properties programmatically? Do you have access to the actual log4j.properties or log4j.xml?


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Dev Das
Greenhorn

Joined: Apr 05, 2009
Posts: 5
Hi Martijn,

It's like this:
logger = Logger.getLogger (Myclass.class);
l_log_path = "FILE_PATH"
l_layout = new PatternLayout (PRINT_PATTERN);
l_dailyfile_appender = new DailyRollingFileAppender (
l_layout
, l_log_path + "/"+ DEFAULT_LOG_FILE
, "'.'yyyy-MM-dd"
);
logger.addAppender (l_dailyfile_appender);
logger.setLevel (Level.AL

Is there any issue in the same ?
Dev Das
Greenhorn

Joined: Apr 05, 2009
Posts: 5
Hi, It's something like this:


logger = Logger.getLogger (THIS_COMPONENT_NAME);
l_log_path = LOG_PATH;
l_layout = new PatternLayout (PRINT_PATTERN);
l_dailyfile_appender = new DailyRollingFileAppender (
l_layout
, l_log_path + "/"+ DEFAULT_LOG_FILE
, "'.'yyyy-MM-dd"
);
logger.addAppender (l_dailyfile_appender);
logger.setLevel (Level.ALL);

Is there any issue in the same ?
Dev Das
Greenhorn

Joined: Apr 05, 2009
Posts: 5
Hi,

I have used the addAppender, setLevel methods on the logger instance which I retrieved using Logger.getLogger(Myclass.class). I know it's not the
recommended way. But i think it works.

TIA
- DD
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

I'm no expert on configuring it in Java code, but it looks like that should work yes. Can you not actually access the properties file itself?
Dev Das
Greenhorn

Joined: Apr 05, 2009
Posts: 5
Hi,

The main configuration file is for a different module altogether. It's just that their jar files are exposed to our application because we refer them. Since there is a shared class loader for all the jars, I can not configure my separate log4j configuration. In case I define a root logger in my own config file, it may overwrite theirs and that won't be a welcome situation.

So I was just trying to devise a way and hence I posted this query. It seems now that I'll have to append my configuration in their properties file only.


Thanks..
Dev Das
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple log4j configurations in a single classloader ?
 
Similar Threads
need help with first time setup of log4j
remove log4j appender from an API
Frontman 2 FileNotFoundException ?
Logger don't write info and debug in Log file
Can't able to write errors/exception in file using log4j