SpringBoot以及logback.xml配置
虽说开箱即用,但是还是要配置下才能用的舒服。简单贴下基本配置以做记录。
References:
先上xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>xxx</groupId>
<artifactId>wechat</artifactId>
<version>0.1-SNAPSHOT</version>
<name>wechat</name>
<packaging>war</packaging>
<description>wechat</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<!-- 不依赖Redis的异步客户端lettuce -->
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- 使用Redis的客户端jedis -->
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency> -->
<!-- apache commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
</dependencies>
<build>
<!-- 打包文件名 -->
<finalName>weixinapp</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 允许运行项目 -->
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置中指定了打包方式为war,依赖有devtools(自动重启)、web、jpa、jdbc、aop、redis(不使用默认的客户端,换jedis)、logging、mariadb、apache commons以及一些构建设置。
application.properties:
#app config
spring.profiles.active=dev
#server config
server.port=8080
#db config
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/bilibili
spring.datasource.username=root
spring.datasource.password=123456
#db connection pool config
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=10
#redis config
spring.redis.database=0
spring.redis.host=192.168.0.199
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=1000
spring.redis.jedis.pool.min-idle=2
spring.redis.jedis.pool.max-active=5
spring.redis.jedis.pool.max-idle=1
spring.redis.jedis.pool.max-wait=3000ms
#JPA config
spring.data.jpa.repositories.enabled=true
spring.jpa.generate-ddl=false
spring.jpa.properties.hibernate.format_sql=true
#log config
logging.config=classpath:logback-spring.xml
配置都是常用的,第一行指定了当前为开发环境,生产环境可能是另一个单独的配置文件,其中指定此值为生产,并在启动项目时,添加配置文件读取路径(可以参考之前的文章设置)。然后是服务器端口,数据库使用了mariadb,连接池使用了默认的hikaricp,下面是redis的一些配置,然后是jpa的设置,关于sql显示的配置没有放在这里,单独写在logback配置文件中了,见下文。最后是设置日志为默认的logback,并且配置文件读取特定的文件。
logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="120 seconds" debug="false">
<contextName>wechat</contextName>
<!-- 开发环境 -->
<springProfile name="dev">
<logger name="com.xxx.wechat" level="debug" />
<property name="log.path" value="D:\\logs" />
</springProfile>
<!-- 生产环境 -->
<springProfile name="prod">
<logger name="com.xxx.wechat" level="info" />
<property name="log.path" value="/var/app/logs" />
</springProfile>
<!--输出到控制台 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!--输出到文件 -->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/panpaywechat.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/panpaywechat.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.hibernate.SQL" level="DEBUG" />
<logger name="org.hibernate.type" level="TRACE" />
<root level="info">
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
</configuration>
开始指定了两种环境下的日志路径,这样即使发布也不需要修改任何东西。下面是输出设置,日志文件是按照时间,每天都会生成一个新的日志文件,还可以设置大小等策略,请查阅官方文档设置。然后就是sql的日志,第一行表示记录sql语句,但是语句中占位符?的值是不记录的,这会给调试带来很大问题,所以第二行指定hibernate的type等级为trace,会记录所有参数内容,还有其他sql日志解决方案,比如google的日志,这里就不介绍了,自行查阅。最后指定生效的部分,这里是console和文件都生效。
感觉配置还是有点多,不过比起原生的spring好多了。