ThrowableInformationPatternConverter xref

View Javadoc

    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
    18  package org.apache.log4j.pattern;
    19
    20  import org.apache.log4j.spi.LoggingEvent;
    21  import org.apache.log4j.spi.ThrowableInformation;
    22
    23
    24  /**
    25   * Outputs the ThrowableInformation portion of the LoggingEvent.
    26   * By default, outputs the full stack trace.  %throwable{none}
    27   * or %throwable{0} suppresses the stack trace. %throwable{short}
    28   * or %throwable{1} outputs just the first line.  %throwable{n}
    29   * will output n lines for a positive integer or drop the last
    30   * -n lines for a negative integer.
    31   *
    32   * @author Paul Smith
    33   *
    34   */
    35  public class ThrowableInformationPatternConverter
    36    extends LoggingEventPatternConverter {
    37
    38    /**
    39     * Maximum lines of stack trace to output.
    40     */
    41    private int maxLines = Integer.MAX_VALUE;
    42
    43    /**
    44     * Private constructor.
    45     * @param options options, may be null.
    46     */
    47    private ThrowableInformationPatternConverter(
    48      final String[] options) {
    49      super("Throwable", "throwable");
    50
    51      if ((options != null) && (options.length > 0)) {
    52        if("none".equals(options[0])) {
    53            maxLines = 0;
    54        } else if("short".equals(options[0])) {
    55            maxLines = 1;
    56        } else {
    57            try {
    58                maxLines = Integer.parseInt(options[0]);
    59            } catch(NumberFormatException ex) {
    60            }
    61        }
    62      }
    63    }
    64
    65    /**
    66     * Gets an instance of the class.
    67      * @param options pattern options, may be null.  If first element is "short",
    68     * only the first line of the throwable will be formatted.
    69     * @return instance of class.
    70     */
    71    public static ThrowableInformationPatternConverter newInstance(
    72      final String[] options) {
    73      return new ThrowableInformationPatternConverter(options);
    74    }
    75
    76    /**
    77     * {@inheritDoc}
    78     */
    79    public void format(final LoggingEvent event, final StringBuffer toAppendTo) {
    80      if (maxLines != 0) {
    81        ThrowableInformation information = event.getThrowableInformation();
    82
    83        if (information != null) {
    84          String[] stringRep = information.getThrowableStrRep();
    85
    86          int length = stringRep.length;
    87          if (maxLines < 0) {
    88              length += maxLines;
    89          } else if (length > maxLines) {
    90              length = maxLines;
    91          }
    92
    93          for (int i = 0; i < length; i++) {
    94              String string = stringRep[i];
    95              toAppendTo.append(string).append("\n");
    96          }
    97        }
    98      }
    99    }
    100
    101   /**
    102    * This converter obviously handles throwables.
    103    * @return true.
    104    */
    105   public boolean handlesThrowable() {
    106     return true;
    107   }
    108 }