log4j 和 log4j2
方式一:log4j2.xml
添加 jar 包
1 <!-- log4j-core --> 2 <!-- 3 <dependency> 4 <groupId>org.apache.logging.log4j</groupId> 5 <artifactId>log4j-core</artifactId> 6 <version>2.6.2</version> 7 </dependency>
文件配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <Configuration> 3 <Appenders> 4 <Console name="STDOUT" target="SYSTEM_OUT"> 5 <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> 6 </Console> 7 </Appenders> 8 <Loggers> 9 <Logger name="com.opensymphony.xwork2" level="debug"/> 10 <Logger name="org.apache.struts2" level="debug"/> 11 <Root level="warn"> 12 <AppenderRef ref="STDOUT"/> 13 </Root> 14 </Loggers> 15 </Configuration>
方式二:log4j.properties
添加 jar 包
1 <!-- log4j --> 2 <dependency> 3 <groupId>log4j</groupId> 4 <artifactId>log4j</artifactId> 5 <version>1.2.17</version> 6 </dependency>
文件配置:
### direct log messages to stdout ### 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{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c:/mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ### log4j.rootLogger=info, stdout
时间: 2024-10-08 13:41:58