Log4j-using a detailed and Java instance application

Source: Internet
Author: User
Tags log4j

Reprint Please specify source: http://blog.csdn.net/qq_26525215

This article is from the "university trip _ Remember the blog"

log4j Introduction

Log4j, the specific I will not copy the introduction of the official website, as long as we know that this is to help us record the log on it.

The role of these 2:
1, tracking the running track code.
2. Output Debugging information

It's simple, but it's a really useful tool! Do not need to be very deep to understand, but must know how to use, how to record the log through log4j, this is necessary!

Here, an example is used to demonstrate how to use log4j, and how to configure the log4j configuration file.

Log4j-jar Package:

First, we need to prepare a log4j jar package, not much to say, directly on the link:
Version is: log4j-1.2.17. (the first version downloaded in 2016.8.22) '

point me to download the Log4j-1.2.17-jar package

The three major components are the following three classes:
1. Logger class-Generate log.
2. Appender class-Defines the destination of the log output.
3. Layout class-Specifies the format of the log output.

Check out the Logger components:

The logs are divided into 5 levels (on, off and off), sorted from high to low (from severe to slight):

fatal-serious errors that may cause the program to not function properly. Like System.exit (), the entire project is not running
error– error, affecting the normal operation of the program. Like the current thread is hung, but other threads or components are still running
The warn– warning does not affect the running of the program. It is a good idea to fix this issue.
info– General information. Does not affect the operation of the program. May be the user's debugging information. Dispensable. Equivalent to System.err.
debug– General information. Equivalent to System.out

The above five levels correspond to the following methods (called in Java, and so will be shown)
Fatal (Object)
Error (Object)
Warn (Object)
Info (Object)
Debug (Object)
The log is output when the information is greater than or equal to the configured level.

Appender components

We just have to remember these 2:

console-Console –org.apache.log4j.consoleappender
file-file –org.apache.log4j.fileappender, Org.apache.log4j.RollingFileAppender

Layout component

function is to define the output format of the log
Org.apache.log4j.HTMLLayout
org.apache.log4j.PatternLayout (Custom)
Org.apache.log4j.SimpleLayout

is the output to the log, in which format output, the understanding of the regular expression is good.

There are some escapes that he rules well, I'm going to list some here, don't write it all down, understand it, and then remember a common match format ok!

Only the custom format patternlayout is interpreted:

%d – 具体的时间. %d{yyyy MMM dd HH:mm:ss,SSS} 输出类似:输出类似:2016年8月21日 00:00:00,921%p – 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL %c – 输出所属的类目,通常就是所在类的全名 -Logger.getLogger(name) 其实就是输出这个name,所以,我们这里一般用当前类的class。%L(这是大写的L) — 只输出行号%l - 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。举例:(Log4jDemo.java:13)%m – 输出代码中指定的消息-我们指定的%n —换行符-Windows平台为“\r\n”,Unix平台为“\n” %r 输出自应用启动到输出该log信息耗费的毫秒数 %t 输出产生该日志事件的线程名
A template for the configuration file:

Create a log4j.properties file under the Src/bin directory (that is, web-inf/classes).

In the Web-inf/classes directory, the default directory for log4j. Log4j.properties is also the default file name for log4j.

# # # #配置根元素Log4j. Rootlogger=info,console,file#存储INFO以及以上级别的记录#file的输入地址为文件#console表示输出到控制台Log4j. Appender. File=org. Apache. Log4j. Rollingfileappender#以下是具体的目录Log4j. Appender. File. File=d:/a/test. Log#每个文件的最大大小Log4j. Appender. File. MaxFileSize= -Kb#最多可以是多少个文件Log4j. Appender. File. Maxbackupindex=3#信息的布局格式-typed in the specified formatLog4j. Appender. File. Layout=org. Apache. Log4j. Patternlayout#具体的布局格式-%d is time#log4j. appender.file.layout.conversionpattern=%d%p [%c]%m%l%nLog4j. Appender. File. Layout. Conversionpattern=%d%p [%l]\:%m-%t%n#以下配置输出到控制台的配置Log4j. Appender. Console=org. Apache. Log4j. ConsoleappenderLog4j. Appender. Console. Layout=org. Apache. Log4j. Patternlayout#log4j. appender.console.layout.conversionpattern=%d%p [%c]%m%nLog4j. Appender. Console. Layout. Conversionpattern=%d%p [%l]\:%m-%t%n
Demo Example:
 PackageCn.hncu.lib;ImportOrg.apache.log4j.Logger;/** * @author Chen Haoxiang * * 2016-8-23 * * Public  class log4jdemo {     Public Static void Main(string[] args) {//The name of the log is any string passed in, usually a class name or package nameLogger log = Logger.getlogger (Log4jdemo.class); for(intI=0;i<Ten; i++) {Log.fatal ("Critical Error message:"+i); Log.error ("error message:"+i); Log.warn ("warning message:"+i); Log.info ("General Information:"+i); Log.debug ("Debug Info:"+i); }    }}
Demo Result: Console output:

File output:

is not very simple, after the configuration of the file, we use in Java, only 2 steps.
1, Logger log = Logger.getlogger ( **** . Class);
2, randomly call the following 5 methods:

log.fatal();log.error();log.warn();log.info();log.debug();参数是Object类型!在日志中用%m来代表我们这里传的值的toString()。

If you want to know more details, you can go to log4j official website:

LOG4J official website

Reprint Please specify source: http://blog.csdn.net/qq_26525215

This article is from the "university trip _ Remember the blog"

Log4j-using a detailed and Java instance application

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.