Log4j2 Problem

Discussion in 'Programming' started by Soldier__, Jun 8, 2021.

Thread Status:
Not open for further replies.
  1. I tried to use log4j2 to make a standalone program.
    I configured the log4j2 and it seems work properly with LogManager.getLogger()
    And I find that it will use my configured format to log with logger.info()
    But with System.out.println() It will not be in the configured format, so is with the e.printStackTrace().

    As far as I know, Spigot plugin using System.out.println() can write into the log even if not using logger.info()

    Does anybody knows how to make it work like Spigot?

    Here is my log4j2.xml and main class in kotlin

    Code (Text):
    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="INFO">
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level]: [%logger{36}] %msg%n"/>
            </Console>
            <RollingRandomAccessFile name="File" fileName="logs/latest.log" filePattern="logs/%d{yyyy-MM-dd}-%i.log.gz">
                <PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level]: [%logger{36}] %msg%n"/>
                <Policies>
                    <TimeBasedTriggeringPolicy/>
                    <OnStartupTriggeringPolicy/>
                </Policies>
                <DefaultRolloverStrategy max="1000"/>
            </RollingRandomAccessFile>
        </Appenders>

        <Loggers>
            <Root level="info">
                <AppenderRef ref="Console"/>
                <AppenderRef ref="File"/>
            </Root>
        </Loggers>
    </Configuration>
    Code (Java):
    package me.zhanshi123.masterserver

    import com.google.gson.Gson
    import me.zhanshi123.masterserver.console.ConsoleScanner
    import me.zhanshi123.masterserver.console.command.CommandManager
    import me.zhanshi123.masterserver.database.Database
    import me.zhanshi123.masterserver.module.DailyReward
    import org.apache.logging.log4j.LogManager
    import org.apache.logging.log4j.Logger
    import kotlin.system.exitProcess

    object Main {

        val logger: Logger by lazy {
            LogManager.getLogger("Master")
        }

        val gson: Gson by lazy {
            Gson()
        }

        val consoleScanner = ConsoleScanner()

        @JvmStatic
        fun main(args: Array<String>) {
            val start = System.currentTimeMillis()
            logger.info("starting...")
            CommandManager.init()
            consoleScanner.start()
            Database.init()
            DailyReward.init()
            val end = System.currentTimeMillis()
            logger.info("complete in ${end - start}ms!")
        }

        fun shutdown() {
            logger.info("stopping")
            consoleScanner.interrupt()
            DailyReward.release()
            Database.release()
            logger.info("exit process with code 0")
            exitProcess(0)
        }
    }
     
     
  2. md_5

    Administrator Developer

    • Funny Funny x 1
    • Informative Informative x 1
Thread Status:
Not open for further replies.