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