| 1 | |
package com.ctrip.framework.apollo.common.customize; |
| 2 | |
|
| 3 | |
import com.google.common.base.Strings; |
| 4 | |
|
| 5 | |
import com.ctrip.framework.foundation.Foundation; |
| 6 | |
import com.dianping.cat.Cat; |
| 7 | |
|
| 8 | |
import org.slf4j.Logger; |
| 9 | |
import org.slf4j.LoggerFactory; |
| 10 | |
import org.springframework.beans.factory.InitializingBean; |
| 11 | |
import org.springframework.util.ClassUtils; |
| 12 | |
import org.springframework.util.ReflectionUtils; |
| 13 | |
|
| 14 | |
|
| 15 | |
import ch.qos.logback.classic.LoggerContext; |
| 16 | |
import ch.qos.logback.core.Appender; |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | 0 | public abstract class LoggingCustomizer implements InitializingBean { |
| 23 | 0 | private static final Logger logger = LoggerFactory.getLogger(LoggingCustomizer.class); |
| 24 | |
private static final String cLoggingAppenderClass = |
| 25 | |
"com.ctrip.framework.clogging.agent.appender.CLoggingAppender"; |
| 26 | 0 | private static boolean cLoggingAppenderPresent = |
| 27 | 0 | ClassUtils.isPresent(cLoggingAppenderClass, LoggingCustomizer.class.getClassLoader()); |
| 28 | |
|
| 29 | |
@Override |
| 30 | |
public void afterPropertiesSet() { |
| 31 | 0 | if (!cLoggingAppenderPresent) { |
| 32 | 0 | return; |
| 33 | |
} |
| 34 | |
|
| 35 | |
try { |
| 36 | 0 | tryConfigCLogging(); |
| 37 | 0 | } catch (Throwable ex) { |
| 38 | 0 | logger.error("Config CLogging failed", ex); |
| 39 | 0 | Cat.logError(ex); |
| 40 | 0 | } |
| 41 | |
|
| 42 | 0 | } |
| 43 | |
|
| 44 | |
private void tryConfigCLogging() throws Exception { |
| 45 | 0 | String appId = Foundation.app().getAppId(); |
| 46 | 0 | if (Strings.isNullOrEmpty(appId)) { |
| 47 | 0 | logger.warn("App id is null or empty!"); |
| 48 | 0 | return; |
| 49 | |
} |
| 50 | |
|
| 51 | |
|
| 52 | 0 | LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); |
| 53 | 0 | Class clazz = Class.forName(cLoggingAppenderClass); |
| 54 | 0 | Appender cLoggingAppender = (Appender) clazz.newInstance(); |
| 55 | |
|
| 56 | 0 | ReflectionUtils.findMethod(clazz, "setAppId", String.class).invoke(cLoggingAppender, appId); |
| 57 | 0 | ReflectionUtils.findMethod(clazz, "setServerIp", String.class) |
| 58 | 0 | .invoke(cLoggingAppender, cloggingUrl()); |
| 59 | 0 | ReflectionUtils.findMethod(clazz, "setServerPort", int.class) |
| 60 | 0 | .invoke(cLoggingAppender, Integer.parseInt(cloggingPort())); |
| 61 | |
|
| 62 | 0 | cLoggingAppender.setName("CentralLogging"); |
| 63 | 0 | cLoggingAppender.setContext(loggerContext); |
| 64 | 0 | cLoggingAppender.start(); |
| 65 | |
|
| 66 | 0 | ch.qos.logback.classic.Logger logger = |
| 67 | 0 | (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("root"); |
| 68 | 0 | logger.addAppender(cLoggingAppender); |
| 69 | |
|
| 70 | 0 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
protected abstract String cloggingUrl(); |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
protected abstract String cloggingPort(); |
| 83 | |
|
| 84 | |
|
| 85 | |
} |