zoukankan      html  css  js  c++  java
  • 集成RocketMQ,项目启动后rocketmq_client.log日志文件配置

    项目添加RocketMQ以后启动项目时会在user.home下创建一个rocketmq_client.log日志文件,文件全路径是:${user.home}/logs/rocketmqlogs/rocketmq_client.log。由于我们的服务器数据盘不在home路径下,会导致系统盘空间占用不断增大,所以需要配置日志目录到其他路径。

    通过源码org.apache.rocketmq.client.log.ClientLogger可以看到,在项目启动时会读取是否启动slf4j配置,读取的配置名称是:rocketmq.client.logUseSlf4j,如果是则会直接获取logger名称为RocketmqClient,Appender名称为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);
            }
        }
    
        // .........
    }
    

    网上很多文章都说可以通过在配置文件中添加rocketmq.client.logUseSlf4j即可,但是我添加后通过debug上面的static块发现并没有读取到,所以只能在项目启动类中添加以下代码:

    public class Application {
        public static void main(String[] args) {
            System.setProperty(ClientLogger.CLIENT_LOG_USESLF4J,"true");
            SpringApplication.run(Application.class, args);
        }
    }
    

    添加完以上代码后,然后在配置日志的输出,我用的logback作为日志输出,具体配置如下:

    <!-- rocketmq日志 -->
        <appender name="RocketmqClientAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_DIR}/${APP_NAME}/rocketmq_client.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                <fileNamePattern>${LOG_DIR}/${APP_NAME}/history/rocketmq_client.%d{yyyyMMdd}.%i.log</fileNamePattern>
                <maxFileSize>${LOG_MAXFILESIZE}</maxFileSize>
                <!--保留时间,单位:天-->
                <maxHistory>${LOG_FILEMAXDAY}</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>
    

    上面的变量可以直接固定,我的项目中所有服务模块中使用了一个统一的日志文件,所以需要在日志配置的xml顶部配置属性,如果使用的是logback.xml则把下面的springProperty标签换成property标签,我使用的是logback-spring.xml,所以使用的springProperty标签:

    <configuration>
        <contextName>${APP_NAME}</contextName>
        <springProperty name="APP_NAME" scope="context" source="spring.application.name"/>
        <springProperty name="LOG_DIR" scope="context" source="platform.logging.dir" defaultValue="../logs/${APP_NAME}"/>
        <springProperty name="LOG_LEVEL" scope="context" source="platform.logging.level" defaultValue="INFO"/>
        <springProperty name="LOG_POINT_FILE" scope="context" source="platform.logging.dir" defaultValue="../logs/point"/>
        <springProperty name="LOG_MAXFILESIZE" scope="context" source="platform.logging.maxsize" defaultValue="200MB"/>
        <springProperty name="LOG_FILEMAXDAY" scope="context" source="platform.logging.maxday" defaultValue="60"/>
        <springProperty name="ServerIP" scope="context" source="spring.cloud.client.ip-address" defaultValue="0.0.0.0"/>
        <springProperty name="ServerPort" scope="context" source="server.port" defaultValue="0000"/>
    </configuration>
    

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

  • 相关阅读:
    duilib基本框架
    字典树(Trie Tree)
    如何用java有选择的输入多行文本
    java 接口默认修饰符
    3org.springframework.beans.factory.BeanDefinitionStoreException异常
    mybatis
    大O表示法
    sql in interview for a job
    static
    StringBuffer 和 StringBuilder
  • 原文地址:https://www.cnblogs.com/ucfjepl/p/13903658.html
Copyright © 2011-2022 走看看