Write an entry to the OutOfMemoryError file?

I would like to be able to capture the OutOfMemoryError and write it to a file. Currently logback only puts it in the console.

How can I configure logback to write to a file without explicitly capturing Error and calling log.error () myself?

Since it is difficult to predict where such an error will occur, it would be best to "link" the file application to the console application. Although how can this be done?

For the same completeness, this is the current configuration I'm using:

  

<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>

      

+3


source to share





All Articles