slf4j,log4j和logback简介

1、这三个都是java项目的日志系统(推荐logback)

2、slf4j可以当作一个接口,slf4j-api,它有不同的实现(commons-logging.jar必须)

(1)slf4j-api.jar + slf4j-simple.jar

(2)slf4j-api.jar + slf4j-jdk.jar

(3)slf4j-api.jar + slf4j-log4j12.jar + log4j.jar

(4)用法

private static org.slf4j.Logger logger = LoggerFactory.getLogger(FirstFilter.class);

logger.info(“111”);

3、log4j使用要配置一个log4j.properties

 ### \u8BBE\u7F6E###
log4j.rootLogger = info,stdout,D,E


### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n


### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n


### \u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =D://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n


在web.xml中自己写一个servlet加载这个文件,

或者用spring框架加载<context:property-placeholder location="classpath:log4j.properties" />


用法:private static Logger logger = Logger.getLogger(FirstFilter.class);

4、logback需要4个jar:slf4j-api、logback-core、logback-classic、logback-access

并在src下面配置logback.xml

<?xml version="1.0" encoding="UTF-8"?>  
<configuration>  
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
   <encoder>  
     <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>  
   </encoder>  
</appender>
  
  <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">  
    <file>D:/logs/mylog.txt</file>  
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
      <!-- rollover daily -->  
      <fileNamePattern>D:/logs/mylog-%d{yyyy-MM-dd_HH-mm}.%i.log</fileNamePattern>  
      <maxHistory>5</maxHistory>   
      <timeBasedFileNamingAndTriggeringPolicy  
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">  
        <!-- or whenever the file size reaches 100MB -->  
        <maxFileSize>100MB</maxFileSize>  
      </timeBasedFileNamingAndTriggeringPolicy>  
    </rollingPolicy>  
    <encoder>  
      <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n</pattern>  
    </encoder>  
  </appender> 
    
  <root level="DEBUG"><appender-ref ref="STDOUT" /></root>  
  
</configuration> 

用法:private static org.slf4j.Logger logger = LoggerFactory.getLogger(FirstFilter.class);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值