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 */ 017 package org.apache.logging.log4j.status; 018 019 import org.apache.logging.log4j.Level; 020 import org.apache.logging.log4j.util.PropertiesUtil; 021 022 import java.io.PrintStream; 023 024 /** 025 * StatusListener that writes to the Console. 026 */ 027 public 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 * Writes status messages to the console. 079 * @param data The StatusData. 080 */ 081 public void log(final StatusData data) { 082 if (data.getLevel().isAtLeastAsSpecificAs(level) && !filtered(data)) { 083 stream.println(data.getFormattedStatus()); 084 } 085 } 086 087 /** 088 * Adds package name filters to exclude. 089 * @param filters An array of package names to exclude. 090 */ 091 public void setFilters(final String... filters) { 092 this.filters = filters; 093 } 094 095 private boolean filtered(final StatusData data) { 096 if (filters == null) { 097 return false; 098 } 099 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 }