Hi,
 Can someone please help me.
I am using RollingFileAppender to log messages to log file.
But I don't want to specify the name of the file to log to. Instead I
want the appender to pick up the name from the Logger class name.
For eg. if Logger.class.getName() returns abc.xyz.apple
So i want Appender to log to apple.log file.
 
So my questions are
- Is it possible to get a reference to logger in the appender?
- I will have to write a custom appender for it or this functionality
already exists?
 
Will something like this work. I have just given a try to write a
CustomAppender, where I want it to pick up the fileName using the
Logger.
 
Any help will be greatly appreciated.
 
cheers,
Smita
 
 
 
package com.ceon.ias.common.logging;
 
import org.apache.log4j.RollingFileAppender;
import java.io.IOException;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.Logger;
 
public class CustomFileAppender extends RollingFileAppender
{
     /**
        The default maximum file size is 10MB.
     */
     protected String logDir = "logs";
 
     public String getLogDir() 
     {   
       return logDir;
     }
 
     public void setLogDir(String logDir ) 
     {
       this.logDir = logDir;
     }
     
 
     public void calculateLogFileName() {
    
        String fullClassName = Logger.class.getName();
        LogLog.debug("fullClassName="+fullClassName);
        String[] tokens =  fullClassName.split(".");
        String filename = tokens[tokens.length - 1] + ".log" ;
        LogLog.debug("filename="+filename);
 
        String fileNameWithPath = getLogDir() + "\\" + filename;
        LogLog.debug("fileNameWithPath="+fileNameWithPath);
 
        try {
          // This will also close the file. This is OK since multiple
          // close operations are safe.
          this.setFile(fileNameWithPath, false);
       }
       catch(IOException e) {
          LogLog.error("setFile("+fileNameWithPath+", false) call
failed.", e);
       }
 }
 
 protected void subAppend(LoggingEvent event) {
     super.subAppend(event);
     if(fileName == null)
       this.calculateLogFileName();
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to