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 import org.apache.logging.log4j.util.PropertiesUtil;
21
22 import java.io.PrintStream;
23
24 /**
25 * StatusListener that writes to the Console.
26 */
27 public class StatusConsoleListener implements StatusListener {
28
29 private static final String STATUS_LEVEL = "org.apache.logging.log4j.StatusLevel";
30
31 private Level level = Level.FATAL;
32
33 private String[] filters = null;
34
35 private final PrintStream stream;
36
37 /**
38 * Creates the StatusConsoleListener using either the level configured by the
39 * "org.apache.logging.log4j.StatusLevel" system property if it is set or to a
40 * default value of FATAL.
41 */
42 public StatusConsoleListener() {
43 final String str = PropertiesUtil.getProperties().getStringProperty(STATUS_LEVEL);
44 if (str != null) {
45 level = Level.toLevel(str, Level.FATAL);
46 }
47 stream = System.out;
48 }
49
50 /**
51 * Creates the StatusConsoleListener using the supplied Level.
52 * @param level The Level of status messages that should appear on the console.
53 */
54 public StatusConsoleListener(final Level level) {
55 this.level = level;
56 stream = System.out;
57 }
58
59 /**
60 * Creates the StatusConsoleListener using the supplied Level.
61 * @param level The Level of status messages that should appear on the console.
62 * @param stream The PrintStream to write to.
63 */
64 public StatusConsoleListener(final Level level, final PrintStream stream) {
65 this.level = level;
66 this.stream = stream;
67 }
68
69 /**
70 * Sets the level to a new value.
71 * @param level The new Level.
72 */
73 public void setLevel(final Level level) {
74 this.level = level;
75 }
76
77 /**
78 * Writes status messages to the console.
79 * @param data The StatusData.
80 */
81 public void log(final StatusData data) {
82 if (data.getLevel().isAtLeastAsSpecificAs(level) && !filtered(data)) {
83 stream.println(data.getFormattedStatus());
84 }
85 }
86
87 /**
88 * Adds package name filters to exclude.
89 * @param filters An array of package names to exclude.
90 */
91 public void setFilters(final String... filters) {
92 this.filters = filters;
93 }
94
95 private boolean filtered(final StatusData data) {
96 if (filters == null) {
97 return false;
98 }
99 final String caller = data.getStackTraceElement().getClassName();
100 for (final String filter : filters) {
101 if (caller.startsWith(filter)) {
102 return true;
103 }
104 }
105 return false;
106 }
107
108 }