本文主要是介绍logback小记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、需要的maven依赖:
<dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version><!--<scope>test</scope>--></dependency><!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.3</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、需要配置lockback(-test).xml 默认会查找使用这个xml文件的配置
<?xml version="1.0" encoding="UTF-8"?> <configuration scan = "true" scanPeriod="60"><!--只输出到控制台--><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><layout class="ch.qos.logback.classic.PatternLayout"><Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern></layout></appender><!--输出日志到文件--><appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"><!--日志的输出路径--><file>logs/MyLog.log</file><append>true</append><rollingPolicy class ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>logs/heuristic-%d{yyyy-MM-dd}.log</fileNamePattern><!--保留30天--><maxHistory>30</maxHistory></rollingPolicy><layout class="ch.qos.logback.classic.PatternLayout" ><pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern></layout></appender><!--指定哪些包使用日志功能--><logger name="logbackDemo" level="error"/><!-- <logger name="logbackDemo.LogbackDemo1" level="info"/>--><root level="debug"><appender-ref ref="STDOUT" /><appender-ref ref="FILE" /></root> </configuration>
3、测试日志:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** Description: 测试logback的日志demo*/
public class LogbackDemo {private static Logger logger = LoggerFactory.getLogger(LogbackDemo.class);public static void main(String[] args) {logger.info("info");logger.debug("debug");logger.error("error");}
}
上面的配置中只会打印error级别的日志信息。
好了以上就logback的简单配置和使用
这篇关于logback小记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!