I need t开发者_开发问答o know how to initialise and use Log4j in java.... When i try to use it i get the following error
log4j:WARN No appenders could be found for logger (Main). log4j:WARN Please initialize the log4j system properly.
You could place a log4j.properties
into your src
folder, this is automatically checked for log4j configuration. A default configuration could look like this.
### Root Logger
log4j.rootLogger=DEBUG, Console
### Console Appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d (%t) %-5p %c: %m%n
Otherwise you could call the default configuration programmatically.
import com.foo.Bar;
// Import log4j classes.
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
public class MyApp {
// Define a static logger variable so that it references the
// Logger instance named "MyApp".
static Logger logger = Logger.getLogger(MyApp.class);
public static void main(String[] args) {
// Set up a simple configuration that logs on the console.
BasicConfigurator.configure();
logger.info("Entering application.");
Bar bar = new Bar();
bar.doIt();
logger.info("Exiting application.");
}
}
Taken from the short introduction document of Apache log4j. You should look for similiar questions before asking them redudant.
Consider to switch to SLF4J which enables you the possibilities to switch the logging implementation easily.
First, read this: http://logging.apache.org/log4j/1.2/manual.html
Shortly the following property is missing when you are running JVM:
-Dlog4j.configuration=mylog4jconfig.xml
BUT: your question shows that you are just starting using log4j. Please note that it is already obsolete by its author, Ceki Gülcü, that implemented new logger for java and recommends people to use it. See Logback (http://logback.qos.ch/) and SLF4J
You need to have the log4j.properties file in your classpath.
精彩评论