All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juli.logging.ch.qos.logback.classic.pattern.EnsureExceptionHandling Maven / Gradle / Ivy

There is a newer version: 9.0.88
Show newest version
/**
 * Logback: the reliable, generic, fast and flexible logging framework.
 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
 *
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *
 *   or (per the licensee's choosing)
 *
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 */
package org.apache.juli.logging.ch.qos.logback.classic.pattern;

import org.apache.juli.logging.ch.qos.logback.classic.LoggerContext;
import org.apache.juli.logging.ch.qos.logback.classic.spi.ILoggingEvent;
import org.apache.juli.logging.ch.qos.logback.core.Context;
import org.apache.juli.logging.ch.qos.logback.core.pattern.CompositeConverter;
import org.apache.juli.logging.ch.qos.logback.core.pattern.Converter;
import org.apache.juli.logging.ch.qos.logback.core.pattern.ConverterUtil;
import org.apache.juli.logging.ch.qos.logback.core.pattern.PostCompileProcessor;

public class EnsureExceptionHandling implements PostCompileProcessor {

    /**
     * This implementation checks if any of the converters in the chain handles
     * exceptions. If not, then this method adds a
     * {@link ExtendedThrowableProxyConverter} instance to the end of the chain.
     * 

* This allows appenders using this layout to output exception information event * if the user forgets to add %ex to the pattern. Note that the appenders * defined in the Core package are not aware of exceptions nor LoggingEvents. *

* If for some reason the user wishes to NOT print exceptions, then she can add * %nopex to the pattern. * * */ public void process(Context context, Converter head) { if (head == null) { // this should never happen throw new IllegalArgumentException("cannot process empty chain"); } if (!chainHandlesThrowable(head)) { Converter tail = ConverterUtil.findTail(head); Converter exConverter = null; LoggerContext loggerContext = (LoggerContext) context; if (loggerContext.isPackagingDataEnabled()) { exConverter = new ExtendedThrowableProxyConverter(); } else { exConverter = new ThrowableProxyConverter(); } tail.setNext(exConverter); } } /** * This method computes whether a chain of converters handles exceptions or not. * * @param head The first element of the chain * @return true if it can handle throwables contained in logging events */ public boolean chainHandlesThrowable(Converter head) { Converter c = head; while (c != null) { if (c instanceof ThrowableHandlingConverter) { return true; } else if (c instanceof CompositeConverter) { if (compositeHandlesThrowable((CompositeConverter) c)) { return true; } } c = c.getNext(); } return false; } /** * This method computes whether a composite converter handles exceptions or not. * * @param compositeConverter The composite converter * @return true if it can handle throwables contained in logging events */ public boolean compositeHandlesThrowable(CompositeConverter compositeConverter) { Converter childConverter = compositeConverter.getChildConverter(); for (Converter c = childConverter; c != null; c = c.getNext()) { if (c instanceof ThrowableHandlingConverter) { return true; } else if (c instanceof CompositeConverter) { boolean r = compositeHandlesThrowable((CompositeConverter) c); if (r) return true; } } return false; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy