Logback将OutOfMemoryError写入文件?

时间:2015-05-21 14:54:59

标签: java logging configuration logback

我希望能够捕获OutOfMemoryError并将其写入文件。目前只有logback才能将其放入控制台。

如何配置回写以写入文件而不显式捕获错误并自行调用log.error()?

由于很难预测这种错误的发生位置,最好将文件追加器“绑定”到控制台追加器。虽然怎么可能呢?

为了完整性,这是我正在使用的当前配置:

    

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} | %thread | %highlight(%-5level) | %cyan(%logger{1}) | %method | %line | %m%n</pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>${STDOUT_LOG_LEVEL}</level>
    </filter>
</appender>

<appender name="FILE_ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>.\Hub_logs\%d{yyyy-MM,aux}\%d{yyyy-MM-dd}_all.%i.log</fileNamePattern>
        <if condition='p("NUMBER_OF_DAYS_TO_KEEP").matches("^-?\\d+$")'>
            <then>
                <if condition='Integer.parseInt(p("NUMBER_OF_DAYS_TO_KEEP")) &lt; 0'>
                    <then>
                        <maxHistory>0</maxHistory>
                    </then>
                    <else>
                        <maxHistory>${NUMBER_OF_DAYS_TO_KEEP}</maxHistory>
                    </else>
                </if>
            </then>
            <else>
                <maxHistory>0</maxHistory>
            </else>
        </if>
        <cleanHistoryOnStart>true</cleanHistoryOnStart>
        <!-- or whenever the file size reaches given value -->
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>250MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} | %-5level | %thread | %logger{1} | %method | %line | %m%n</pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>${FILE_ALL_LOG_LEVEL}</level>
    </filter>
</appender>

<root level="TRACE">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="FILE_ALL"/>
</root>

0 个答案:

没有答案