View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools.logging.log4j2;
12  
13  import com.google.common.base.Strings;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  import java.util.Map;
18  
19  import org.apache.commons.lang3.reflect.MethodUtils;
20  
21  import psiprobe.tools.logging.DefaultAccessor;
22  
23  /**
24   * The Class Log4JLoggerAccessor.
25   */
26  public class Log4J2LoggerConfigAccessor extends DefaultAccessor {
27  
28    /** The context. */
29    private boolean context;
30  
31    /** The LoggerContext. */
32    private Log4J2LoggerContextAccessor loggerContext;
33  
34    /** The loggers Map of appenders. **/
35    private Map<String, Object> appenderMap;
36  
37    /**
38     * Sets the target.
39     *
40     * @param target the new target
41     */
42    @Override
43    @SuppressWarnings("unchecked")
44    public void setTarget(Object target) {
45      super.setTarget(target);
46  
47      try {
48        this.appenderMap = (Map<String, Object>) invokeMethod(target, "getAppenders", null, null);
49      } catch (Exception e) {
50        logger.error("{}#getAppenders() failed", target.getClass().getName(), e);
51      }
52    }
53  
54    /**
55     * Gets the appenders.
56     *
57     * @return the appenders
58     */
59    public List<Log4J2AppenderAccessor> getAppenders() {
60      List<Log4J2AppenderAccessor> appenders = new ArrayList<>();
61      if (appenderMap != null) {
62        for (Object unwrappedAppender : appenderMap.values()) {
63          Log4J2AppenderAccessor appender = wrapAppender(unwrappedAppender);
64          if (appender != null) {
65            appenders.add(appender);
66          }
67        }
68      }
69      return appenders;
70    }
71  
72    /**
73     * Gets the appender.
74     *
75     * @param name the name
76     *
77     * @return the appender
78     */
79    public Log4J2AppenderAccessor getAppender(String name) {
80      if (this.appenderMap != null) {
81        Object appender = appenderMap.get(name);
82        return wrapAppender(appender);
83      }
84      return null;
85    }
86  
87    /**
88     * Checks if is context.
89     *
90     * @return true, if is context
91     */
92    public boolean isContext() {
93      return context;
94    }
95  
96    /**
97     * Sets the context.
98     *
99     * @param context the new context
100    */
101   public void setContext(boolean context) {
102     this.context = context;
103   }
104 
105   /**
106    * Sets the logger context.
107    *
108    * @param loggerContext the new logger context
109    */
110   public void setLoggerContext(Log4J2LoggerContextAccessor loggerContext) {
111     this.loggerContext = loggerContext;
112   }
113 
114   /**
115    * Checks if is root.
116    *
117    * @return true, if is root
118    */
119   public boolean isRoot() {
120     return Strings.isNullOrEmpty(getName());
121   }
122 
123   /**
124    * Gets the name.
125    *
126    * @return the name
127    */
128   public String getName() {
129     return (String) getProperty(getTarget(), "name", null);
130   }
131 
132   /**
133    * Gets the level.
134    *
135    * @return the level
136    */
137   public String getLevel() {
138     try {
139       Object level = MethodUtils.invokeMethod(getTarget(), "getLevel");
140       return (String) MethodUtils.invokeMethod(level, "toString");
141     } catch (Exception e) {
142       logger.error("{}#getLevel() failed", getTarget().getClass().getName(), e);
143     }
144     return null;
145   }
146 
147   /**
148    * Sets the level.
149    *
150    * @param newLevelStr the new level
151    */
152   public void setLevel(String newLevelStr) {
153     try {
154       Object level = MethodUtils.invokeMethod(getTarget(), "getLevel");
155       Object newLevel = MethodUtils.invokeMethod(level, "toLevel", newLevelStr);
156       MethodUtils.invokeMethod(getTarget(), "setLevel", newLevel);
157       loggerContext.updateLoggers();
158     } catch (Exception e) {
159       logger.error("{}#setLevel('{}') failed", getTarget().getClass().getName(), newLevelStr, e);
160     }
161   }
162 
163   /**
164    * Wrap appender.
165    *
166    * @param appender the appender
167    *
168    * @return the log4 j appender accessor
169    */
170   private Log4J2AppenderAccessor wrapAppender(Object appender) {
171     try {
172       if (appender == null) {
173         throw new IllegalArgumentException("appender is null");
174       }
175       Log4J2AppenderAccessor appenderAccessor = new Log4J2AppenderAccessor();
176       appenderAccessor.setTarget(appender);
177       appenderAccessor.setLoggerAccessor(this);
178       appenderAccessor.setApplication(getApplication());
179       return appenderAccessor;
180     } catch (Exception e) {
181       logger.error("Could not wrap appender: {}", appender, e);
182     }
183     return null;
184   }
185 
186 }