1. logback介绍
Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块:logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日志的功能。
2. maven依赖
如果你没有使用maven,那么你自己去下载jar包吧...
3. 配置和使用
在spring-boot中可以放在application.yml或logback-spring.xml中
application.yml简单配置使用:
logging: pattern: console: "%d - %msg%n" file: D:/springboot/sell.log level: debuglogback-spring.xml可以配置复杂的功能:
//需求:区分info和error日志 // 每天产生一个日志文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> %msg%n </pattern> </layout> </appender> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路径--> <fileNamePattern>D:/springboot/sell-info.%d.log</fileNamePattern> </rollingPolicy> </appender> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路径--> <fileNamePattern>D:/springboot/sell-error.%d.log</fileNamePattern> </rollingPolicy> </appender> <root level="info"> <appender-ref ref="consoleLog"/> <appender-ref ref="fileInfoLog"/> <appender-ref ref="fileErrorLog"/> </root> </configuration>
@Slf4j public class Loggertest { //loggerback //org.slf4j.event可以看到级别, //private final Logger logger=LoggerFactory.getLogger(Loggertest.class); //需求:区分info和error日志 // 每天产生一个日志文件 @Test public void test(){ /* logger.debug("debug.........................."); logger.info("info------------------------"); logger.error("error========================="); */ log.debug("debug.........................."); log.info("info------------------------"); log.error("error========================="); String name="imooc"; String password="123456"; //log.info("name: " +name+" ,password: "+password); log.info("name: {}, password: {}",name,password); } }