Search in sources :

Example 1 with LoggingLevel

use of org.apache.camel.LoggingLevel in project camel by apache.

the class LogEndpoint method createLogger.

/**
     * Creates the logger {@link Processor} to be used.
     */
protected Processor createLogger() throws Exception {
    Processor answer;
    // setup a new logger here
    CamelLogger camelLogger;
    LoggingLevel loggingLevel = LoggingLevel.INFO;
    if (level != null) {
        loggingLevel = LoggingLevel.valueOf(level);
    }
    if (providedLogger == null) {
        camelLogger = new CamelLogger(loggerName, loggingLevel, getMarker());
    } else {
        camelLogger = new CamelLogger(providedLogger, loggingLevel, getMarker());
    }
    if (getGroupSize() != null) {
        answer = new ThroughputLogger(camelLogger, getGroupSize());
    } else if (getGroupInterval() != null) {
        Boolean groupActiveOnly = getGroupActiveOnly() != null ? getGroupActiveOnly() : Boolean.TRUE;
        Long groupDelay = getGroupDelay();
        answer = new ThroughputLogger(camelLogger, this.getCamelContext(), getGroupInterval(), groupDelay, groupActiveOnly);
    } else {
        answer = new CamelLogProcessor(camelLogger, localFormatter);
    }
    // the logger is the processor
    setProcessor(answer);
    return answer;
}
Also used : CamelLogger(org.apache.camel.util.CamelLogger) CamelLogProcessor(org.apache.camel.processor.CamelLogProcessor) Processor(org.apache.camel.Processor) LoggingLevel(org.apache.camel.LoggingLevel) CamelLogProcessor(org.apache.camel.processor.CamelLogProcessor) ThroughputLogger(org.apache.camel.processor.ThroughputLogger)

Example 2 with LoggingLevel

use of org.apache.camel.LoggingLevel 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);
        }
    }
}
Also used : LoggingLevel(org.apache.camel.LoggingLevel) ExchangeFormatter(org.apache.camel.spi.ExchangeFormatter) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 3 with LoggingLevel

use of org.apache.camel.LoggingLevel in project camel by apache.

the class CamelLogger method log.

public void log(String message, Throwable exception, LoggingLevel loggingLevel) {
    LoggingLevel oldLogLevel = getLevel();
    setLevel(loggingLevel);
    log(message, exception);
    setLevel(oldLogLevel);
}
Also used : LoggingLevel(org.apache.camel.LoggingLevel)

Example 4 with LoggingLevel

use of org.apache.camel.LoggingLevel in project camel by apache.

the class LogDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    ObjectHelper.notEmpty(message, "message", this);
    // use simple language for the message string to give it more power
    Expression exp = routeContext.getCamelContext().resolveLanguage("simple").createExpression(message);
    // get logger explicitely set in the definition
    Logger logger = this.getLogger();
    // get logger which may be set in XML definition
    if (logger == null && ObjectHelper.isNotEmpty(loggerRef)) {
        logger = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), loggerRef, Logger.class);
    }
    if (logger == null) {
        // first - try to lookup single instance in the registry, just like LogComponent
        Map<String, Logger> availableLoggers = routeContext.lookupByType(Logger.class);
        if (availableLoggers.size() == 1) {
            logger = availableLoggers.values().iterator().next();
            LOG.debug("Using custom Logger: {}", logger);
        } else if (availableLoggers.size() > 1) {
            // we should log about this somewhere...
            LOG.debug("More than one {} instance found in the registry. Falling back to create logger by name.", Logger.class.getName());
        }
    }
    if (logger == null) {
        String name = getLogName();
        if (name == null) {
            name = routeContext.getCamelContext().getGlobalOption(Exchange.LOG_EIP_NAME);
            if (name != null) {
                LOG.debug("Using logName from CamelContext properties: {}", name);
            }
        }
        if (name == null) {
            name = routeContext.getRoute().getId();
            LOG.debug("LogName is not configured, using route id as logName: {}", name);
        }
        logger = LoggerFactory.getLogger(name);
    }
    // should be INFO by default
    LoggingLevel level = getLoggingLevel() != null ? getLoggingLevel() : LoggingLevel.INFO;
    CamelLogger camelLogger = new CamelLogger(logger, level, getMarker());
    return new LogProcessor(exp, camelLogger);
}
Also used : CamelLogger(org.apache.camel.util.CamelLogger) LoggingLevel(org.apache.camel.LoggingLevel) Expression(org.apache.camel.Expression) LogProcessor(org.apache.camel.processor.LogProcessor) Logger(org.slf4j.Logger) CamelLogger(org.apache.camel.util.CamelLogger)

Example 5 with LoggingLevel

use of org.apache.camel.LoggingLevel in project camel by apache.

the class EnumConverterTest method testMandatoryConvertEnum.

public void testMandatoryConvertEnum() throws Exception {
    LoggingLevel level = context.getTypeConverter().mandatoryConvertTo(LoggingLevel.class, "DEBUG");
    assertEquals(LoggingLevel.DEBUG, level);
}
Also used : LoggingLevel(org.apache.camel.LoggingLevel)

Aggregations

LoggingLevel (org.apache.camel.LoggingLevel)12 CamelLogger (org.apache.camel.util.CamelLogger)3 Exchange (org.apache.camel.Exchange)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 ExchangeFormatter (org.apache.camel.spi.ExchangeFormatter)2 Logger (org.slf4j.Logger)2 File (java.io.File)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 Expression (org.apache.camel.Expression)1 Processor (org.apache.camel.Processor)1 GenericFileExclusiveReadLockStrategy (org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy)1 CamelLogProcessor (org.apache.camel.processor.CamelLogProcessor)1 DefaultExchangeFormatter (org.apache.camel.processor.DefaultExchangeFormatter)1 LogProcessor (org.apache.camel.processor.LogProcessor)1 ThroughputLogger (org.apache.camel.processor.ThroughputLogger)1 IdempotentRepository (org.apache.camel.spi.IdempotentRepository)1