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 java.io.PrintStream;
20
21 import org.apache.logging.log4j.Level;
22 import org.apache.logging.log4j.util.PropertiesUtil;
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 * Return the Log Level for which the Listener should receive events.
79 * @return the Log Level.
80 */
81 @Override
82 public Level getStatusLevel() {
83 return this.level;
84 }
85
86 /**
87 * Writes status messages to the console.
88 * @param data The StatusData.
89 */
90 @Override
91 public void log(final StatusData data) {
92 if (!filtered(data)) {
93 stream.println(data.getFormattedStatus());
94 }
95 }
96
97 /**
98 * Adds package name filters to exclude.
99 * @param filters An array of package names to exclude.
100 */
101 public void setFilters(final String... filters) {
102 this.filters = filters;
103 }
104
105 private boolean filtered(final StatusData data) {
106 if (filters == null) {
107 return false;
108 }
109 final String caller = data.getStackTraceElement().getClassName();
110 for (final String filter : filters) {
111 if (caller.startsWith(filter)) {
112 return true;
113 }
114 }
115 return false;
116 }
117
118 }