log4j
是目前最流行的java开源日志处理框架 , 是Apache的子项目。
官方网站:https://logging.apache.org/log4j/1.2/index.html
log4j2
是对Log4j 的升级版本,参考了logback 的一些优秀的设计,并且修复了一些问题,因此带来了一些重大的提升。
官方网站: https://logging.apache.org/log4j/2.x/index.html
logback
是由log4j创始人设计的另一个开源日志组件。
官方网站: http://logback.qos.ch/
slf4j
是java简单日志门面或各种各样日志框架的抽象(例如logging,logback、log4j),它允许终端用户在项目部署时插入真正所需要的日志框架
官方网站:https://www.slf4j.org/
pom依赖
< dependency>
< groupId> org. slf4j< / groupId>
< artifactId> slf4j- api< / artifactId>
< version> 1.7 .5 < / version>
< / dependency>
< dependency>
< groupId> ch. qos. logback< / groupId>
< artifactId> logback- core< / artifactId>
< version> 1.0 .11 < / version>
< / dependency>
< dependency>
< groupId> ch. qos. logback< / groupId>
< artifactId> logback- classic< / artifactId>
< version> 1.0 .11 < / version>
< / dependency>
示例
public class Slf4JTest {
private static final Logger logger = LoggerFactory . getLogger ( Slf4JTest . class ) ;
public static void main ( String [ ] args) {
logger. info ( "Hello,tese, this is a line of log message logged by Logback" ) ;
}
}
logback配置
<?xml version="1.0" encoding="UTF-8"?>
< configuration scan = " true" scanPeriod = " 30 seconds" >
< property name = " PATH" value = " logs" > </ property>
< property name = " PROJECT" value = " flinkStudy" > </ property>
< appender name = " CONSOLE" class = " ch.qos.logback.core.ConsoleAppender" >
< encoder charset = " UTF-8" >
< pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{100}) - %msg%n</ pattern>
</ encoder>
</ appender>
< appender name = " FILE1" class = " ch.qos.logback.core.rolling.RollingFileAppender" >
< rollingPolicy class = " ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
< fileNamePattern> ${PATH}/${PROJECT}-%d{yyyyMMdd}.log</ fileNamePattern>
< maxHistory> 30</ maxHistory>
</ rollingPolicy>
< encoder charset = " UTF-8" >
< pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{100} - %msg%n</ pattern>
</ encoder>
</ appender>
< appender name = " FILE2" class = " ch.qos.logback.core.rolling.RollingFileAppender" >
< rollingPolicy class = " ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" >
< fileNamePattern> ${PATH}/${PROJECT}-%d{yyyyMMdd}.%i.log</ fileNamePattern>
< maxHistory> 30</ maxHistory>
< maxFileSize> 100MB</ maxFileSize>
< totalSizeCap> 20GB</ totalSizeCap>
</ rollingPolicy>
< encoder charset = " UTF-8" >
< pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{100} - %msg%n</ pattern>
</ encoder>
</ appender>
< appender name = " FILE3" class = " ch.qos.logback.core.rolling.RollingFileAppender" >
< file> ${PATH}/${PROJECT}.log</ file>
< rollingPolicy class = " ch.qos.logback.core.rolling.FixedWindowRollingPolicy" >
< fileNamePattern> ${PATH}/${PROJECT}.%i.log.zip</ fileNamePattern>
< minIndex> 1</ minIndex>
< maxIndex> 10</ maxIndex>
</ rollingPolicy>
< triggeringPolicy class = " ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
< maxFileSize> 10MB</ maxFileSize>
</ triggeringPolicy>
< encoder charset = " UTF-8" >
< pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{100} - %msg%n</ pattern>
</ encoder>
</ appender>
< root level = " ERROR" >
< appender-ref ref = " CONSOLE" />
</ root>
< logger name = " com.leo" level = " INFO" >
< appender-ref ref = " FILE1" />
</ logger>
</ configuration>