通过java代码给log4j指定appender

工具代码

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!");
posted @ 2018-10-10 22:19  liqipeng  阅读(1900)  评论(0编辑  收藏  举报