SpringBoot集成RocketMQ日志文件配置

SpringBoot项目集成rocketmq-client


    org.apache.rocketmq
    rocketmq-client
     4.7.0

项目启动时会在 u s e r . h o m e / l o g s 目 录 下 创 建 一 个 r o c k e t m q c l i e n t . l o g 日 志 文 件 , 文 件 全 路 径 是 : {user.home}/logs目录下创建一个rocketmq_client.log日志文件,文件全路径是: user.home/logsrocketmqclient.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配置文件中配置日志的输出,具体配置如下:


    
        ${LOG_DIR}/rocketmq_client.log
        
            ${LOG_DIR}/history/rocketmq_client.%d{yyyyMMdd}.%i.log
            1024M
            
            30
        
        
            %d{yy-MM-dd.HH:mm:ss.SSS} [%-16t] %-5p %-22c{0} %X{ServiceId} - %m%n
        
    
    
        
        
    

配置完毕,启动项目可以看到日志文件已经输出到指定的位置了。

备注:

 System.setProperty(ClientLogger.CLIENT_LOG_USESLF4J,"true");

这段代码配置java的系统属性,也可以通过JAVA_OPTS方式设置。即在SpringBoot的启动脚本例如Shell脚本加上:

java -jar -Drocketmq.client.logUseSlf4j=true  xxx-1.x.x.jar

你可能感兴趣的:(java乐园)