Using the log4j log tool in Java applications

Source: Internet
Author: User
Tags log4j

Creating a journal in a project is necessary for a project, now used more widely, more popular log tools have log4j; This log tool is actually very convenient to use; I know her is also deep, but at the time when the need to use is really unprepared, know how to configure but just do not know how to use, Waste a lot of time;
Today is the way to write a quick start, hoping to give you a quick start, and what is the first goal of being a programmer. Of course it is to realize your ideas, to achieve your goal is the King; then we use log4j this tool to do what. Of course, to make it easier for us to use, no, it makes me feel like seven turns. First of all, this method may not be so reasonable, or optimal, but you like it, I use it in the project, will have an impact on your system performance. I do not know, to analyze themselves;
Then cut the crap and get into the subject;

The first step: down a log4j binary package: http://logging.apache.org/log4j/docs/download.html

Step Two: Create a new project in Eclipse and import the Log4j.jar package into the project.

The third step: Create a new log4j package, and then build a Java file Testlog4j.java under the package, the code is as follows:

Package log4j;

Import Org.apache.log4j.Logger;

public class Testlog4j {

public void log4j () {
Logger Log=logger.getlogger (This.getclass (). GetName ());
System.out.println ("Test log4j");
Log.fatal ("testlog4j + Bonze");
}

public static void Main (string[] args) {
testlog4j tl=new testlog4j ();
Tl.log4j ();
}
}

Run, as if not right. Oh, of course not, there is a properties file to configure it;
In the root file directory of the LOG4J project, create a new text file file that reads:

#log4j. Rootlogger = DEBUG, A1
#log4j. Rootlogger = INFO, A1
Log4j.rootlogger = ERROR, A1
#这里定义的是error级别的, in fact, with the info-class on it, anyway, there are 6 levels you look at using;
#log4j. appender.a1 = Org.apache.log4j.ConsoleAppender
Log4j.appender.A1 = Org.apache.log4j.FileAppender

Log4j.appender.A1 = Org.apache.log4j.RollingFileAppender
Log4j.appender.A1.File =log4j.log
#定义日志的存放路径和文件名;
log4j.appender.a1.maxfilesize=500kb
#当log文件大于500K的时候删除重新建;
log4j.appender.a1.maxbackupindex=10
Log4j.appender.A1.layout = Org.apache.log4j.PatternLayout
#使用的是自定义的日志格式, here are some of the custom parameters;
#log4j. Appender.A1.layout.ConversionPattern =%-4r [%t]%-5p%c%x-%m%n
Log4j.appender.A1.layout.ConversionPattern = [%d] [%t]%m%n

Run it, hehe · Now finally create a log4j.log file in your log4j project directory; congratulations.
Success, this is just a very simple, fast way to get started, the specific in-depth understanding of the need for your own slowly learning slightly,
Only after the effect will have the motivation to learn, now you can in their own projects to achieve log4j, the other slowly refueling bar

 

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.