SpringBoot项目集成rocketmq-client
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.7.0</version>
</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配置文件中配置日志的输出,具体配置如下:
<!-- rocketmq日志 -->
<appender name="RocketmqClientAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/rocketmq_client.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/history/rocketmq_client.%d{yyyyMMdd}.%i.log</fileNamePattern>
<maxFileSize>1024M</maxFileSize>
<!--保留时间,单位:天-->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>%d{yy-MM-dd.HH:mm:ss.SSS} [%-16t] %-5p %-22c{0} %X{ServiceId} - %m%n</pattern>
</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乐园
标签:SpringBoot,CLIENT,client,static,RocketMQ,public,rocketmq,LOG From: https://blog.51cto.com/u_13538361/6377695