一、Springboot整合log4j2日志问题
当根据网上教程添加依赖,编写配置文件后测试日志却怎么也没有日志目录生成,网上教程如下:
1. Maven 依赖 pom.xml配置
<!-- 包含 mvc,aop 等jar资源 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 切换log4j2日志读取 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
2. 然后添加如下两个依赖
<!-- 配置 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>
</dependency>
3. 配置文件添加log4j2.yml,文件存放resource目录下
# 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。
Configuration:
status: warn
monitorInterval: 30
Properties: # 定义全局变量
Property: # 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下:
#测试:-Dlog.level.console=warn -Dlog.level.xjj=trace
#生产:-Dlog.level.console=warn -Dlog.level.xjj=info
- name: log.level.console
value: info
- name: log.path
value: log
- name: project.name
value: opendoc
- name: log.pattern
value: "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-30.30C{1.} : %m%n"
Appenders:
Console: #输出到控制台
name: CONSOLE
target: SYSTEM_OUT
PatternLayout:
pattern: ${log.pattern}
# 启动日志
RollingFile:
- name: ROLLING_FILE
fileName: ${log.path}/${project.name}.log
filePattern: "${log.path}/historyRunLog/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: ${log.pattern}
Filters:
# 一定要先去除不接受的日志级别,然后获取需要接受的日志级别
ThresholdFilter:
- level: error
onMatch: DENY
onMismatch: NEUTRAL
- level: info
onMatch: ACCEPT
onMismatch: DENY
Policies:
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100
# 平台日志
- name: PLATFORM_ROLLING_FILE
ignoreExceptions: false
fileName: ${log.path}/platform/${project.name}_platform.log
filePattern: "${log.path}/platform/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: ${log.pattern}
Policies:
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100
# 业务日志
- name: BUSSINESS_ROLLING_FILE
ignoreExceptions: false
fileName: ${log.path}/bussiness/${project.name}_bussiness.log
filePattern: "${log.path}/bussiness/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: ${log.pattern}
Policies:
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100
# 错误日志
- name: EXCEPTION_ROLLING_FILE
ignoreExceptions: false
fileName: ${log.path}/exception/${project.name}_exception.log
filePattern: "${log.path}/exception/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
ThresholdFilter:
level: error
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: ${log.pattern}
Policies:
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100
# DB 日志
- name: DB_ROLLING_FILE
ignoreExceptions: false
fileName: ${log.path}/db/${project.name}_db.log
filePattern: "${log.path}/db/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: ${log.pattern}
Policies:
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100
Loggers:
Root:
level: info
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE
- ref: EXCEPTION_ROLLING_FILE
Logger:
- name: platform
level: info
additivity: false
AppenderRef:
- ref: CONSOLE
- ref: PLATFORM_ROLLING_FILE
- name: bussiness
level: info
additivity: false
AppenderRef:
- ref: BUSSINESS_ROLLING_FILE
- name: exception
level: debug
additivity: true
AppenderRef:
- ref: EXCEPTION_ROLLING_FILE
- name: db
level: info
additivity: false
AppenderRef:
- ref: DB_ROLLING_FILE
# 监听具体包下面的日志
# Logger: # 为com.xjj包配置特殊的Log级别,方便调试
# - name: com.xjj
# additivity: false
# level: ${sys:log.level.xjj}
# AppenderRef:
# - ref: CONSOLE
# - ref: ROLLING_FILE
4. 不同日志工具类util编辑
由于配置了4个文件存放不同日志,分别为平台日志(${project.name}_platform.log)、 业务日志(${project.name}_bussiness.log)、错误日志(${project.name}_exception.log)、DB 日志(${project.name}_db.log),文件夹外面为运行日志,不同文件日志级别不一样,因此程序员在开发时候需要注意引入不同日志,也就是说开发出现的日志,程序员可以进行分类,分别调用公共方法即可。公共类编辑如下:
package com.open.util;
/**
* 本地日志枚举
* @author Administrator
*
*/
public enum LogEnum {
BUSSINESS("bussiness"),
PLATFORM("platform"),
DB("db"),
EXCEPTION("exception"),
;
private String category;
LogEnum(String category) {
this.category = category;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
package com.open.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 本地日志参考类
* @author Administrator
*
*/
public class LogUtils {
/**
* 获取业务日志logger
*
* @return
*/
public static Logger getBussinessLogger() {
return LoggerFactory.getLogger(LogEnum.BUSSINESS.getCategory());
}
/**
* 获取平台日志logger
*
* @return
*/
public static Logger getPlatformLogger() {
return LoggerFactory.getLogger(LogEnum.PLATFORM.getCategory());
}
/**
* 获取数据库日志logger
*
* @return
*/
public static Logger getDBLogger() {
return LoggerFactory.getLogger(LogEnum.DB.getCategory());
}
/**
* 获取异常日志logger
*
* @return
*/
public static Logger getExceptionLogger() {
return LoggerFactory.getLogger(LogEnum.EXCEPTION.getCategory());
}
}
具体调用如下:
@GetMapping("/helloworld")
public String helloworld() throws Exception{
Logger log = LogUtils.getExceptionLogger();
Logger log1 = LogUtils.getBussinessLogger();
Logger log2 = LogUtils.getDBLogger();
userService.queryUser();
log.error("getExceptionLogger===日志测试");
log1.info("getBussinessLogger===日志测试");
log2.debug("getDBLogger===日志测试");
return "helloworld";
}
问题:经过上述配置后发现并没有生成对应的日志目录,开始以为是配置文件哪里写错了,仔细检查核对后还是失败。
问题解决:通过百度、谷歌查看相关的问题,但是基本上都是搜到的上述类似做法,大概的方向就是要排除掉springboot默认的日志,然后再测试的时候突然发现控制台打印出的日志提示绑定了logback,然后就确定是没有排除掉默认的日志,然后以为是jar包冲突了就把logback的jar包删了然后测试,又提示栈溢出,百度得知此时是logslf4j实例化日志对象死循环了(具体原因不详),发现不少这个问题又重新导入logback的jar包,突然在一篇博客上看到排除默认日志的位置放得不一样,最后果然通过这个解决了该问题。
如上我们看到排除默认日志的位置是放在spring-boot-starter-web中的,后面博客发现的位置是放在spring-boot-starter中的,如下图所示:
<dependency> <!-- exclude掉spring-boot的默认log配置 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
更改之后问题终于成功解决,生成了对应的日志目录和日志,这个问题可能是因为作用范围的原因,还需详究。
参考网址:
1. https://blog.csdn.net/u010598111/article/details/80556437
2. https://www.cnblogs.com/ly-radiata/articles/6026534.html
二、服务器部署项目Redis连接失败问题
问题原因:在服务器把Redis配置文件redis.conf修改后把daemonize设置为了no,这样会导致redis服务启动后窗口关闭服务也会结束掉,redis服务都关了当然连接失败了。开始以为是端口的原因换了几次端口都没用,对计算机端口的理解还不是很清楚,需要详究。
问题解决:
将redis.conf中的daemonize设置为yes(守护进程),这样就可以默认启动就后台运行,不会被关闭了。最后测试连接成功。
参考网址:https://blog.csdn.net/qq_42805749/article/details/89372571?tdsourcetag=s_pcqq_aiomsg