Logback 给出错误 Cannot cast object '3 gb' with class 'java.lang.String' to class 'ch.qos.logback.core.util.FileSize'

Logback giving error Cannot cast object '3 gb' with class 'java.lang.String' to class 'ch.qos.logback.core.util.FileSize'

我有一个 springboot 应用程序,在尝试使用带有 groovy 配置的 logback 进行登录时,我收到以下错误:

    Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '3 gb' with class 'java.lang.String' to class 'ch.qos.logback.core.util.FileSize'
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)

我的 groovy 配置文件:

import ch.qos.logback.classic.PatternLayout
import static ch.qos.logback.classic.Level.INFO

scan("60 seconds")
def LOG_PATH = "logs"
def LOG_ARCHIVE = "${LOG_PATH}/archive"



appender("RollingFile-Appender", RollingFileAppender) {
    file = "${LOG_PATH}/rollingfile.log"
    rollingPolicy(TimeBasedRollingPolicy) {
        fileNamePattern = "${LOG_ARCHIVE}/Rainbow_Notifications.log%d{yyyy-MM-dd}.log"
        maxHistory = 30
        totalSizeCap = "3 gb"
    }
    encoder(PatternLayoutEncoder) {
        pattern = "%msg%n"
    }
}


logger("com.something", INFO, ["RollingFile-Appender"])

注意:我什至试过这些 strings:as 文件大小:3gb3 gb3GB3096mb,3096 mb,3096 MB

尝试替换行

    totalSizeCap = "3 gb"

    totalSizeCap = FileSize.valueOf("3 gb")

您还需要添加行 import ch.qos.logback.core.util.FileSize;

setTotalSizeCap method of the TimeBasedRollingPolicy class takes a FileSize 对象,不是字符串。 FileSize 中的静态 valueOf 方法应该进行从字符串到 FileSize.

的必要转换