Logging in Java

Source: Internet
Author: User

Common Logging:

Commons logging provides a unified log system access, basic Commons logging can achieve console output, of course Commons logging also provide other such as log4j access

The basic commons logging
    • Dependency:
    <dependency>            <groupId>commons-logging</groupId>            <artifactid>commons-logging </artifactId>            <version>1.2</version>        </dependency>

Basic Output rule: console output.

    • Basic methods of Use
Package Com.test;import org.apache.commons.logging.log;import org.apache.commons.logging.logfactory;/** * Created by Ygshen on 7/23/16. */public class MainClass {   //Logger factory queries the implementation of logging to define a specific log method, if no log implementation such as LOG4J is defined   // Logging is using the default console output    private static Log logger= Logfactory.getlog (mainclass.class);    public static void Main (string[] args) {        logger.info ("Test Information");        Logger.error ("Test error message");}    }

Apply Advanced Rule log4j:

Common logging can provide basic support for other advanced logging. Like log4j.

Logging4j+common-logging How to use:

    • Dependency:
<!--https://mvnrepository.com/artifact/log4j/log4j--><dependency>    <groupid>log4j</ groupid>    <artifactId>log4j</artifactId>    <version>1.2.17</version></ dependency><!--Https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core-->< dependency>    <groupId>org.apache.logging.log4j</groupId>    <artifactid>log4j-core </artifactId>    <version>2.5</version></dependency><dependency>    < groupid>org.apache.logging.log4j</groupid>    <artifactId>log4j-api</artifactId>    <version>2.6.2</version></dependency>

    • Custom commons-logging.properties, log4j.properties configuration file

Commons-logging.properties

Org.apache.commons.logging.log=org.apache.commons.logging.impl.log4jlogger

Log4j.properties: This configuration file is mainly used to configure the Appender output target, mainly including Consoleappender, Fileappender

#定义成默认的Console输出
log4j.rootlogger=debug,stdout### output to console # # #log4j. appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.target=system.out## Output log log4j.appender.stdout.threshold=infolog4j.appender.stdout.layout= above the info level Org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern=%d{absolute}%5p%c{1}:%L-%m%n

# Define The root logger with Appender filelog=/home/ygshen/workspace/logslog4j.rootlogger =info,file### output to log file # # # Log4j.appender.file = Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.file.File = ${log}/ Log.outlog4j.appender.file.Append = true# Define The layout for file appenderlog4j.appender.file.layout= org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversionpattern=%m%n

    • Java code is used with Commons logging

Logging in Java

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.