代码改变世界

rocketmq 日志问题

2017-05-17 19:13  吴峰  阅读(20185)  评论(5编辑  收藏  举报

今天使用rocketmq,消费者每消费一条消息都会产生info日志

搜索无果,查看源代码

 1 public class ClientLogger {
 2     public static final String CLIENT_LOG_ROOT = "rocketmq.client.logRoot";
 3     public static final String CLIENT_LOG_MAXINDEX = "rocketmq.client.logFileMaxIndex";
 4     public static final String CLIENT_LOG_LEVEL = "rocketmq.client.logLevel";
 5     private static Logger log;
 6 
 7     static {
 8         log = createLogger(LoggerName.CLIENT_LOGGER_NAME);
 9     }
10 
11     private static Logger createLogger(final String loggerName) {
12         String logConfigFilePath =
13             System.getProperty("rocketmq.client.log.configFile",
14                 System.getenv("ROCKETMQ_CLIENT_LOG_CONFIGFILE"));
15         Boolean isloadconfig =
16             Boolean.parseBoolean(System.getProperty("rocketmq.client.log.loadconfig", "true"));
17 
18         final String log4JResourceFile =
19             System.getProperty("rocketmq.client.log4j.resource.fileName", "log4j_rocketmq_client.xml");
20 
21         final String logbackResourceFile =
22             System.getProperty("rocketmq.client.logback.resource.fileName", "logback_rocketmq_client.xml");
23 
24         String clientLogRoot = System.getProperty(CLIENT_LOG_ROOT, "${user.home}/logs/rocketmqlogs");
25         System.setProperty("client.logRoot", clientLogRoot);
26         String clientLogLevel = System.getProperty(CLIENT_LOG_LEVEL, "INFO");
27         System.setProperty("client.logLevel", clientLogLevel);
28         String clientLogMaxIndex = System.getProperty(CLIENT_LOG_MAXINDEX, "10");
29         System.setProperty("client.logFileMaxIndex", clientLogMaxIndex);
30 
31         if (isloadconfig) {
32             try {
33                 ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
34                 Class classType = iLoggerFactory.getClass();
35                 if (classType.getName().equals("org.slf4j.impl.Log4jLoggerFactory")) {
36                     Class<?> domconfigurator;
37                     Object domconfiguratorobj;
38                     domconfigurator = Class.forName("org.apache.log4j.xml.DOMConfigurator");
39                     domconfiguratorobj = domconfigurator.newInstance();
40                     if (null == logConfigFilePath) {
41                         Method configure = domconfiguratorobj.getClass().getMethod("configure", URL.class);
42                         URL url = ClientLogger.class.getClassLoader().getResource(log4JResourceFile);
43                         configure.invoke(domconfiguratorobj, url);
44                     } else {
45                         Method configure = domconfiguratorobj.getClass().getMethod("configure", String.class);
46                         configure.invoke(domconfiguratorobj, logConfigFilePath);
47                     }
48 
49                 } else if (classType.getName().equals("ch.qos.logback.classic.LoggerContext")) {
50                     Class<?> joranConfigurator;
51                     Class<?> context = Class.forName("ch.qos.logback.core.Context");
52                     Object joranConfiguratoroObj;
53                     joranConfigurator = Class.forName("ch.qos.logback.classic.joran.JoranConfigurator");
54                     joranConfiguratoroObj = joranConfigurator.newInstance();
55                     Method setContext = joranConfiguratoroObj.getClass().getMethod("setContext", context);
56                     setContext.invoke(joranConfiguratoroObj, iLoggerFactory);
57                     if (null == logConfigFilePath) {
58                         URL url = ClientLogger.class.getClassLoader().getResource(logbackResourceFile);
59                         Method doConfigure =
60                             joranConfiguratoroObj.getClass().getMethod("doConfigure", URL.class);
61                         doConfigure.invoke(joranConfiguratoroObj, url);
62                     } else {
63                         Method doConfigure =
64                             joranConfiguratoroObj.getClass().getMethod("doConfigure", String.class);
65                         doConfigure.invoke(joranConfiguratoroObj, logConfigFilePath);
66                     }
67 
68                 }
69             } catch (Exception e) {
70                 System.err.println(e);
71             }
72         }
73         return LoggerFactory.getLogger(LoggerName.CLIENT_LOGGER_NAME);
74     }
75 
76     public static Logger getLog() {
77         return log;
78     }
79 
80     public static void setLog(Logger log) {
81         ClientLogger.log = log;
82     }
83 }

所有日志名定义

public class LoggerName {
    public static final String FILTERSRV_LOGGER_NAME = "RocketmqFiltersrv";
    public static final String NAMESRV_LOGGER_NAME = "RocketmqNamesrv";
    public static final String BROKER_LOGGER_NAME = "RocketmqBroker";
    public static final String CLIENT_LOGGER_NAME = "RocketmqClient";
    public static final String TOOLS_LOGGER_NAME = "RocketmqTools";
    public static final String COMMON_LOGGER_NAME = "RocketmqCommon";
    public static final String STORE_LOGGER_NAME = "RocketmqStore";
    public static final String STORE_ERROR_LOGGER_NAME = "RocketmqStoreError";
    public static final String TRANSACTION_LOGGER_NAME = "RocketmqTransaction";
    public static final String REBALANCE_LOCK_LOGGER_NAME = "RocketmqRebalanceLock";
    public static final String ROCKETMQ_STATS_LOGGER_NAME = "RocketmqStats";
    public static final String COMMERCIAL_LOGGER_NAME = "RocketmqCommercial";
    public static final String FLOW_CONTROL_LOGGER_NAME = "RocketmqFlowControl";
    public static final String ROCKETMQ_AUTHORIZE_LOGGER_NAME = "RocketmqAuthorize";
    public static final String DUPLICATION_LOGGER_NAME = "RocketmqDuplication";
    public static final String PROTECTION_LOGGER_NAME = "RocketmqProtection";
    public static final String WATER_MARK_LOGGER_NAME = "RocketmqWaterMark";
}

 

配置

rocketmq.client.log.loadconfig

rocketmq.client.log.configFile

日志级别

<Logger name="RocketmqClient" level="WARN"/>

 

问题解决