通过java代码给log4j指定appender

时间:2021-07-02 21:51:07

工具代码

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.LogManager;
import org.apache.log4j.PatternLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogUtil {

    public static final Logger LOGGER = LoggerFactory.getLogger(LogUtil.class);

    static {
        org.apache.log4j.Logger logger = LogManager.getLogger(LOGGER.getName());
        ConsoleAppender consoleAppender = new ConsoleAppender(new PatternLayout("%d [%t] %-5p [%c] - %m%n"));
        logger.addAppender(consoleAppender);
    }

}

使用

LogUtil.LOGGER.info("hello!");