Configuration and use of log4j

Source: Internet
Author: User

Benefits of using logs:

Log information can be saved for a long time.

Logs can be saved to: network, file, database

Sets the level of the log.


Fatal–system.exit (0); -JVM,

error– error, module error.

warn– Warning

Info-Programmer Debug Information-equivalent to System.err. And that's how we often output it.

Debug-system.out. – In general, the frame is used out.


Set Print level Up compatibility

LOG4J requires two files:

1, Guide Package Log4j.jar

2, log4j configuration file, this profile, must be located in Classpath, must be called: Log Four components:

logger– Log class, set the log class. Design the root log.

appener– Append object. Log output target.

layout– the format of the log

Level levels

1 log4j.properties2 3 4 #设置logger的根日志, level, A1 refers to the target of the output5 Log4j.rootlogger=on,console,file6 #设置日志的appender-The target of the output7 #其他的还是RollingFileAppender8 Log4j.appender.console=org.apache.log4j.consoleappender9 #日志的格式, the format of the description log is specified by the program itselfTen Log4j.appender.console.layout=org.apache.log4j.patternlayout One #%d=date,%p=preority level, ALog4j.appender.console.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss:SSS}%-5p%-10t%c<%l>%m%n -  -  the Log4j.appender.file=org.apache.log4j.rollingfileappender - #设置文件名 - Log4j.appender.file.file=d:/a/a.log - #设置文件最大大小 + log4j.appender.file.maxfilesize=10kb - #设置文件最多备份几个 + log4j.appender.file.maxbackupindex=3 A #日志的格式, the format of the description log is specified by the program itself at Log4j.appender.file.layout=org.apache.log4j.patternlayout -Log4j.appender.file.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss:SSS}%-5p%-10t%c<%l>%m%n

Using Log4jdemo

1  PackageCn.test.demo;2 3 ImportOrg.apache.log4j.Logger;4 5  Public classLogdemo {6     StaticLogger Logg=logger.getlogger (Logdemo.class);7      Public Static voidMain (string[] args) {8Logg.fatal ("Fatal ...");9Logg.error ("Error ...");TenLogg.warn ("Warn ..."); ("Info"); ALogg.debug ("Debug"); -System.err.println ("Over"); -     } the}

Configuration and use of log4j

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: 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.