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