001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.status; 018 019import java.io.PrintStream; 020 021import org.apache.logging.log4j.Level; 022import org.apache.logging.log4j.util.PropertiesUtil; 023 024/** 025 * StatusListener that writes to the Console. 026 */ 027public class StatusConsoleListener implements StatusListener { 028 029 private static final String STATUS_LEVEL = "org.apache.logging.log4j.StatusLevel"; 030 031 private Level level = Level.FATAL; 032 033 private String[] filters = null; 034 035 private final PrintStream stream; 036 037 /** 038 * Creates the StatusConsoleListener using either the level configured by the 039 * "org.apache.logging.log4j.StatusLevel" system property if it is set or to a 040 * default value of FATAL. 041 */ 042 public StatusConsoleListener() { 043 final String str = PropertiesUtil.getProperties().getStringProperty(STATUS_LEVEL); 044 if (str != null) { 045 level = Level.toLevel(str, Level.FATAL); 046 } 047 stream = System.out; 048 } 049 050 /** 051 * Creates the StatusConsoleListener using the supplied Level. 052 * @param level The Level of status messages that should appear on the console. 053 */ 054 public StatusConsoleListener(final Level level) { 055 this.level = level; 056 stream = System.out; 057 } 058 059 /** 060 * Creates the StatusConsoleListener using the supplied Level. 061 * @param level The Level of status messages that should appear on the console. 062 * @param stream The PrintStream to write to. 063 */ 064 public StatusConsoleListener(final Level level, final PrintStream stream) { 065 this.level = level; 066 this.stream = stream; 067 } 068 069 /** 070 * Sets the level to a new value. 071 * @param level The new Level. 072 */ 073 public void setLevel(final Level level) { 074 this.level = level; 075 } 076 077 /** 078 * Return the Log Level for which the Listener should receive events. 079 * @return the Log Level. 080 */ 081 @Override 082 public Level getStatusLevel() { 083 return this.level; 084 } 085 086 /** 087 * Writes status messages to the console. 088 * @param data The StatusData. 089 */ 090 @Override 091 public void log(final StatusData data) { 092 if (!filtered(data)) { 093 stream.println(data.getFormattedStatus()); 094 } 095 } 096 097 /** 098 * Adds package name filters to exclude. 099 * @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}