use of org.apache.camel.spi.ExchangeFormatter in project camel by apache.
the class RedeliveryErrorHandler method logFailedDelivery.
private void logFailedDelivery(boolean shouldRedeliver, boolean newException, boolean handled, boolean continued, boolean isDeadLetterChannel, Exchange exchange, String message, RedeliveryData data, Throwable e) {
if (logger == null) {
return;
}
if (!exchange.isRollbackOnly()) {
if (newException && !data.currentRedeliveryPolicy.isLogNewException()) {
// do not log new exception
return;
}
if (!newException && handled && !data.currentRedeliveryPolicy.isLogHandled()) {
// do not log handled
return;
}
if (!newException && continued && !data.currentRedeliveryPolicy.isLogContinued()) {
// do not log handled
return;
}
if (!newException && shouldRedeliver && !data.currentRedeliveryPolicy.isLogRetryAttempted()) {
// do not log retry attempts
return;
}
if (!newException && !shouldRedeliver && !data.currentRedeliveryPolicy.isLogExhausted()) {
// do not log exhausted
return;
}
}
LoggingLevel newLogLevel;
boolean logStackTrace;
if (exchange.isRollbackOnly()) {
newLogLevel = data.currentRedeliveryPolicy.getRetriesExhaustedLogLevel();
logStackTrace = data.currentRedeliveryPolicy.isLogStackTrace();
} else if (shouldRedeliver) {
newLogLevel = data.currentRedeliveryPolicy.getRetryAttemptedLogLevel();
logStackTrace = data.currentRedeliveryPolicy.isLogRetryStackTrace();
} else {
newLogLevel = data.currentRedeliveryPolicy.getRetriesExhaustedLogLevel();
logStackTrace = data.currentRedeliveryPolicy.isLogStackTrace();
}
if (e == null) {
e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
}
if (newException) {
// log at most WARN level
if (newLogLevel == LoggingLevel.ERROR) {
newLogLevel = LoggingLevel.WARN;
}
String msg = message;
if (msg == null) {
msg = "New exception " + ExchangeHelper.logIds(exchange);
// special for logging the new exception
Throwable cause = e;
if (cause != null) {
msg = msg + " due: " + cause.getMessage();
}
}
if (e != null && logStackTrace) {
logger.log(msg, e, newLogLevel);
} else {
logger.log(msg, newLogLevel);
}
} else if (exchange.isRollbackOnly()) {
String msg = "Rollback " + ExchangeHelper.logIds(exchange);
Throwable cause = exchange.getException() != null ? exchange.getException() : exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
if (cause != null) {
msg = msg + " due: " + cause.getMessage();
}
// should we include message history
if (!shouldRedeliver && data.currentRedeliveryPolicy.isLogExhaustedMessageHistory()) {
// only use the exchange formatter if we should log exhausted message body (and if using a custom formatter then always use it)
ExchangeFormatter formatter = customExchangeFormatter ? exchangeFormatter : (data.currentRedeliveryPolicy.isLogExhaustedMessageBody() || camelContext.isLogExhaustedMessageBody() ? exchangeFormatter : null);
String routeStackTrace = MessageHelper.dumpMessageHistoryStacktrace(exchange, formatter, false);
if (routeStackTrace != null) {
msg = msg + "\n" + routeStackTrace;
}
}
if (newLogLevel == LoggingLevel.ERROR) {
// log intended rollback on maximum WARN level (no ERROR)
logger.log(msg, LoggingLevel.WARN);
} else {
// otherwise use the desired logging level
logger.log(msg, newLogLevel);
}
} else {
String msg = message;
// should we include message history
if (!shouldRedeliver && data.currentRedeliveryPolicy.isLogExhaustedMessageHistory()) {
// only use the exchange formatter if we should log exhausted message body (and if using a custom formatter then always use it)
ExchangeFormatter formatter = customExchangeFormatter ? exchangeFormatter : (data.currentRedeliveryPolicy.isLogExhaustedMessageBody() || camelContext.isLogExhaustedMessageBody() ? exchangeFormatter : null);
String routeStackTrace = MessageHelper.dumpMessageHistoryStacktrace(exchange, formatter, e != null && logStackTrace);
if (routeStackTrace != null) {
msg = msg + "\n" + routeStackTrace;
}
}
if (e != null && logStackTrace) {
logger.log(msg, e, newLogLevel);
} else {
logger.log(msg, newLogLevel);
}
}
}
use of org.apache.camel.spi.ExchangeFormatter in project camel by apache.
the class LogComponent method createEndpoint.
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
LoggingLevel level = getLoggingLevel(parameters);
Logger providedLogger = getLogger(parameters);
if (providedLogger == null) {
// try to look up the logger in registry
Map<String, Logger> availableLoggers = getCamelContext().getRegistry().findByTypeWithName(Logger.class);
if (availableLoggers.size() == 1) {
providedLogger = availableLoggers.values().iterator().next();
LOG.info("Using custom Logger: {}", providedLogger);
} else if (availableLoggers.size() > 1) {
LOG.info("More than one {} instance found in the registry. Falling back to creating logger from URI {}.", Logger.class.getName(), uri);
}
}
LogEndpoint endpoint = new LogEndpoint(uri, this);
endpoint.setLevel(level.name());
setProperties(endpoint, parameters);
if (providedLogger == null) {
endpoint.setLoggerName(remaining);
} else {
endpoint.setProvidedLogger(providedLogger);
}
// first, try to pick up the ExchangeFormatter from the registry
ExchangeFormatter localFormatter = getCamelContext().getRegistry().lookupByNameAndType("logFormatter", ExchangeFormatter.class);
if (localFormatter != null) {
setProperties(localFormatter, parameters);
} else if (localFormatter == null && exchangeFormatter != null) {
// do not set properties, the exchangeFormatter is explicitly set, therefore the
// user would have set its properties explicitly too
localFormatter = exchangeFormatter;
} else {
// if no formatter is available in the Registry, create a local one of the default type, for a single use
localFormatter = new DefaultExchangeFormatter();
setProperties(localFormatter, parameters);
}
endpoint.setLocalFormatter(localFormatter);
return endpoint;
}
use of org.apache.camel.spi.ExchangeFormatter in project camel by apache.
the class ExpressionBuilder method messageHistoryExpression.
/**
* Returns the message history (including exchange details or not)
*/
public static Expression messageHistoryExpression(final boolean detailed) {
return new ExpressionAdapter() {
private ExchangeFormatter formatter;
public Object evaluate(Exchange exchange) {
ExchangeFormatter ef = null;
if (detailed) {
// use the exchange formatter to log exchange details
ef = getOrCreateExchangeFormatter(exchange.getContext());
}
return MessageHelper.dumpMessageHistoryStacktrace(exchange, ef, false);
}
private ExchangeFormatter getOrCreateExchangeFormatter(CamelContext camelContext) {
if (formatter == null) {
Set<ExchangeFormatter> formatters = camelContext.getRegistry().findByType(ExchangeFormatter.class);
if (formatters != null && formatters.size() == 1) {
formatter = formatters.iterator().next();
} else {
// setup exchange formatter to be used for message history dump
DefaultExchangeFormatter def = new DefaultExchangeFormatter();
def.setShowExchangeId(true);
def.setMultiline(true);
def.setShowHeaders(true);
def.setStyle(DefaultExchangeFormatter.OutputStyle.Fixed);
try {
Integer maxChars = CamelContextHelper.parseInteger(camelContext, camelContext.getGlobalOption(Exchange.LOG_DEBUG_BODY_MAX_CHARS));
if (maxChars != null) {
def.setMaxChars(maxChars);
}
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
formatter = def;
}
}
return formatter;
}
@Override
public String toString() {
return "messageHistory(" + detailed + ")";
}
};
}
Aggregations