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> <groupId>commons-logging</groupId> <artifactid>commons-logging </artifactId> <version>1.2</version> </dependency>
Basic Output rule: console output.
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:
<!--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