日志几乎是一个项目的第二最重要的东西,而繁多的日志框架、各种依赖版本冲突 ,真的是头都疼。
一般项目满足以下条件就差不多了
- 基本日志输出, 能打成可执行jar包
- 日志的持久化, 入文件
SpringBoot+Mybatis+Druid+log4j 日志打包
- 把
SpringBoot
自带的日志功能关闭
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- 切换log4j2日志读取 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
- 添加
Mybatis
依赖
<!-- log-file config start -->
<!-- 配置 log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 识别 log4j2.yml文件 -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.8.10</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<!-- log-file config end -->
- 添加
log4j.xml
Appenders:
Console: # 输出到控制台
name: CONSOLE # Appender命名
target: SYSTEM_OUT
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) --- %m%n"
RollingFile: # 输出到文件,超过256MB归档
- name: ROLLING_FILE
ignoreExceptions: false
fileName: file.log
filePattern: "${date:yyyy-MM}-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) --- %m%n"
Policies:
SizeBasedTriggeringPolicy:
size: "256 MB"
DefaultRolloverStrategy:
max: 100
Loggers:
Root:
level: info
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE
Logger:
- name: com.example.mybatis.dao # 对dao层开启debug模式, 可以看到SQL
additivity: false
level: debug
AppenderRef:
- ref: CONSOLE
-
SpringBoot
方式打可执行jar
包
<build>
<finalName>video_black_incs</finalName>
<plugins>
<!-- 打 数据源+日志 的可执行依赖包 -->
<!-- 什么 shade、assembly 都是垃圾 -->
<!-- 楼上+1 -->
<!-- 楼上+1 -->
<!-- 楼上+1 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 工程主入口-->
<mainClass>com.asia.hy.increment.IncrementHandle</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 数据源监控
- 过滤器
import com.alibaba.druid.support.http.WebStatFilter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
@WebFilter(
urlPatterns = "/",
initParams = {
@WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*")
}
)
public class DruidStatFilter extends WebStatFilter {
}
- 监控器
import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
@WebServlet(
urlPatterns = {"/druid/*"},
initParams = {
@WebInitParam(name = "loginUsername", value = "admin"),
@WebInitParam(name = "loginPassword", value = "test"),
@WebInitParam(name = "resetEnable", value = "false")
}
)
public class DruidStatViewServlet extends StatViewServlet {
}
访问(账密上面配置)
网友评论