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
java logging configuration logback


source to share


No one has answered this question yet

Check out similar questions:

3061
grep file but show multiple surrounding lines?
1571
How to avoid Java code in JSP files?
1315
How do I create a file and write it in Java?
231
Why not use java.util.logging?
196
Logging Levels - Logback - Thumb rule to assign log levels
122
log4j vs logback
ten
Logback: how to log only errors to a file
1
Dynamically adding Appender to logback
1
TimeBasedRollingPolicy Program Log
0
How to log in from a log configuration object



All Articles
Loading...
X
Show
Funny
Dev
Pics