Logback giving error无法将类'java.lang.String'的对象'3 gb'强制转换为类'ch.qos.logback.core.util.FileSize'

时间:2016-10-20 18:30:55

标签: java logging spring-boot logback logback-groovy

我有一个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"])

注意:我甚至尝试过这些字符串:文件大小:3gb3 gb3GB3096mb,{{1 }},3096 mb

1 个答案:

答案 0 :(得分:6)

Try replacing the line

    totalSizeCap = "3 gb"

with

    totalSizeCap = FileSize.valueOf("3 gb")

You'll need to add the line import ch.qos.logback.core.util.FileSize; as well.

The setTotalSizeCap method of the TimeBasedRollingPolicy class takes a FileSize object, not a string. The static valueOf method in FileSize should do the necessary conversion from string to FileSize.