简体   繁体   中英

Log4j is not working

I am trying with logging methods (Log4j) in eclipse and I am getting the following problems

And I deployed log4j.properties file in project folder. Here below one is the main method of my program:

 package com.aransys.roughworkout.log4j;

 import org.apache.log4j.Logger;

 import java.io.IOException;
 import java.sql.SQLException;
 import java.util.logging.*;

 public class Log4jExample  {

static Logger log = Logger.getRootLogger();
public static void main(String[] args) throws IOException,SQLException {
    log.debug("Sample debug message");
    log.info("Sample info message");
    log.warn("Sample warn message");
    log.error("Sample error message");
    log.fatal("Sample fatal message");
 }
}

And below one is the code in Log4j.properties in my program:

log4j.rootLogger=DEBUG,FILE
log4j.appender.FILE=org.apache.log4j.ConsoleAppender
log4j.appender.FILE.layout=org.apache.log4j.SimpleLayout

But the code is not running properly, and it is the stating the following error.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
at com.aransys.roughworkout.log4j.Log4jExample.main(Log4jExample.java:8)

Your code not even compile! Create your logger like this:

private static final Logger log = Logger.getLogger(Log4jExample.class);

and remove

import java.util.logging.*;

After that, clean and build your project.

According to the exception that you get it seams that there is an compilation problem you have at line 8 which is not resolved

if you are using Eclipse then eclipse must showing the compilation error on same line (ie 8), If not just clean and rebuild the project under the Project tab of Eclipse.

I have run the same code in my eclipse and its works fine for me

hope this will solve your problem....!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM