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    
021    import java.io.PrintStream;
022    
023    /**
024     * StatusListener that writes to the Console.
025     */
026    public class StatusConsoleListener implements StatusListener {
027    
028        private static final String STATUS_LEVEL = "org.apache.logging.log4j.StatusLevel";
029    
030        private Level level = Level.FATAL;
031    
032        private String[] filters = null;
033    
034        private final PrintStream stream;
035    
036        /**
037         * Creates the StatusConsoleListener using either the level configured by the
038         * "org.apache.logging.log4j.StatusLevel" system property if it is set or to a
039         * default value of FATAL.
040         */
041        public StatusConsoleListener() {
042            String str = System.getProperty(STATUS_LEVEL);
043            if (str != null) {
044                level = Level.toLevel(str, Level.FATAL);
045            }
046            stream = System.out;
047        }
048    
049        /**
050         * Creates the StatusConsoleListener using the supplied Level.
051         * @param level The Level of status messages that should appear on the console.
052         */
053        public StatusConsoleListener(Level level) {
054            this.level = level;
055            stream = System.out;
056        }
057    
058        /**
059         * Creates the StatusConsoleListener using the supplied Level.
060         * @param level The Level of status messages that should appear on the console.
061         * @param stream The PrintStream to write to.
062         */
063        public StatusConsoleListener(Level level, PrintStream stream) {
064            this.level = level;
065            this.stream = stream;
066        }
067    
068        /**
069         * Sets the level to a new value.
070         * @param level The new Level.
071         */
072        public void setLevel(Level level) {
073            this.level = level;
074        }
075    
076        /**
077         * Writes status messages to the console.
078         * @param data The StatusData.
079         */
080        public void log(StatusData data) {
081            if (data.getLevel().isAtLeastAsSpecificAs(level) && !filtered(data)) {
082                stream.println(data.getFormattedStatus());
083            }
084        }
085    
086        /**
087         * Adds package name filters to exclude.
088         * @param filters An array of package names to exclude.
089         */
090        public void setFilters(String... filters) {
091            this.filters = filters;
092        }
093    
094        private boolean filtered(StatusData data) {
095            if (filters == null) {
096                return false;
097            }
098            String caller = data.getStackTraceElement().getClassName();
099            for (String filter : filters) {
100                if (caller.startsWith(filter)) {
101                    return true;
102                }
103            }
104            return false;
105        }
106    
107    }