logback:异常堆栈跟踪之前的换行符,但不是吗?

时间:2015-05-27 10:43:55

标签: logback

如果我在记录模式中的异常堆栈跟踪之前添加换行符:

<Pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} %msg %n %rEx{short} %n</Pattern>

然后打印一个额外的换行符,因此正常的日志记录语句最终会在它们之间留下空行。

如果我删除了额外换行符:

<Pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} %msg %rEx{short} %n</Pattern>

然后堆栈跟踪不会在新行上开始,这使得它们难以阅读。

任何人都可以想到一种获取logback的方法,只有在存在堆栈跟踪时才打印额外的换行符,否则不会?

1 个答案:

答案 0 :(得分:2)

我有一个有效的解决方案,但我不喜欢它,因为它过度设计,应该可以更优雅。

这是一个更通用的问题的特定情况:仅当某个评估者(检查存在异常的评估者)匹配时才输出模式。我很久以前就从SO的某处获得了实现。

需要janino。如果有人比我经验更多的回归可以将其减少到更合理的代码量,请执行。

IfPresentConverter.java

public class IfPresentConverter extends CompositeConverter<ILoggingEvent> {
    private final List<EventEvaluator<ILoggingEvent>> evaluatorList = new ArrayList<>();
    private int errorCount = 0;

    @Override
    @SuppressWarnings("unchecked")
    public void start() {
        final List<String> optionList = getOptionList();
        final Map<?, ?> evaluatorMap = (Map<?, ?>) getContext().getObject(CoreConstants.EVALUATOR_MAP);

        for (final String evaluatorStr : optionList) {
            final EventEvaluator<ILoggingEvent> ee = (EventEvaluator<ILoggingEvent>) evaluatorMap.get(evaluatorStr);
            if (ee != null) {
                evaluatorList.add(ee);
            }
        }

        if (evaluatorList.isEmpty()) {
            addError("At least one evaluator is expected, but you have declared none.");
            return;
        }

        super.start();
    }

    @Override
    public String convert(final ILoggingEvent event) {
        boolean evalResult = true;
        for (final EventEvaluator<ILoggingEvent> ee : evaluatorList) {
            try {
                if (!ee.evaluate(event)) {
                    evalResult = false;
                    break;
                }
            } catch (final EvaluationException eex) {
                evalResult = false;

                errorCount++;
                if (errorCount < CoreConstants.MAX_ERROR_COUNT) {
                    addError("Exception thrown for evaluator named [" + ee.getName() + "].", eex);
                } else {
                    final ErrorStatus errorStatus = new ErrorStatus("Exception thrown for evaluator named [" + ee.getName() + "].", this, eex);
                    errorStatus.add(new ErrorStatus("This was the last warning about this evaluator's errors. " + "We don't want the StatusManager to get flooded.", this));
                    addStatus(errorStatus);
                }
            }
        }

        if (evalResult) {
            return super.convert(event);
        } else {
            return CoreConstants.EMPTY_STRING;
        }
    }

    @Override
    protected String transform(final ILoggingEvent event, final String in) {
        return in;
    }
}

logback.xml

<configuration>
    <conversionRule conversionWord="ifPresent" converterClass="org.example.IfPresentConverter"/>

    <evaluator name="has_ex">
        <expression>return throwableProxy != null;</expression>
    </evaluator>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>lorem ipsum %msg{}%ifPresent(\n%ex{full}){has_ex}%n</pattern>
        </encoder>
    </appender>

    <root>
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>