Logback buffer messages before HDD is available

Viewed 23

So i have an embeedded java device that runs code as early as possible after boot. But HDD is not available immediately.

I've found out that using logback with xml configuration is not working as whenever it starts it checks for existance of log file which is not possible as HDD is not mounted yet.

So I've programatically added FileAppender when HDD is started. But this results in lots of messages being lost as there is no appender when the device boots and I am unable to get logs from the time when it booted till the HDD is available.

Is there a way to buffer these messages and print them to the file once the HDD is booted?

my way of initing appender:

fun startLogger() {
    logger.debug("Starting HDD logger")
    val lc = LoggerFactory.getILoggerFactory() as LoggerContext
    lc.reset()
    try {
        val root = lc.getLogger(Logger.ROOT_LOGGER_NAME)
        logFileAppender.apply {
            context = lc
            name = "logFile"
            file = "/mnt/hdd/cz.myq.roger.ricoh/logs/logfile.log"
            encoder = PatternLayoutEncoder().apply {
                context = lc
                pattern = "%-12date{yyyy-MM-dd HH:mm:ss.SSS} %logger{12} %-5level - %msg%n"
                start()
            }
            isAppend = true
            rollingPolicy = TimeBasedRollingPolicy<ILoggingEvent>().apply {
                context = lc
                fileNamePattern = "/mnt/hdd/cz.myq.roger.ricoh/logs/logfile-%d{yyyy-MM-dd}.log.zip"
                maxHistory = 7
                setTotalSizeCap(FileSize.valueOf("100MB"))
            }.also {
                it.setParent(this@apply)
                it.start()
            }
            start()
        }
        root.addAppender(logFileAppender)

        lc.getLogger(DNSIncoming::class.java).level = Level.WARN
    } catch (e: Exception) {
        logger.error("Error starting log", e)
    }
}
0 Answers
Related