# Common log4j Configuration

Source: Internet
Author: User

Log4j configuration is commonly used. Two methods are generally used:. properties and. xml. The following two simple examples are provided:
1. log4j. Properties

### Set Org. level info, debug, warn, error, and output location a1, a2 # log4j.category.org. zblog = error, A1 log4j.category.org. zblog = info, A2 log4j. appender. a1 = org. apache. log4j. consoleappender ### set the output location A1 to leleappender (console) ## log4j. appender. a1.layout = org. apache. log4j. patternlayout ### set the output layout format patterlayout of A1. (you can flexibly specify the layout mode.) # log4j. appender. a1.layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS, SSS} [% C]-[% P] % m % N ### configure the log output format ## log4j. appender. a2 = org. apache. log4j. rollingfileappender ### set the output location A2 to the file (a new file is generated when the file size reaches the specified size) # log4j. appender. a2.file = E:/study/log4j/zhuwei.html ### File Location ## log4j. appender. a2.maxfilesize = 500kb ### file size # log4j. appender. a2.maxbackupindex = 1 log4j. appender. a2.layout = org. apache. log4j. htmllayout
HTML output

Ii. log4j. xml

<? XML version = "1.0" encoding = "gb2312"?> <! Doctype log4j: Configuration System "log4j. DTD "> <log4j: configuration xmlns: log4j =" http://jakarta.apache.org/log4j/ "> <appender name =" org. zblog. all "class =" org. apache. log4j. rollingfileappender "> <! -- Set the channel ID: Org. zblog. all and output mode: Org. apache. log4j. rollingfileappender --> <Param name = "file" value = "E:/study/log4j/All. output. log "/> <! -- Set the file parameter: log output file name --> <Param name = "APPEND" value = "false"/> <! -- Set whether to add new logs to the base of the original log when the service is restarted --> <Param name = "maxbackupindex" value = "10"/> <layout class = "org. apache. log4j. patternlayout "> <Param name =" conversionpattern "value =" % P (% C: % L)-% m % N "/> <! -- Set the output file project and format --> </layout> </appender> <appender name = "org. zblog. zcw "class =" org. apache. log4j. rollingfileappender "> <Param name =" file "value =" E:/study/log4j/zhuwei. output. log "/> <Param name =" APPEND "value =" true "/> <Param name =" maxfilesize "value =" 10240 "/> <! -- Set the file size --> <Param name = "maxbackupindex" value = "10"/> <layout class = "org. apache. log4j. patternlayout "> <Param name =" conversionpattern "value =" % P (% C: % L) -% m % N "/> </layout> </appender> <logger name =" zcw. log "> <! -- Set the domain name limit, that is, the zcw. log domain and the following logs are output to the corresponding channel below --> <level value = "debug"/> <! -- Set level --> <appender-ref = "org. zblog. zcw"/> <! -- Corresponds to the preceding channel id --> </logger> <root> <! -- Set the channel for receiving all outputs --> <appender-ref = "org. zblog. All"/> <! -- Corresponds to the preceding channel id --> </root> </log4j: configuration>

Iii. configuration file loading method:

Import Org. apache. log4j. logger; import Org. apache. log4j. propertyconfigurator; import Org. apache. log4j. XML. domconfigurator; public class log4japp {public static void main (string [] ARGs) {domconfigurator. configure ("E:/study/log4j/log4j. XML "); // load. XML file // propertyconfigurator. configure ("E:/study/log4j/log4j. properties "); // load. properties file logger log = logger. getlogger ("org. zblog. test "); log.info (" test ");}}

4. project use log4j
In a web application, you can load the configuration file in a separate servlet, and configure the servlet in Web. XML to load when the application starts. In a multi-person project, you can set an output channel for each person, so that each person can use his/her own domain name when building a logger, output debugging information to your own log file.

5. Common output formats #-X: Left alignment when X information is output; # % P: log information level # % d {}: log information generation time # % C: log information location (Class Name) # % m: generated log details # % N: Output log information line feed

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.