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 java.io.File;
14  
15  import psiprobe.tools.logging.AbstractLogDestination;
16  
17  /**
18   * The Class Log4J2AppenderAccessor.
19   */
20  public class Log4J2AppenderAccessor extends AbstractLogDestination {
21  
22    /** The logger accessor. */
23    private Log4J2LoggerConfigAccessor loggerAccessor;
24  
25    /**
26     * Gets the logger accessor.
27     *
28     * @return the logger accessor
29     */
30    public Log4J2LoggerConfigAccessor getLoggerAccessor() {
31      return loggerAccessor;
32    }
33  
34    /**
35     * Sets the logger accessor.
36     *
37     * @param loggerAccessor the new logger accessor
38     */
39    public void setLoggerAccessor(Log4J2LoggerConfigAccessor loggerAccessor) {
40      this.loggerAccessor = loggerAccessor;
41    }
42  
43    @Override
44    public boolean isContext() {
45      return getLoggerAccessor().isContext();
46    }
47  
48    @Override
49    public boolean isRoot() {
50      return getLoggerAccessor().isRoot();
51    }
52  
53    @Override
54    public String getName() {
55      return getLoggerAccessor().getName();
56    }
57  
58    @Override
59    public String getLogType() {
60      return "log4j2";
61    }
62  
63    @Override
64    public String getIndex() {
65      return (String) invokeMethod(getTarget(), "getName", null, null);
66    }
67  
68    @Override
69    public String getConversionPattern() {
70      Object layout = invokeMethod(getTarget(), "getLayout", null, null);
71      if (layout != null && "org.apache.logging.log4j.core.layout.PatternLayout"
72          .equals(layout.getClass().getName())) {
73        return (String) invokeMethod(layout, "getConversionPattern", null, null);
74      }
75      return null;
76    }
77  
78    @Override
79    public File getFile() {
80      String fileName = (String) getProperty(getTarget(), "fileName", null);
81      if (fileName != null) {
82        return new File(fileName);
83      }
84      // Check for SMTPAppender information
85      File result = null;
86      if ("org.apache.logging.log4j.core.appender.SmtpAppender"
87          .equals(getTarget().getClass().getName())) {
88        Object smtpManager = getProperty(getTarget(), "manager", null, true);
89        Object factoryData = getProperty(smtpManager, "data", null, true);
90        Object cc = getProperty(factoryData, "cc", null, true);
91        Object bcc = getProperty(factoryData, "bcc", null, true);
92        Object from = getProperty(factoryData, "from", null, true);
93        Object subjectSerializer = getProperty(factoryData, "subject", null, true);
94        String subject = null;
95        if (subjectSerializer != null) {
96          Object[] subjectFormatters =
97              (Object[]) getProperty(subjectSerializer, "formatters", null, true);
98          if (subjectFormatters != null) {
99            Object subjectFormatterConverter =
100               getProperty(subjectFormatters[0], "converter", null, true);
101           if (subjectFormatterConverter != null) {
102             subject = (String) getProperty(subjectFormatterConverter, "literal", null, true);
103           }
104         }
105       }
106       result = new File("mailto:" + getProperty(factoryData, "to", "", true)
107           + (from != null ? "&from=" + from : "") + (cc != null ? "&cc=" + cc : "")
108           + (bcc != null ? "&bcc=" + bcc : "") + (subject != null ? "&subject=" + subject : ""));
109     } else {
110       result = getStdoutFile();
111     }
112     return result;
113   }
114 
115   @Override
116   public String getLevel() {
117     return getLoggerAccessor().getLevel();
118   }
119 
120   @Override
121   public String[] getValidLevels() {
122     return new String[] {"OFF", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE", "ALL"};
123   }
124 
125 }