Brief introduction and example of log4j

Source: Internet
Author: User
Tags log log log4j

Log4j is really simple, simple to the point of outrageous. Not to log it. So I'll give you a log, and then you can write something with log, and first come to a complete class example:

Package test;

import Org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

Public class Test {

Static Log log = Logfactory.getlog (Test.class);

Public void log () {

Log.debug ("Debug Info.");

Log.info ("info info");

Log.warn ("Warn info");

Log.error ("error info");

Log.fatal ("Fatal info");

}

Public Static void Main (string[] args) {

Test test = new test ();

Test.log ();

}

}

Log Level

log4j The log information into five levels by default

Debug < Info < Warn < error < fatal

Although I can add the level, but I think there is no need, five is enough. When you write the information, you classify the information as one of the five levels, and then call the corresponding function.

What is the use of grading five grades? Where did the log information go?

"Logfactory.getlog (Test.class)" What is the meaning of pinch. Then look down.

Inheriting Ideas

The key point of log4j is that it inherits ideas. That is, a log can inherit another log's attributes (where output goes, log rank, log format, and so on). How to inherit.

Log4j is based on the name of the log to determine the inheritance relationship, such as:

The log named "Com.zhlmmc.lib" is the parent of "Com.zhlmmc.lib.log". LOG4J also has a rootlogger, which is equivalent to Java object.

Third, look back at "logfactory.getlog (Test.Class)" Here, "test.class" In fact, is the complete path of the test class (package name + class name), "Test." Test ". So if there is a "test" log, then test this log inherits it, otherwise it inherits Rootlogger.

defining a Property file

Log4j.rootlogger=info, stdout

Log4j.appender.stdout=org.apache.log4j.consoleappender

Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

# to output the caller ' s file name and line number.

log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l)-%m%n

Analyze:

First line, configure Log4j.rootlogger you know. It should be the root, must be configured, otherwise the other log inherit what ah. The other log can be configured or not configured. The first parameter after the equal sign represents the log level, one of five levels, followed by a parameter that lets log know where the output is, and if you want the log output to two places, add two output parameters, such as:

Log4j.rootlogger=info, stdout, file

The info here indicates that log level is info, and all logs with less than info are not logged. For example, using this configuration file, the class that I just started lifting

Log.debug ("Debug Info.");

This sentence is not working because the debug level is less than info. This makes it easy to control what information is displayed when debugging and what information is removed when it is released. These are not changed code, it is very convenient.

But what about stdout and file?

Then look down, is the configuration of stdout, the name is random, you can call it a:

Log4j.appender.a=org.apache.log4j.consoleappender

Then the above Rootlogger parameter stdout also must change to a, the other use place of course also must change. The key here is not the name, but the Appender type, such as the "Consoleappender" here, see the bar, output to console. The next two lines are set log format, usually you copy it.

In the actual project development, it is likely to encounter the quoted package log4j to log, such as Hibernate. So here you can easily control how the packet logs. For example, add a line to the above configuration file:

Log4j.logger.org.hibernate=fatal

Then all the classes below the Org.hibernate package will show very little information because the fatal is the highest level.

Deploy

Put the log4j bag and the commons-logging bag (add together only two) under Classpath. Then save the configuration file as Log4j.properties, also under CLASSPATH (if you use Eclipse, put it in the SRC directory). And then you can run.

In the process of loading, the program reads/web-inf/log4j.properties this file.
The configuration file is explained as follows :

The

#log4j中有五级logger FATAL ERROR WARN infodebug
#配置根Logger with the syntax:
#log4j. Rootlogger = [level], Appendername, app Endername, ...
Log4j.rootlogger=info, A1, R
#这一句设置会把所有级别的log都输出
#如果为log4j. Rootlogger=warn, means that only warn,error,fatal is exported, The Debug,info will be screened out.
# A1 is set to be a consoleappender.
#log4j中Appender有几层如控制台, files, GUI components, even socket servers, NT Event loggers, UNIX Syslog daemons, and so on
#ConsoleAppender输出到控制台
Log4j.appender.a1=org.apache.log4j.consoleappender the output layout used by the
# A1, where the log4j provides 4 layouts. Org.apache.log4j.HTMLLayout (layout in HTML table)
#org. Apache.log4j.PatternLayout (You can specify layout patterns flexibly),
# Org.apache.log4j.SimpleLayout (The level and information string that contains the log information),
#org. Apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)

Log4j.appender.a1.layout=org.apache.log4j.patternlayout
#灵活定义输出格式 specifically view log4j Javadoc org.apache.log4j.PatternLayout
#d time ....
Log4j.appender.a1.layout.conversionpattern=%-d{yyyy-mm-dd HH:MM:SS} [%c]-[%p]%m%n
#R output to the file rollingfileappender extension, you can provide a backup function of the log.
Log4j.appender.r=org.apache.log4j.rollingfileappender
#日志文件的名称
Log4j.appender.r.file=log4j.log
#日志文件的大小
log4j.appender.r.maxfilesize=100kb
# Save a backup file
Log4j.appender.r.maxbackupindex=1

Log4j.appender.r.layout=org.apache.log4j.ttcclayout
#log4j. Appender.r.layout.conversionpattern=%-d{yyyy-mm-dd HH:mm:ss} [%c]-[%p]%m%n



# level: Is the priority of logging, divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all, or levels you define.

#Log4j建议只使用四个级别, the priority from highest to lowest is error, WARN, INFO, DEBUG.

 

#Log4jTest Logger logger = Logger.getlogger in Java (This.getclass (). GetName ()); may have LOG4J.ROOTLOGGER=DEBUG,CONSOLE,A1 configuration

Log4j.rootlogger=debug,console,a1

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.