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.logback13;
12  
13  import ch.qos.logback.core.OutputStreamAppender;
14  import ch.qos.logback.core.encoder.Encoder;
15  import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
16  
17  import java.io.File;
18  import java.nio.file.Path;
19  
20  import psiprobe.tools.logging.AbstractLogDestination;
21  
22  /**
23   * A wrapper for a Logback appender for a specific logger.
24   */
25  public class Logback13AppenderAccessor extends AbstractLogDestination {
26  
27    /** The logger accessor. */
28    private Logback13LoggerAccessor loggerAccessor;
29  
30    /**
31     * Gets the logger accessor.
32     *
33     * @return the logger accessor
34     */
35    public Logback13LoggerAccessor getLoggerAccessor() {
36      return loggerAccessor;
37    }
38  
39    /**
40     * Sets the logger accessor.
41     *
42     * @param loggerAccessor the new logger accessor
43     */
44    public void setLoggerAccessor(Logback13LoggerAccessor loggerAccessor) {
45      this.loggerAccessor = loggerAccessor;
46    }
47  
48    @Override
49    public boolean isContext() {
50      return getLoggerAccessor().isContext();
51    }
52  
53    @Override
54    public boolean isRoot() {
55      return getLoggerAccessor().isRoot();
56    }
57  
58    @Override
59    public String getName() {
60      return getLoggerAccessor().getName();
61    }
62  
63    /**
64     * Returns the log type, to distinguish logback appenders from other types like log4j appenders or
65     * jdk handlers.
66     *
67     * @return the log type
68     */
69    @Override
70    public String getLogType() {
71      return "logback13";
72    }
73  
74    /**
75     * Returns the name of this appender.
76     *
77     * @return the name of this appender.
78     */
79    @Override
80    public String getIndex() {
81      return (String) getProperty(getTarget(), "name", null);
82    }
83  
84    /**
85     * Returns the file that this appender writes to by accessing the {@code file} bean property of
86     * the appender.
87     *
88     * <p>
89     * If no such property exists, we assume the appender to write to stdout or stderr so the output
90     * will be contained in catalina.out.
91     *
92     * @return the file this appender writes to
93     */
94    @Override
95    public File getFile() {
96      String fileName = (String) getProperty(getTarget(), "file", null);
97      return fileName != null ? Path.of(fileName).toFile() : getStdoutFile();
98    }
99  
100   @Override
101   public String getEncoding() {
102     if (getTarget() instanceof OutputStreamAppender) {
103       OutputStreamAppender<?> appender = (OutputStreamAppender<?>) getTarget();
104       Encoder<?> encoder = appender.getEncoder();
105       if (encoder instanceof LayoutWrappingEncoder) {
106         LayoutWrappingEncoder<?> base = (LayoutWrappingEncoder<?>) encoder;
107         if (base.getCharset() != null) {
108           return base.getCharset().name();
109         }
110       }
111     }
112     return null;
113   }
114 
115   /**
116    * Gets the level of the associated logger.
117    *
118    * @return the logger's level
119    */
120   @Override
121   public String getLevel() {
122     return getLoggerAccessor().getLevel();
123   }
124 
125   /**
126    * Returns the valid log level names.
127    *
128    * <p>
129    * Note that Logback has no FATAL level.
130    *
131    * @return the valid log level names
132    */
133   @Override
134   public String[] getValidLevels() {
135     return new String[] {"OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE", "ALL"};
136   }
137 
138 }