Logging in Java
Common logging:
Log4j1仅仅作为一个实际的日志框架,commons-logging作为门面,统一各种日志框架的混乱格局
基本的commons logging
- Dependency:
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency>
基本输出规则: console输出。
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 会查询logging的实现来定义具体的Log方式,如果没有定义诸如log4j的log 实现 那么 //logging使用的是默认的console 输出 private static Log logger= LogFactory.getLog(mainclass.class); public static void main(String[] args){ logger.info("test information"); logger.error("test error message"); } }
应用实现Log4J1:
common logging 可以提供其它高级logging的基础支持。比如log4j
Logging4J+common-logging 使用方法:
- Dependency:
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
- 自定义commons-logging.properties, log4j.properties配置文件
commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
Log4J.properties: 这个配置文件 主要是用来配置Appender的即输出目标,主要包括 consoleAppender, FileAppender
#定义成默认的Console输出
log4j.rootLogger=debug,stdout ### 输出到控制台 ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out ## 输出INFO级别以上的日志 log4j.appender.stdout.Threshold=INFO log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Define the root logger with appender file log=/home/ygshen/WorkSpace/logs log4j.rootLogger =info,file ### 输出到日志文件 ### log4j.appender.file = org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File = ${log}/log.out log4j.appender.file.Append = true # Define the layout for file appender log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.conversionPattern=%m%n
- Java代码同commons logging 中使用的。
这里代码里所使用到的Log 以及 LogFactory都是commons-logging自身的类
单独使用Log4J1
也可以不借助commons-logging 而单独使用log4j
- Dependency 同上
- log4j.properties同上
- Java代码
package com.test; // 单独使用 Log4J import org.apache.log4j.Logger; /** * Created by ygshen on 7/23/16. */ public class mainclass { // 获取logger的方式与使用commons logging 不同 private static Logger logger= Logger.getLogger(mainclass.class); public static void main(String[] args){ logger.info("Test pure log4j logging"); } }
Logback
使用logback+slf4j的方式同一开始介绍的commons-logging + log4j1 方式基本相同,这里commons-log=slf4j充当门面。而logback和log4j1的角色相同充当具体Logr的实现
1. Dependency
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.7</version> </dependency> <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.7</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency>
2. 配置文件: 这里配置文件只能是*.xml. 首先找的是logback.groovy 没有的话找logback-test.xml 还没有找logback.xml
这里配置一个logback.xml做为例子,实际工作中可能考虑到不同环境的logback文件 后续会讨论到
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="LOG_DIR" value="/home/ygshen/WorkSpace/logs"/> <property name="LOG_FILE_NAME" value="logs.log"/> <!-- Output to File and Rotate if it's necessary --> <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_DIR}/${LOG_FILE_NAME}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${LOG_DIR}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}.%i</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- or whenever the file size reaches 100MB --> <maxFileSize>100MB</maxFileSize> <maxHistory>10</maxHistory> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%date [%-5thread] %-5level [%-10logger] %-10msg%n</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="ROLLING"/> </root> </configuration>
3. 代码部分
package com.test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by ygshen on 7/23/16. */ public class mainclass {
// 这里所使用到的Logger 和LoggerFactory都是slf4j的类充当loggback的门面 private static Logger logger = LoggerFactory.getLogger(mainclass.class); public static void main(String[] args){ logger.info("This is logger test for slf4j and logback"); } }
最后分析以下: http://www.cnblogs.com/ygshen/p/4498210.html