Logback: one file for two appenders

34 Views Asked by At

I'm using Spring Boot 2.5.7 and logback over slf4j. I need to trim the stacktrace when logging errors from a third-party library.

For this purpose, I have created an additional appender that uses a converter to cut off the stacktrace. But when I try to write this event to the same file, the appender either doesn't work at all or throws a configuration error.

conversionRule("trimmed_message", MessageStacktraceTrimmerTemplate)

appender("STDOUT", RollingFileAppender) {
    file = "${LOG_PATH}/nohup.service"
    rollingPolicy(SizeAndTimeBasedRollingPolicy) {
        fileNamePattern = "${LOG_ARCHIVE}/stdout/nohup.service.%d{yyyy-MM-dd}.%i"
        maxHistory = 30
        maxFileSize = FileSize.valueOf("10 MB")
        totalSizeCap = FileSize.valueOf("2 GB")
    }

    encoder(PatternLayoutEncoder) {
        charset = StandardCharsets.UTF_8
        pattern = "%date %-5level - %msg%n"
    }
}

appender("TRIMMED_STDOUT", RollingFileAppender) {
    file = "${LOG_PATH}/nohup.service"

    encoder(PatternLayoutEncoder) {
        charset = StandardCharsets.UTF_8
        pattern = "%date %-5level - %trimmed_message%n"
    }
}

root(INFO, ["STDOUT"])
logger("com.package.my", INFO, ["STDOUT"], false)
logger("com.netflix.discovery", INFO, ["TRIMMED_STDOUT"], false)

With this configuration appender not working, but service starts successfully. If I add a rollingPolicy to TRIMMED_STDOUT or change type to FileApppender, I get:

ERROR in ch.qos.logback.core.rolling.RollingFileAppender[TRIMMED_STDOUT] - 'FileNamePattern' option has the same value "logs/archive/stdout/nohup.service.%d{yyyy-MM-dd}.%i" as that given for appender [STDOUT] defined earlier.

ERROR in ch.qos.logback.core.FileAppender[TRIMMED_STDOUT] - 'File' option has the same value "logs/nohup.service" as that given for appender [STDOUT] defined earlier.

I wanted to use Prudent Mode, but I couldn't figure out how, and it looks redundant. What am I doing wrong and is what I have in mind feasible?

0

There are 0 best solutions below