Java uses LOG4 to log logs

Source: Internet
Author: User
Tags log4j

We use in the system, in order to facilitate the search for problems, it is necessary to record the operation of the log, and the current relatively mature and stable program logging method is LOG4, I am also a rookie, and then study in the study on the record of the use of the way to facilitate future access, at the same time this article refers to the Blog Park:/http Www.cnblogs.com/zhangpengshou/p/5128050.html's article, thank you for that.

LOG4 has a 1.x version and a 2.x version, the use of two versions is very different, in order to facilitate the use of the 2.x version.

The code is simple, first to configure the XML, to create Log4j2.xml in the class's sibling as follows:

<?XML version= "1.0" encoding= "UTF-8"?><ConfigurationStatus= "Error">    <!--define all the Appender first -    <appenders>        <!--The configuration of this output console -        <Consolename= "Console"Target= "System_out">            <!--This is the format of the output log -            <Patternlayoutpattern= "%d{hh:mm:ss." SSS}%-5level%class{36}%l%M-%msg%xex%n "/>        </Console>        <!--The file will print out all the information, and this log will be automatically emptied each time the program is run, determined by the Append property, suitable for temporary testing -        <Filename= "Error"FileName= "E:/logs/error.log"Append= "false">            <!--console only outputs level and above information (Onmatch), other direct rejections (Onmismatch) -            <Thresholdfilter Level= "Error"Onmatch= "ACCEPT"Onmismatch= "DENY"/>            <Patternlayoutpattern= "Record time:%date%n thread Id:[%thread]%n log level:%-5level%n record Location:%location%n message Description:%property{message}%n exception:%exception%n message:% message%newline%n------------------------------------------%n "/>        </File>        <!--This will print out all the information, each time the size exceeds size, the size of the log will be automatically saved by the year-month folder created under and compressed, as the archive -        <Rollingfilename= "Rollingfile"FileName= "E:/logs/history.log"Filepattern= "log/$${date:yyyy-mm}/history-%d{mm-dd-yyyy}-%i.log.gz">            <Patternlayoutpattern= "Record time:%date%n thread Id:[%thread]%n log level:%-5level%n record Location:%location%n message Description:%property{message}%n exception:%exception%n message:% message%newline%n------------------------------------------%n "/>            <Sizebasedtriggeringpolicysize= "50MB"/>        </Rollingfile>    </appenders>    <!--then define the logger, only the appender,appender that define the logger and introduce will take effect -    <Loggers>        <!--Create a default root logger -        <Root Level= "Trace">            <Appender-refref= "Error"/>            <Appender-refref= "Rollingfile"/>            <Appender-refref= "Console"/>        </Root>    </Loggers></Configuration>

There are comments in the code, this does not do a detailed explanation, about LOG4 other configuration can Baidu a bit. Java code is used in the following ways:

ImportJava.io.File;Importjava.util.List;Importorg.dom4j.*;ImportOrg.dom4j.io.SAXReader;ImportOrg.apache.logging.log4j.LogManager;ImportOrg.apache.logging.log4j.Logger; Public classHello {Private StaticLogger Logger = Logmanager.getlogger (Hello.class);  Public Static voidMain (string[] args) {Logger.debug (Test); Logger.error ("Error Message"); }}

Acknowledgement: http://www.cnblogs.com/zhangpengshou/p/5128050.html

Java uses LOG4 to log logs

Related Article

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.