logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ganesh_912 <ganesh...@gmail.com>
Subject Not able to create log file FileAppender
Date Tue, 08 Nov 2011 12:29:13 GMT

Hi ,

I am designing a web app which will create log files dyanamically from a
servlet when app is deployed but it is creating a log file from .properties
file only.

Below is servlet code snippet that i am using to create a log file with
log4j.properties file.


public void contextInitialized(ServletContextEvent sce) {
        try {
            
         // Load log4j.properties from WEB-INF folder
         String log4jConfigurationFile =
sce.getServletContext().getRealPath("/WEB-INF/log4j.properties");         
         PropertyConfigurator.configure(log4jConfigurationFile);
            
         //Override log4j Configuration
         String newLogFilePath = "C:/tempTEST/hi/Test_Logger1.log"; // Get
this path from environment variable dynamically
                  
          if(newLogFilePath !=null) {
              Appender oldFileAppender =
Logger.getRootLogger().getAppender("FILE"); // Existing Appender
              	
              
              //PatternLayout pattern = new PatternLayout("%m%n"); // create
a new pattern if required
              FileAppender newFileAppender = new FileAppender ();
              newFileAppender.setThreshold(Priority.DEBUG); // Set the
Threshold
              newFileAppender.setName("New_FILE"); // Set new Appender name
              if(oldFileAppender != null && oldFileAppender.getLayout()
!=null){
                newFileAppender.setLayout(oldFileAppender.getLayout()); //
Set the existing layout
              }
                          
              try {
              newFileAppender.setFile(newLogFilePath); // Set new log file
path
              newFileAppender.setWriter(new OutputStreamWriter // Set Output
Stream and Encoding
                      (new FileOutputStream(newLogFilePath),"IBM-1047"));
              } catch(Exception exception){
                  System.out.println("Exception: " + exception);
              }
              Logger.getRootLogger().addAppender(newFileAppender);
              System.out.println("Latest log file is in: " +
newLogFilePath);
              
              // Remove the old log4j configuration
              Logger.getRootLogger().removeAppender(oldFileAppender);
          }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        }

 ###log4j.properties is here below #####
log4j.rootCategory=DEBUG,FILE
log4j.category.org.apache.tools.ant.Project=WARN,FILE
log4j.appender.name=FILE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.Append=true
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS}
%m%n
#log4j.appender.FILE.file=/users/iowt/online/hmo/logs/Test_Logger.log
log4j.appender.FILE.file=C:/temp23/Test_Logger.log
log4j.appender.FILE.encoding=IBM-1047

After deploying the app i am getting the Test_Logger.log created with path
C:/temp23/Test_Logger.log even if that path did not exist before.

But the log file that i want to generate dynamically
C:/tempTEST/hi/Test_Logger1.log (for example) which i have mentioned in
servlet file above is not getting generated.I am getting
FileNotFoundException for that file path in servlet file.

Please suggest how can i create that log file also which i have mention in
servlet file.

Thanks in advance...
Ganesh:,(:,(:,(
-- 
View this message in context: http://old.nabble.com/Not-able-to-create-log-file-FileAppender-tp32802790p32802790.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message