How does log4j2 output exception information to a log file?

problem description

the original project uses log4j to manage logs, but now it cannot automatically output exception information to the log file after being upgraded to log4j2.

the environmental background of the problems and what methods you have tried

now you can manually catch exceptions and print exception messages to the console and log files via logger.error ("", e);), but the project is too big and it takes too much time to modify it, so you want to capture it automatically like log4j and output it to the log file.

related codes

log4j2 profile

<?xml version="1.0" encoding="UTF-8"?>
<!-- log4j2  -->
<!--  trace<debug<info<warn<error<fatal -->
<configuration status="debug">
    <!--  -->
    <Properties>
        <!--  -->
        <Property name="pattern">%d{yyyy-MM-dd HH:mm:ss,SSS} (%F:%L) %5p %m%n</Property>
        <!--  -->
        <Property name="filePath">D:/Tomcat 6.0/log4j/equipmentLease/</Property>
        <!-- debug -->
        <Property name="debugFileName">${filePath}/debug.log</Property>
        <!-- info -->
        <Property name="infoFileName">${filePath}/info.log</Property>
        <!-- error -->
        <Property name="errorFileName">${filePath}/error.log</Property>
    </Properties>
    
    <!--  -->
    <appenders>
        <!--  -->
        <Console name="console" target="SYSTEM_OUT">
            <!--  -->
            <PatternLayout pattern="${pattern}"/>
        </Console>
        
        <!-- debug -->
        <RollingFile name="debug_appender" fileName="${debugFileName}"
                filePattern="${filePath}/%d{yyyy-MM-dd}/debug_%i.log">
            <!--  -->
            <Filters>
                <!-- debuginfo -->
                <ThresholdFilter level="debug"/>
                <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <!--  -->
            <PatternLayout pattern="${pattern}"/>
            <!--  -->
            <Policies>
                <!--  -->
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <!--  -->
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
        </RollingFile>
        
        <!-- info -->
        <RollingFile name="info_appender" fileName="${infoFileName}"
                filePattern="${filePath}/%d{yyyy-MM-dd}/info_%i.log">
            <!--  -->
            <Filters>
                <!-- infoerror -->
                <ThresholdFilter level="info"/>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <!--  -->
            <PatternLayout pattern="${pattern}"/>
            <!--  -->
            <Policies>
                <!--  -->
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <!--  -->
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
        </RollingFile>
        
        <!-- error -->
        <RollingFile name="error_appender" fileName="${errorFileName}"
                filePattern="${filePath}/%d{yyyy-MM-dd}/error_%i.log">
            <!--  -->
            <Filters>
                <!-- error -->
                <ThresholdFilter level="error"/>
            </Filters>
            <!--  -->
            <PatternLayout pattern="${pattern}"/>
            <Policies>
                <!--  -->
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <!--  -->
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
        </RollingFile>
    </appenders>
    
    <!-- logger -->
    <loggers>
        
        <!-- logger -->
        <!-- debug appender -->
        <Root level="debug">
            <!--  -->
            <AppenderRef ref="console"/>
            <!-- debug -->
            <AppenderRef ref="debug_appender"/>
            <!-- info -->
            <AppenderRef ref="info_appender"/>
            <!-- error -->
            <AppenderRef ref="error_appender"/>
        </Root>
    </loggers>
</configuration>
May.13,2021

has been invited.
it's just that I don't know much about this problem. Is there any way to log without calling the log method? Does Log4j still have the function of automatically catching exceptions? can you post a document address to have a look at it (sincerely ask for advice).

your requirements I can think of at present is through the Spring AOP way to achieve, in the afterThrowing () method, the exception catch print, and add the scope of the section to the package or class we want to deal with, the problem is solved, of course, only deal with the scope of unhandled exceptions, scope handling exceptions also need you to manually add log.info print.

I don't know if I got to the point.

 @AfterThrowing(value = "", throwing = "e")
    public void afterThrowing(Throwable e) {
       
       logger.error("", e);
        
    }
Menu