0

I am new to Log4j API. So, I configured the properties file and written the sample program to test it. Below is my config file and test class.

< ?xml version="1.0" encoding="UTF-8" ?>
<!--<span class="hiddenSpellError" pre=""-->DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
    -5p %c %x - %m%n"/>
</layout>
</appender>

<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="./logs/testlog.log"/>
<layout class="org.apache.log4j.PatternLayout">
-5p %c %p - %m%n"/>
</layout>
</appender>

<root>
<priority value ="trace"></priority>
<appender-ref ref="console"></appender>
<appender-ref ref="fileAppender"></appender>
</root>
</log4j:configuration>

and sample test class is

public class Log4jExample {
private static Logger logger=Logger.getLogger("Project_name");
public static void main(String[] args){
try{
  FileInputStream fstream = 
                     new FileInputStream("D:\\textfile.txt");
  // use DataInputStream to read binary NOT text
  // DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
  String strLine;
  while ((strLine = br.readLine()) != null){
 System.out.println (strLine);
  }
  in.close();
 }catch (FileNotFoundException fe){
 logger.error("File Not Found",fe);
    logger.warn("This is a warning message");
    logger.trace("This message will not be logged since log level is set as DEBUG");
 }catch (IOException e){
 logger.error("IOEXception occured:", e);
}
}

}

am working in netbeans and it is showing that logger.error,logger.trace,logger.warn are "cannot file symbol" error. Please advice with this issue.

4
  • Add log4j to your classpath Commented Dec 11, 2013 at 11:44
  • 1
    Make sure you import org.apache.log4j.Logger Commented Dec 11, 2013 at 11:45
  • stackoverflow.com/a/582519/2628911 Commented Dec 11, 2013 at 11:48
  • am receiving this error. java.lang.NoClassDefFoundError: org/slf4j/ILoggerFactory Commented Dec 11, 2013 at 12:44

1 Answer 1

0

Make sure you have Log4j in your classpath and your code compiles. Also make sure your configuration file is correct. The file you posted is invalid xml. Take a look at the Log4j xml format documentation. Here is syntactically corrected version:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="-5p %c %x - %m%n"/>
        </layout>
    </appender>

    <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="./logs/testlog.log"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="-5p %c %p - %m%n"/>
        </layout>
    </appender>

    <root>
        <priority value="trace"/>
        <appender-ref ref="console"/>
        <appender-ref ref="fileAppender"/>
    </root>
</log4j:configuration>
Sign up to request clarification or add additional context in comments.

2 Comments

am receiving java.lang.NoClassDefFoundError: org/slf4j/ILoggerFactory error
This indicates that you are using slf4j. Make sure you have it in the classpath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.