log4j配置和使用

时间:2022-02-14 21:56:48

创建文件”log4j.properties“,并写入:

# Configure logging for testing: optionally with log file
log4j.rootLogger=info, stdout, logfile
# debug>info>error

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=./Log/MyTez.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
输出的log文件为”MyTez.log“.


代码中:

声明log文件:

	private static final Logger LOG = LoggerFactory.getLogger(TezLogGenerator.class);
TezLogGenerator为此自定的类;

写入log有:

LOG.info/LOG.error等。