View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.status;
18  
19  import org.apache.logging.log4j.Level;
20  
21  import java.io.PrintStream;
22  
23  /**
24   * StatusListener that writes to the Console.
25   */
26  public class StatusConsoleListener implements StatusListener {
27  
28      private static final String STATUS_LEVEL = "org.apache.logging.log4j.StatusLevel";
29  
30      private Level level = Level.FATAL;
31  
32      private String[] filters = null;
33  
34      private final PrintStream stream;
35  
36      /**
37       * Creates the StatusConsoleListener using either the level configured by the
38       * "org.apache.logging.log4j.StatusLevel" system property if it is set or to a
39       * default value of FATAL.
40       */
41      public StatusConsoleListener() {
42          String str = System.getProperty(STATUS_LEVEL);
43          if (str != null) {
44              level = Level.toLevel(str, Level.FATAL);
45          }
46          stream = System.out;
47      }
48  
49      /**
50       * Creates the StatusConsoleListener using the supplied Level.
51       * @param level The Level of status messages that should appear on the console.
52       */
53      public StatusConsoleListener(Level level) {
54          this.level = level;
55          stream = System.out;
56      }
57  
58      /**
59       * Creates the StatusConsoleListener using the supplied Level.
60       * @param level The Level of status messages that should appear on the console.
61       * @param stream The PrintStream to write to.
62       */
63      public StatusConsoleListener(Level level, PrintStream stream) {
64          this.level = level;
65          this.stream = stream;
66      }
67  
68      /**
69       * Sets the level to a new value.
70       * @param level The new Level.
71       */
72      public void setLevel(Level level) {
73          this.level = level;
74      }
75  
76      /**
77       * Writes status messages to the console.
78       * @param data The StatusData.
79       */
80      public void log(StatusData data) {
81          if (data.getLevel().isAtLeastAsSpecificAs(level) && !filtered(data)) {
82              stream.println(data.getFormattedStatus());
83          }
84      }
85  
86      /**
87       * Adds package name filters to exclude.
88       * @param filters An array of package names to exclude.
89       */
90      public void setFilters(String... filters) {
91          this.filters = filters;
92      }
93  
94      private boolean filtered(StatusData data) {
95          if (filters == null) {
96              return false;
97          }
98          String caller = data.getStackTraceElement().getClassName();
99          for (String filter : filters) {
100             if (caller.startsWith(filter)) {
101                 return true;
102             }
103         }
104         return false;
105     }
106 
107 }