This discussion is archived
5 Replies Latest reply: Jul 3, 2013 5:53 AM by 836548 RSS

Write into log file

shivasha Newbie
Currently Being Moderated

Hi,
I need to write into log file.
The issue is log file is created but it not stroring information

getting below error:

log4j:ERROR Could not find value for key log4j.appender.DEBUG
log4j:ERROR Could not instantiate appender named "DEBUG".
log4j:ERROR Could not find value for key log4j.appender.R.layout

logging.properties
log4j.rootLogger=info,debug, stdout, R   
log4j.appender.stdout=org.apache.log4j.ConsoleAppender   
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=D:/notifier1-logs.log   
log4j.appender.R.MaxFileSize=220KB
log4j.appender.R.MaxBackupIndex=1

Main.java
public static void main(String[] args) {  

final Logger LOGGER = Logger.getLogger(Main.class
         .getName());

          
    try {  
     PropertyConfigurator.configure("D:/JavaLogging/resources/logging.properties");
    
    
     LOGGER.info("The log message.");
     
         LOGGER.info("My first log");  
      
          
    } catch (SecurityException e) {  
        e.printStackTrace();  
    }
      
    LOGGER.info("Hi How r u?");  
}  

  • 1. Re: Write into log file
    jtahlborn Expert
    Currently Being Moderated

    you are instantiating LOGGER before you are configuring log4j.  i would imagine that isn't going to work very well.  (and your logging config is apparently problematic as well).

  • 2. Re: Write into log file
    shivasha Newbie
    Currently Being Moderated

    thanks,

    could you pls share the code sample (working version)

  • 3. Re: Write into log file
    836548 Journeyer
    Currently Being Moderated

    don't expect for ready answers. earlier it is already pointed why the program is giving error.

  • 4. Re: Write into log file
    shivasha Newbie
    Currently Being Moderated

    @836548,

     

    I tried the way jtahlborn suggested.

    Still problem is there.

     

    If you have solution provide and dont comment on other posts.


  • 5. Re: Write into log file
    836548 Journeyer
    Currently Being Moderated

    what i said earlier was correct, unless you want to try your self on the given pointers .

    Below is the working program:
    logging.properties

    log4j.rootLogger=debug, stdout, R

    log4j.appender.stdout=org.apache.log4j.ConsoleAppender   

    log4j.appender.R=org.apache.log4j.RollingFileAppender

    log4j.appender.R.File=D:/testLogger.log   

    log4j.appender.R.MaxFileSize=220KB

    log4j.appender.R.MaxBackupIndex=1

    log4j.appender.R.layout=org.apache.log4j.PatternLayout //Layout is required, hence added

    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout //Layout is required, hence added

     

    Main Program

    [code]

    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;

    class TestLogger
    {
    static {
       PropertyConfigurator.configure("logging.properties");
       }
    public static void main(String[] args) {  
      final Logger LOGGER = Logger.getLogger(TestLogger.class.getName());
      try {  
       LOGGER.info("The log message.");
       LOGGER.info("My first log");  
      } catch (SecurityException e) {  
       e.printStackTrace();  
      }
          
        LOGGER.info("Hi How r u?");  
    }  
    }

    [code]

     

    command to compile and run

    javac -cp .;log4j-1.2.13.jar TestLogger.java

     

    java -cp .;log4j-1.2.13.jar TestLogger

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points