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.core.pattern;
018    
019    import org.apache.logging.log4j.Logger;
020    import org.apache.logging.log4j.core.LogEvent;
021    import org.apache.logging.log4j.status.StatusLogger;
022    
023    /**
024     * LoggingEventPatternConverter is a base class for pattern converters
025     * that can format information from instances of LoggingEvent.
026     */
027    public abstract class LogEventPatternConverter extends AbstractPatternConverter {
028    
029        /**
030         * Allow subclasses access to the status logger without creating another instance.
031         */
032        protected static final Logger LOGGER = StatusLogger.getLogger();
033    
034        /**
035         * Constructs an instance of LoggingEventPatternConverter.
036         *
037         * @param name  name of converter.
038         * @param style CSS style for output.
039         */
040        protected LogEventPatternConverter(final String name, final String style) {
041            super(name, style);
042        }
043    
044        /**
045         * Formats an event into a string buffer.
046         *
047         * @param event      event to format, may not be null.
048         * @param toAppendTo string buffer to which the formatted event will be appended.  May not be null.
049         */
050        public abstract void format(final LogEvent event, final StringBuilder toAppendTo);
051    
052        /**
053         * {@inheritDoc}
054         */
055        @Override
056        public void format(final Object obj, final StringBuilder output) {
057            if (obj instanceof LogEvent) {
058                format((LogEvent) obj, output);
059            }
060        }
061    
062        /**
063         * Normally pattern converters are not meant to handle Exceptions although
064         * few pattern converters might.
065         * <p/>
066         * By examining the return values for this method, the containing layout will
067         * determine whether it handles throwables or not.
068         *
069         * @return true if this PatternConverter handles throwables
070         */
071        public boolean handlesThrowable() {
072            return false;
073        }
074    }