zhouxinyu commented on a change in pull request #218: [ROCKETMQ-354] client log4j2 not logging URL: https://github.com/apache/rocketmq/pull/218#discussion_r161976737
########## File path: client/src/main/java/org/apache/rocketmq/client/log/Log4j2Helper.java ########## @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.client.log; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Layout; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.appender.AsyncAppender; +import org.apache.logging.log4j.core.appender.RollingFileAppender; +import org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy; +import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy; +import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy; +import org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy; +import org.apache.logging.log4j.core.config.AppenderRef; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.LoggerConfig; +import org.apache.logging.log4j.core.layout.PatternLayout; + +import java.lang.reflect.Method; + +public class Log4j2Helper { + + private static final int ASYNC_APPENDER_QUEUE_SIZE = 1024; + + private static final String APPENDER_FILE_SIZE = "100MB"; + + private static final int ASYNC_APPENDER_SHUTDOWN_TIMEOUT = 3; + + private static final String LOG_LAYOUT_PATTERN = "%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n"; + + private static Method getMethodWithOnlyName(Class clazz, String name) { + Method[] declaredMethods = clazz.getDeclaredMethods(); + for (Method declaredMethod : declaredMethods) { + if (declaredMethod.getName().equals(name)) { + return declaredMethod; + } + } + return null; + } + + private static DefaultRolloverStrategy createRolloverStrategy(String max, final Configuration config) { + Method createMethod = getMethodWithOnlyName(DefaultRolloverStrategy.class, "createStrategy"); + if (createMethod != null) { + createMethod.setAccessible(true); + Class<?>[] parameterTypes = createMethod.getParameterTypes(); + try { + if (parameterTypes.length == 5) { + return (DefaultRolloverStrategy) createMethod.invoke(null, max, "1", null, "1", config); + } else { + return (DefaultRolloverStrategy) createMethod.invoke(null, max, "1", null, "1", null, true, config); + } + } catch (Exception e) { + System.err.println(e); + } + } + return null; + } + + private static AbstractAppender createAsyncAppender(AbstractAppender appender, boolean blocking, int queueSize, Configuration config) { + String refAppenderName = appender.getName(); + AppenderRef ref = AppenderRef.createAppenderRef(refAppenderName, null, null); + AppenderRef[] refs1 = new AppenderRef[]{ref}; + String asyncAppenderName = "Async" + refAppenderName; + + Method createMethod = getMethodWithOnlyName(AsyncAppender.class, "createAppender"); + + if (createMethod == null) { + return appender; + } + AsyncAppender asyncAppender = null; + try { + if (createMethod.getParameterTypes().length == 9) { Review comment: Do we have another approach to distinguish these two methods, instead of using magic number 9? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
