最近版本的Log4j,使用maven,导入两个jar包,依赖如下:
- <dependencies>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-api</artifactId>
- <version>2.6.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-core</artifactId>
- <version>2.6.2</version>
- </dependency>
- </dependencies>
然后在自己的resource文件下,新建
log4j.properties,运行不了。
修改依赖文件:
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
然后就可以了,不同的的版本不一样,新版本的用法要继续学习。
然后配置文件详解:
log4j.rootLogger = INFO, File, stdout rootLogger是设置日志级别和输出端的地方。 比如这句,
日志级别是INFO, 输出地方WieFile 和 stdout两个地方。
然后在下面要配置输出地方的位置,格式,比如下面:
Here, we use two appenders, one appender to log to a file and another one to log to the console. The first appender uses the and the second one the . The first appender has a attribute, where the logging file is set, whereas the second one has a attribute which is set to . Both appenders use a layout for their log files. The is used and the is set to:
is used to set the date pattern, is used to print the class name, to print the message, and to leave an empty line.
log4j.appender.File=org.apache.log4j.FileAppender
log4j.appender.File.File=C:\\logs\\logs.log
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.conversionPattern=%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n
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{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n

被折叠的 条评论
为什么被折叠?



