Using log4j in Eclipse

Source: Internet
Author: User
Tags log4j

Writing programs on the Java platform is also a bit of a time, from the former Java-ee to the current background program, more or less used to the log4j to output the log, but each time the use of the others are good, and then tell me how to use, and then directly call the good, has been log4j in the state of the half solution. Today, with the understanding of its own, know the reason why the attitude, carefully studied the next log4j.

1, the use of log4j in eclipse

For programmers, the quickest way is to get started first, see the intuitive effect is said, so the first step is how to make your program in eclipse inside use.

The first step: first, create a new project Minatest, then add package, called testlog4j, and add the following code:

Package testlog4j;


Import Org.apache.log4j.Logger;
Import Org.apache.log4j.BasicConfigurator;
Import Org.apache.log4j.Level;

public class Testlog4j {
public static void Main (String argv[]) {

Create a logger by the name of Class Log4jtest.
Logger Logger = Logger.getlogger (testlog4j. Class);

Use the default configuration.
Basicconfigurator.configure ();

Set the logger level to Level.info
Logger.setlevel (Level.info);

This request would be disabled since Level.debug < Level.info.
Logger.debug ("This is Debug.");

These requests would be enabled.
Logger.info ("This is a info.");
Logger.warn ("This is a warning.");
Logger.error ("This is an error.");
Logger.fatal ("This is a fatal error.");

Return
}
}

Step Two: Add the Log4j.jar package to the project

After the code is written, there is a syntax error in eclipse, because Eclipse doesn't know where to import the corresponding class. At the current project point in Package Explorer, right-click->property->java build Path, select Libraries->add JARS on the tab page, and add the jar package for the downloaded log4j. After "OK", you will find that in the current project in Package Explorer, in addition to the previous JRE System Library, there is a referenced libraries, which is the added jar package. After clean, there is no grammatical error.

This time, if we run as application, we get the following hint:

Log4j:warn No Appenders could is found for logger (Com.foo).
Log4j:warn Please initialize the log4j system properly.
Log4j:warn http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Because the program cannot find the Log4j.properties profile. So, the following is to add this configuration file to the current project.

Step three: Add Log4j.properties to the current project

1 Right-click on the current project. New->folder, in the pop-up dialog box "folder name" type the name of a folder, here with log4j. Then click on "Advanced", select "link to folder in the" File system, "Browse" a directory, general select the current project directory, and then enter the associated folder name, such as d:/workspace/minatest/ Log4j. This creates an associated directory. Then in Package Explorer to find the newly created file log4j folder, import your defined log4j.properties (import process Province), if not ready-made, the following can be used as a reference:


Log4j.rootlogger=debug, R, O

# Stdout
Log4j.appender.o=org.apache.log4j.consoleappender

# File
Log4j.appender.r=org.apache.log4j.rollingfileappender
Log4j.appender.r.file=log4j.log

# Control the maximum log file size
log4j.appender.r.maxfilesize=100kb

# Archive log files (one backup file here)
Log4j.appender.r.maxbackupindex=1

Log4j.appender.r.layout=org.apache.log4j.patternlayout
Log4j.appender.o.layout=org.apache.log4j.patternlayout

Log4j.appender.r.layout.conversionpattern=[%d{iso8601}]%5p%6.6r[%t]%x-%c.%m (%f:%l)-%m%n
Log4j.appender.o.layout.conversionpattern=[%d{iso8601}]%5p%6.6r[%t]%x-%c.%m (%f:%l)-%m%n

I'm going to say it in the first minute.

Fourth Step compilation

In But before package (testlog4j) Right-click, run As->run configuration, and then select Classpath in the tab page of the pop-up page, select the current project "Minatest" and find the right " Advanced "Enter the optional state, after clicking, will then pop up a dialog box, select" Add Folders ", find the current project, select the Log4j folder, and then determine. In this way, we add log4j.properties to the current project's class path. Select Run. View the console for Eclipse

[2010-06-11 19:44:42,523] Info 0[main]-testLog4j.testLog4j.main (testlog4j.java:24)-this is a info.
[2010-06-11 19:44:42,533] WARN 10[main]-TestLog4j.testLog4j.main (testlog4j.java:25)-this is a warning.
[2010-06-11 19:44:42,533] Error 10[main]-TestLog4j.testLog4j.main (testlog4j.java:26)-This is a error.
[2010-06-11 19:44:42,533] FATAL 10[main]-TestLog4j.testLog4j.main (testlog4j.java:27)-This is a FATAL error.

In this case, you can debug the code side of the eclipse.

2 Log4j.properties various interpretations

Detailed interpretation can go to see the relevant documents, here only to say the format, to help get started

The basic format of the log4j configuration file is as follows:

#配置根Logger
Log4j.rootlogger = [level], appenderName1, appenderName2, ...

Looking back at the above

Log4j.rootlogger=debug, R, O

Here R,o is two appdername, of course, it can be another name.

Down, is to define different attributes for these two appender.

First Look at Appender O

Log4j.appender.o=org.apache.log4j.consoleappender definition output to console

Log4j.appender.o.layout=org.apache.log4j.patternlayout defines the layout of the output

Log4j.appender.o.layout.conversionpattern=[%d{iso8601}]%5p%6.6r[%t]%x-%c.%m (%f:%l)-%m%n output format

Looking at Appender R, the difference is that the output is not the same place

Log4j.appender.r=org.apache.log4j.rollingfileappender
Log4j.appender.r.file=log4j.log

This means that the output is in the file

As a result, the program still has a log4j.log file under the current project.

Brief introduction, detailed documentation Google or read the official document directly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.