LOG4J Basic Configuration

Source: Internet
Author: User

First download log4j about the jar package, although now log4j updated to 2.0, but it is completely incompatible with the previous 1.X version, so today we still first to use the 1.X version, and so on a little longer, 2.0 version stable and the use of a large number of upgrades

The latest version of the 1.X version is 1.2.17, but now Log4j's official homepage is only 2.0 of the jar package download, so do not be confused by him, although 2.0 of the package download also has a 1.2 version of the package, but that is 2.0, not the previous 1.2.

We open the log4j version of the page to download
Address: http://archive.apache.org/dist/logging/log4j/
You can see that there are all previous versions of log4j, and 1 of them. X The latest version is the February 13 update of version 1.2.17.
We directly download:
Jar (Rack Package): Http://archive.apache.org/dist/logging/log4j/1.2.17/log4j-1.2.17.jar
Tar.gz (package + source package): http://archive.apache.org/dist/logging/log4j/1.2.17/log4j-1.2.17.tar.gz

After the download, we started the initial use of log4j

1. First put the jar package into the project Lib library

My project is the project of idea, so Lib is in the directory, but the normal JAVV Web application lib is the web directory

2. After placing the jar package, we add a configuration file using log4j
Put the configuration file in the Classpath directory, generally our normal situation is placed in the SRC root directory can be

123456789101112131415161718192021222324252627282930 #定义Log4j输出的日志级别#TRACE < DEBUG < INFO < WARN < ERROR < FATALlog4j.rootCategory=DEBUG,stdout#  Log4j提供的appender有以下几种:#  org.apache.log4j.ConsoleAppender(控制台)#  org.apache.log4j.FileAppender(文件)#  org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)#  org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生新文件)#  org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)log4j.appender.stdout=org.apache.log4j.ConsoleAppender#  Log4j提供的layout有以下几种:#  org.apache.log4j.HTMLLayout(以HTML表格形式布局),#  org.apache.log4j.PatternLayout(可以灵活地指定布局模式),#  org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),#  org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)log4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.threshold=DEBUG#    %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL#    %r 输出自应用启动到输出该log信息耗费的毫秒数#  %c 输出所属的类目,通常就是所在类的全名#  %t 输出产生该日志事件的线程名#  %n 输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n”#  %d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},输出类似: 20021018221028921#  %l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main(TestLog4.java:10)#    %f 输出文件名 如Test_Log4J.java#   [%c{1}就是将class名从右开始输出几个部分,输出的是,Test_Log4J.如果将1改为2,则输出为test.Test_Log4J.log4j.appender.stdout.layout.ConversionPattern=[%d{yyy-MM-dd HH:mm:ss,SSS}]-[%p]-[MSG!:%m]-[%c\:%L]%n

Explain the above configuration, although there are comments, I define STDOUT log4j output object, his output level is debug, from the above note, debug is greater than the trace level, but less than the info level, So normally this object will only output debug or debug above the level of information, and trace level of information he will ignore,
And then I set the output object he said to Consoleappender, which is the direct output to the console.
Finally, specify his output format Patternlayout:[%d{yyy-mm-dd hh:mm:ss,sss}]-[%p]-[msg!:%m]-[%c\:%l]%n
So the normal output of the information should be like this

1 [2013-11-1123:48:30,062]-[INFO]-[MSG!:测试]-[org.out.HelloOut:26]

Time-level-output information-Class of output

3. With the above configuration file, we write a test class, test whether the configuration is successful, can output the log
The code is as follows:

12345678910111213 package org.test;import org.apache.log4j.Logger;public class Log4jTest {    private static final Logger log = Logger.getLogger(Log4jTest.class);    public static void main(String[] args) { <span></span>  <span></span> <span></span>      <span></span>                 log.trace("TRACE:测试输出1");        <span></span>log.debug<span></span>("DEBUG:测试输出2");        log.info("INFO: Log4j 输出测试成功");    }}

4. After running the above code we will end up with the following result

12 [2013-11-12 00:19:16,562]-[DEBUG]-[MSG!:DEBUG:测试输出2]-[org.test.Log4jTest:18][2013-11-12 00:19:16,562]-[INFO]-[MSG!:INFO: Log4j 输出测试成功]-[org.test.Log4jTest:19]

We found that test 1 was not output, which means that our configuration was successful, only the debug level and the debug level of the log are output, and output to the console

5. Configuration succeeded.

LOG4J Basic Configuration

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.