简体   繁体   中英

Dummy log4j configuration (for tests)

是否可以通过简单的方式从Java代码配置log4j-结果我希望将所有日志都写入stdout?

static Logger logger = Logger.getLogger(NewMain1.class);

public static void main(String[] args) {
    BasicConfigurator.configure();

    logger.info("out");
}

Log4j has a ConsoleAppender to redirect the logs to console. Here is a sample config:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Logger root = Logger.getRootLogger();
root.addAppender(new ConsoleAppender(
    new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM