引入Log4j依赖
1 2 3 4 5
| <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
|
配置文件
新建一个log4j.properties文件,文件存放位置:
文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12
| log4j.rootLogger=DEBUG, Console ,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.appender.File = org.apache.log4j.FileAppender log4j.appender.File.File = E://myLog.log log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
|
Java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import org.apache.log4j.Logger;
public class Test {
private static final Logger logger = Logger.getLogger(Test.class);
public static void main(String[] args) { logger.info("调试信息"); logger.error("错误信息"); } }
|
结果
E盘下也有一个myLog.log文件