Java/SpringBoot + Maven 使用Logback
1.添加依赖
如果是普通Java + Maven项目,需要手动添加如下依赖
<properties> <logback.version>1.2.3</logback.version> <slf4j.version>1.7.30</slf4j.version> </properties> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> </dependencies>
如果是Spring Boot + Maven 项目
可以省略这一步,因为Spring-boot-starter已经包含了这些依赖
2. 在 src/main/resources(source folder) 目录下 新建logback.xml 配置文件 如下:
配置方式1:所有级别的日志都在同一个文件中
可按需修改property的PROJECT_NAME,生成的log文件格式是 projectname.yyyy-MM-dd.i.log, 同一天内每10M一个log文件
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="10 seconds"> <property name="LOG_HOME" value="./logs" /> <property name="PROJECT_NAME" value="projectname" /> <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n" /> <!--输出到控制台 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>info</level> </filter> <encoder> <pattern>${LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!--输出到文件 --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}.%d{yyyy-MM-dd}.%i.log</FileNamePattern> <MaxHistory>30</MaxHistory> <MaxFileSize>10MB</MaxFileSize> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- 设置 log 级别 info --> <root level="info"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> </configuration>
配置方式2:不同级别的log分开存在不同的文件中
可按需修改property的PROJECT_NAME,生成的log文件格式是 projectname.loglever.yyyy-MM-dd.i.log, 同一天内每10M一个log文件
<!-- scan 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。 --> <!-- scanPeriod 设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。 --> <!-- debug 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 --> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <!-- 定义日志文件 输出位置 --> <property name="LOG_HOME" value="./logs" /> <property name="PROJECT_NAME" value="projectname" /> <!-- 日志最大的历史 30天 --> <property name="maxHistory" value="30" /> <!-- 日志单文件最大 10MB --> <property name="maxSize" value="10MB" /> <!-- 设置日志输出格式 --> <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n" /> <!-- ConsoleAppender 控制台输出日志 --> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- ERROR级别日志 --> <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 过滤器,只记录WARN级别的日志 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <!-- 滚动策略,它根据时间和日志文件大小来制定滚动策略. --> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}.error.%d{yyyy-MM-dd}.%i.log</FileNamePattern> <MaxHistory>${maxHistory}</MaxHistory> <MaxFileSize>${maxSize}</MaxFileSize> </rollingPolicy> <!-- 设置日志输出格式 --> <encoder> <pattern>${LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- WARN级别日志 appender --> <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}.warn.%d{yyyy-MM-dd}.%i.log</FileNamePattern> <MaxHistory>${maxHistory}</MaxHistory> <MaxFileSize>${maxSize}</MaxFileSize> </rollingPolicy> <encoder> <pattern>${LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- INFO级别日志 appender --> <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}.info.%d{yyyy-MM-dd}.%i.log</FileNamePattern> <MaxHistory>${maxHistory}</MaxHistory> <MaxFileSize>${maxSize}</MaxFileSize> </rollingPolicy> <encoder> <pattern>${LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- DEBUG级别日志 appender --> <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}.debug.%d{yyyy-MM-dd}.%i.log</FileNamePattern> <MaxHistory>${maxHistory}</MaxHistory> <MaxFileSize>${maxSize}</MaxFileSize> </rollingPolicy> <encoder> <pattern>${LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- TRACE级别日志 appender --> <appender name="TRACE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>TRACE</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}.trace.%d{yyyy-MM-dd}.%i.log</FileNamePattern> <MaxHistory>${maxHistory}</MaxHistory> <MaxFileSize>${maxSize}</MaxFileSize> </rollingPolicy> <encoder> <pattern>${LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- root级别 DEBUG --> <root> <!-- 打印debug级别日志及以上级别日志 --> <level value="debug" /> <!-- 控制台输出 --> <appender-ref ref="console" /> <!-- 文件输出 --> <appender-ref ref="ERROR" /> <appender-ref ref="INFO" /> <appender-ref ref="WARN" /> <appender-ref ref="DEBUG" /> <appender-ref ref="TRACE" /> </root> </configuration>
3.使用
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LogBack { private static Logger logger = LoggerFactory.getLogger(LogBack.class); public static void main(String[] args) { logger.error("============error====5555"); logger.warn("============warn====5555"); logger.info("==========info====5555"); logger.debug("==========debug====5555"); logger.trace("==========trace====5555"); } }
4. export jar 包时,也会把logback.xml打包
执行java -jar demolog.jar 后logs文件夹和jar包同级目录
参考
注意:
rollingPolicy =TimeBasedRollingPolicy 是基于时间的,不能和 triggeringPolicy =SizeBasedTriggeringPolicy 一起组合使用,会有冲突