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.IOException; 20 import java.io.PrintStream; 21 22 import org.apache.logging.log4j.Level; 23 24 /** 25 * StatusListener that writes to the Console. 26 */ 27 @SuppressWarnings("UseOfSystemOutOrSystemErr") 28 public class StatusConsoleListener implements StatusListener { 29 30 private Level level = Level.FATAL; 31 private String[] filters; 32 private final PrintStream stream; 33 34 /** 35 * Creates the StatusConsoleListener using the supplied Level. 36 * @param level The Level of status messages that should appear on the console. 37 */ 38 public StatusConsoleListener(final Level level) { 39 this(level, System.out); 40 } 41 42 /** 43 * Creates the StatusConsoleListener using the supplied Level. Make sure not to use a logger stream of some sort 44 * to avoid creating an infinite loop of indirection! 45 * @param level The Level of status messages that should appear on the console. 46 * @param stream The PrintStream to write to. 47 * @throws IllegalArgumentException if the PrintStream argument is {@code null}. 48 */ 49 public StatusConsoleListener(final Level level, final PrintStream stream) { 50 if (stream == null) { 51 throw new IllegalArgumentException("You must provide a stream to use for this listener."); 52 } 53 this.level = level; 54 this.stream = stream; 55 } 56 57 /** 58 * Sets the level to a new value. 59 * @param level The new Level. 60 */ 61 public void setLevel(final Level level) { 62 this.level = level; 63 } 64 65 /** 66 * Return the Log Level for which the Listener should receive events. 67 * @return the Log Level. 68 */ 69 @Override 70 public Level getStatusLevel() { 71 return this.level; 72 } 73 74 /** 75 * Writes status messages to the console. 76 * @param data The StatusData. 77 */ 78 @Override 79 public void log(final StatusData data) { 80 if (!filtered(data)) { 81 stream.println(data.getFormattedStatus()); 82 } 83 } 84 85 /** 86 * Adds package name filters to exclude. 87 * @param filters An array of package names to exclude. 88 */ 89 public void setFilters(final String... filters) { 90 this.filters = filters; 91 } 92 93 private boolean filtered(final StatusData data) { 94 if (filters == null) { 95 return false; 96 } 97 final String caller = data.getStackTraceElement().getClassName(); 98 for (final String filter : filters) { 99 if (caller.startsWith(filter)) { 100 return true; 101 } 102 } 103 return false; 104 } 105 106 @Override 107 public void close() throws IOException { 108 // only want to close non-system streams 109 if (this.stream != System.out && this.stream != System.err) { 110 this.stream.close(); 111 } 112 } 113 }