View Javadoc
1   package cn.home1.oss.lib.log4j2.jmx;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   
5   import org.apache.logging.log4j.Level;
6   import org.apache.logging.log4j.LogManager;
7   import org.apache.logging.log4j.core.Logger;
8   import org.apache.logging.log4j.core.LoggerContext;
9   import org.apache.logging.log4j.core.config.LoggerConfig;
10  import org.springframework.jmx.export.annotation.ManagedResource;
11  import org.springframework.stereotype.Component;
12  
13  import java.io.Serializable;
14  import java.util.ArrayList;
15  import java.util.Collection;
16  import java.util.Comparator;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.Set;
20  
21  @Component
22  @ManagedResource(//
23    objectName = "ch.qos.logback.classic:Name=default,Type=ch.qos.logback.classic.jmx.JMXConfigurator",
24    description = "oss log4j extend Bean")
25  public class Log4j2Manager implements Log4j2ManagerMBean {
26  
27    private static final String EMPTY = "";
28  
29    @Override
30    public void setLoggerLevel(final String loggerName, final String level) {
31      if (loggerName != null && level != null) {
32        final Level logLevel = Level.toLevel(level.trim(), null);
33        if (logLevel != null) {
34          this.setLoggerLevel(loggerName.trim(), logLevel);
35        }
36      }
37    }
38  
39    private void setLoggerLevel(final String loggerName, final Level level) {
40      checkNotNull(loggerName, "loggerName must not null");
41      checkNotNull(level, "level must not null");
42  
43      final LoggerContext lc = LoggerContext.getContext(false);
44      final Map<String, LoggerConfig> map = lc.getConfiguration().getLoggers();
45      final LoggerConfig loggerConfig = map.get(loggerName);
46      if (loggerConfig != null) {
47        loggerConfig.setLevel(level);
48        lc.updateLoggers();
49      } else {
50        final Logger logger = lc.getLogger(loggerName);
51        if (logger != null) {
52          logger.setLevel(level);
53        }
54      }
55    }
56  
57    @Override
58    public String getLoggerLevel(final String loggerName) {
59      String retValue = EMPTY;
60  
61      if (loggerName != null) {
62        final String loggerNameFin = loggerName.trim();
63        final LoggerContext lc = LoggerContext.getContext(false);
64        final Logger logger = lc.getLogger(loggerNameFin);
65        if (logger != null && logger.getLevel() != null) {
66          retValue = logger.getLevel().toString();
67        }
68      }
69  
70      return retValue;
71    }
72  
73    @Override
74    public List<String> getLoggerList() {
75      final LoggerContext lc = (LoggerContext) LogManager.getContext(false);
76      final List<String> strList = new ArrayList<>();
77  
78      final Collection<Logger> loggerCollection = lc.getLoggers();
79      final List<Logger> loggerList = new ArrayList<>(loggerCollection);
80  
81      // 添加配置中的logger
82      final Set<String> keySetConfig = lc.getConfiguration().getLoggers().keySet();
83      for (final String keyConfig : keySetConfig) {
84        if (!lc.hasLogger(keyConfig)) {
85          final Logger logger = lc.getLogger(keyConfig);
86          loggerList.add(logger);
87        }
88      }
89  
90      loggerList.sort(new LoggerComparator());
91  
92      for (final Logger logger : loggerList) {
93        strList.add(logger.getName());
94      }
95  
96      return strList;
97    }
98  
99    @Override
100   public String getLoggerEffectiveLevel(final String loggerName) {
101     String retValue = EMPTY;
102     if (loggerName != null) {
103       final String loggerNameFin = loggerName.trim();
104 
105       final LoggerContext lc = LoggerContext.getContext(false);
106       final Logger logger = lc.getLogger(loggerNameFin);
107       if (logger != null) {
108         retValue = logger.getLevel().toString();
109       }
110     }
111 
112     return retValue;
113   }
114 
115   @Override
116   public void setRootLoggerLevel(final String levelStr) {
117     final LoggerContext lc = LoggerContext.getContext(false);
118     final Logger logger = lc.getRootLogger();
119 
120     final Level logLevel = Level.toLevel(levelStr, null);
121     if (logLevel != null) {
122       logger.get().setLevel(logLevel);
123       lc.updateLoggers();
124     }
125   }
126 
127   @Override
128   public String getRootLoggerLevel() {
129     final LoggerContext lc = LoggerContext.getContext(false);
130     final Logger logger = lc.getRootLogger();
131 
132     String retValue = EMPTY;
133     if (logger != null && logger.getLevel() != null) {
134       retValue = logger.getLevel().toString();
135     }
136 
137     return retValue;
138   }
139 
140   public static final class LoggerComparator implements Comparator<Logger>, Serializable {
141 
142     private static final long serialVersionUID = 1074614190743916148L;
143 
144     @Override
145     public int compare(final Logger l1, final Logger l2) {
146       int retValue;
147       if (l1.getName().equals(l2.getName())) {
148         retValue = 0;
149       } else {
150         if (l1.getName().equals(LogManager.ROOT_LOGGER_NAME)) {
151           retValue = -1;
152         } else if (l2.getName().equals(LogManager.ROOT_LOGGER_NAME)) {
153           retValue = 1;
154         } else {
155           retValue = l1.getName().compareTo(l2.getName());
156         }
157       }
158 
159       return retValue;
160     }
161   }
162 }