Search code examples
javaspringspring-mvcjakarta-eelog4j2

how does record error info in somewhere when running jar application


I'm have spring boot application below is get RuntimeException sample.

@RestController
public class HelloController {
    @GetMapping("/getRuntimeError")
    public String hello() {
        throw new RuntimeException("error");
    }
}

run on linux

start command:

nohup java -jar app.jar > app.log

when runtime error occurred I'd use vim edit app.log check what error is.

but when app.log file growth size 2GB, I can't view what exactly the error because file too large.

so I'm using log4j2 RollingFile logger record log, but can't actual RuntimeException stack info.

this is log4j2 config

<?xml version="1.0" encoding="utf-8"?>
<configuration status="warn">
    <Properties>
        <Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
        <Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
        <Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{%5p} %clr{%pid}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
    </Properties>
    <appenders>
        <console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout
                    pattern="${sys:CONSOLE_LOG_PATTERN}"/>
        </console>
        <RollingFile name="RollingFileInfo" fileName="logs/info.log"
                     filePattern="logs/log-INFO-%d{yyyy-MM-dd}_%i.log.gz"
                     append="false">
            <PatternLayout pattern="${CONSOLE_LOG_PATTERN}"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="200MB"/>
            </Policies>
            <DefaultRolloverStrategy max="15"/>
        </RollingFile>
    </appenders>
    <loggers>
        <root level="info" includeLocation="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
        </root>
    </loggers>
</configuration>

using this command

nohup java -jar app.jar > /dev/null > 2>&1 &

curl error info

I'm problem is: how can record Error and also avoid file too large🙂

any suggestions

Thanks for the help


Solution

  • From what I understand your logger is configured to log to the console and then you redirect that output to the file.

    Instead you should configure logger to write to a file and then it's easy to configure log rotation policy (see spring boot documentation).

    Would be much easier to help if you provide your log4j configuration.

    Edit: Thanks for providing log4j config.

    The problem can be that your log4j file is not loaded by spring. If you want to use log4j place your file in the resources folder and add dependencies to your maven or gradle file:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    </dependencies>
    

    Remember to exclude the spring-boot-starter-logging. Your logs should be now visible in the console (by console I mean the terminal where spring boot application is running, not where curl client is executed) and in the info.log file.

    You could also configure the same thing using logback file instead of log4j. You wouldn't have to change your build file in this case, just put the confing into spring boot application.properties file.