Getting started with Apache log4j log tools [Maven project configuration]

Source: Internet
Author: User
Tags log log

A brief introduction to the use of Org.apache.log4j.Logger in the MAVEN project.
[1] First we need to find the coordinates of Org.apache.log4j.Logger and configure to Pom.xml

<Dependency>    <groupId>Log4j</groupId>    <Artifactid>Log4j</Artifactid>    <version>1.2.14</version>    <Scope>Provided</Scope></Dependency>

[2] By default, you need to add a configuration file under the Src/main/java folder Log4j.properties, the specific content can be referenced below a simple example:

Log4j.rootlogger=console,file,stdout
Log4j.addivity.org.apache=true

# # # \u8f93\u51fa\u5230\u63a7\u5236\u53f0 # #
Log4j.appender.stdout = Org.apache.log4j.ConsoleAppender
Log4j.appender.stdout.Target = System.out
Log4j.appender.stdout.layout = Org.apache.log4j.PatternLayout
Log4j.appender.stdout.layout.ConversionPattern = [Framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n

#\u5e94\u7528\u4e8e\u6587\u4ef6
Log4j.appender.file=org.apache.log4j.fileappender
Log4j.appender.file.file=f:/log4j/file.log
Log4j.appender.file.append=false
Log4j.appender.file.encoding=gbk
Log4j.appender.file.layout=org.apache.log4j.patternlayout
Log4j.appender.file.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n

Note: The above example prints log log information to the console and F:/log4j/file.log files. Simple properties such as append control whether the log is overwritten, Conversionpattern is the format of the log output, etc. do not repeat here. Want to deep ♂ into the understanding of friends can continue to search degrees Niang other collar ♂ domain. The Deep dark fantasy ...
Note: I do not know why it is written to find that the annotation seems to have something wrong. I seem to have seen these bars of excellent hexadecimal Sanskrit, seemingly can be translated into Chinese text. I was so frightened that I lost ♂ color when I finished the translation. This occurs when pasting Chinese characters into the. properties file of Eclipse.

[3] Finally only need to write the program debugging can be

ImportOrg.apache.log4j.Logger;//import Org.apache.log4j.PropertyConfigurator; Public classRockderiai {Private StaticLogger Logger = Logger.getlogger (rockderiai.class);  Public Static voidMain (string[] args) {//propertyconfigurator.configure ("f:/config/log4j.properties");Logger.debug ("-----"); }}

Note: It is not a good choice to put the log4j.properties file in the Src/main/java folder in the actual project. So at the very beginning of the program, you can configure the location of the log4j.properties using the log4j's own configurator propertyconfigurator. The part that is commented out in the code above. The Configurator overrides the default path and throws an exception if the Configurator path is incorrect. The Configurator is said to have several other, not to be described here.

Getting started with Apache log4j log tools [Maven project 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.