Changing log file name from Java Code (with log4j)
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Vidal_CHRIQUI
Posted On:   Monday, October 14, 2002 08:53 AM

Hi,

Unlike most found examples, I would like to set the log file name from my Java code (and not from the log4j.properties file).

Is it possible?
Thanks

Re: Changing log file name from Java Code (with log4j)

Posted By:   Anonymous  
Posted On:   Tuesday, October 15, 2002 05:51 AM

You can instantiate your own FileAppender and set its path. Add that appender via category┬┤s addAppender() method and it should work.

private static Logger logger = (Logger) Logger.getInstance(this.getClass().getName());

logger.addAppender(new FileAppender("myLogfile.log"));

Re: Changing log file name from Java Code (with log4j)

Posted By:   Michael_Canzoneri  
Posted On:   Tuesday, October 15, 2002 05:09 AM

In my application there is three base classes. In each base class I put the Logger into the contructor.

I declare it in the class...


protected static Logger log = Logger.getLogger(MyClass.class);



The in the Constructor...

public MyClass() {

log = Logger.getLogger(this.getClass());

}



And in my log.lcf file (log control file)...

log4j.appender.A2.layout.ConversionPattern=%d{MM/dd/yy HH:mm:ss, SSS} %c{4} (%F:%L) - %m%n



You may have to play with the pattern a little but to get it the way you want it but I think this should help.



If you have any other questions just ask



Michael

About | Sitemap | Contact