一、简介
本文主要讲JAVA项目中引入Logback的方法。
二、解决
1、引入依赖。
<!--Begin LogBack Log--> <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</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.25</version> </dependency> <!--End LogBack Log-->
2、添加配置
在resources文件夹,或者类目录下中,新建logback.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <!-- For assistance related to logback-translator or configuration --> <!-- files in general, please contact the logback user mailing list --> <!-- at http://www.qos.ch/mailman/listinfo/logback-user --> <!-- --> <!-- For professional support please see --> <!-- http://www.qos.ch/shop/products/professionalSupport --> <!-- --> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- Daily rollover --> <fileNamePattern>log/generator.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- Keep 7 days' worth of history --> <maxHistory>7</maxHistory> </rollingPolicy> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Configure so that it outputs to both console and log file --> <root level="INFO"> <appender-ref ref="FILE" /> <appender-ref ref="STDOUT" /> </root> </configuration>
日志配置参考:https://www.cnblogs.com/songxingzhu/p/9644883.html
3、编写调用代码 :
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ApplicationMain { static Logger log = LoggerFactory.getLogger(ApplicationMain.class); public static void main(String[] args) { log.info("hello world."); } }