##### Coverage Report - com.ctrip.apollo.biz.customize.LoggingCustomizer

Classes in this File | Line Coverage | Branch Coverage | Complexity
LoggingCustomizer |
16% | 6/36
12% | 1/8
| 5.0;5

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