SpringBoot集成RocketMQ,rocketmq_client.log日志文件配置
点击上方「蓝字」关注我们
SpringBoot项目集成rocketmq-client
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-clientartifactId>
<version>4.7.0version>
dependency>
项目启动时会在${user.home}/logs
目录下创建一个rocketmq_client.log
日志文件,文件全路径是:${user.home}/logs/rocketmqlogs/rocketmq_client.log
。如果一个系统部署多个微服务话,会导致所有的微服务的RocketMQ的日志都会打印到该文件里,不容易定位到底是哪个微服务的RocketMQ Client出现了错误。所以需要配置rocketmq_client.log日志目录到各自微服务的日志目录加以区分。
通过源码org.apache.rocketmq.client.log.ClientLogger
可以看到,在项目启动时会读取是否启动slf4j配置,读取的配置名称是:
rocketmq.client.logUseSlf4j
如果该配置为true的话,则会直接获取logger名称为RocketmqClientAppender名称为RocketmqClientAppender
的配置,以下是源码:
public class ClientLogger {
public static final String CLIENT_LOG_USESLF4J = "rocketmq.client.logUseSlf4j";
public static final String CLIENT_LOG_ROOT = "rocketmq.client.logRoot";
public static final String CLIENT_LOG_MAXINDEX = "rocketmq.client.logFileMaxIndex";
public static final String CLIENT_LOG_FILESIZE = "rocketmq.client.logFileMaxSize";
public static final String CLIENT_LOG_LEVEL = "rocketmq.client.logLevel";
public static final String CLIENT_LOG_ADDITIVE = "rocketmq.client.log.additive";
public static final String CLIENT_LOG_FILENAME = "rocketmq.client.logFileName";
public static final String CLIENT_LOG_ASYNC_QUEUESIZE = "rocketmq.client.logAsyncQueueSize";
public static final String ROCKETMQ_CLIENT_APPENDER_NAME = "RocketmqClientAppender";
private static final InternalLogger CLIENT_LOGGER;
private static final boolean CLIENT_USE_SLF4J;
//private static Appender rocketmqClientAppender = null;
static {
// 获取是否启动slf4j配置
CLIENT_USE_SLF4J = Boolean.parseBoolean(System.getProperty(CLIENT_LOG_USESLF4J, "false"));
if (!CLIENT_USE_SLF4J) {
InternalLoggerFactory.setCurrentLoggerType(InnerLoggerFactory.LOGGER_INNER);
CLIENT_LOGGER = createLogger(LoggerName.CLIENT_LOGGER_NAME);
createLogger(LoggerName.COMMON_LOGGER_NAME);
createLogger(RemotingHelper.ROCKETMQ_REMOTING);
} else {
// 获取logger
CLIENT_LOGGER = InternalLoggerFactory.getLogger(LoggerName.CLIENT_LOGGER_NAME);
}
}
// .........
}
通过以上源码分析可以看到如下一小段
System.getProperty(CLIENT_LOG_USESLF4J, "false")
所有在SpringBoot项目启动类中添加以下代码:
public class Application {
public static void main(String[] args) {
//添加配置
System.setProperty(ClientLogger.CLIENT_LOG_USESLF4J,"true");
SpringApplication.run(Application.class, args);
}
}
添加完以上代码后,然后在logback-spring.xml配置文件中配置日志的输出,具体配置如下:
<appender name="RocketmqClientAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/rocketmq_client.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/history/rocketmq_client.%d{yyyyMMdd}.%i.logfileNamePattern>
<maxFileSize>1024MmaxFileSize>
<maxHistory>30maxHistory>
rollingPolicy>
<encoder charset="UTF-8">
<pattern>%d{yy-MM-dd.HH:mm:ss.SSS} [%-16t] %-5p %-22c{0} %X{ServiceId} - %m%npattern>
encoder>
appender>
<logger name="RocketmqClient" additivity="false">
<level value="warn" />
<appender-ref ref="RocketmqClientAppender"/>
logger>
配置完毕,启动项目可以看到日志文件已经输出到指定的位置了。
备注:
System.setProperty(ClientLogger.CLIENT_LOG_USESLF4J,"true");
这段代码配置java的系统属性,也可以通过JAVA_OPTS方式设置。即在SpringBoot的启动脚本例如Shell脚本加上:
java -jar -Drocketmq.client.logUseSlf4j=true xxx-1.x.x.jar
扫码二维码
获取更多精彩
Java乐园
评论