Log4j使用

最近版本的Log4j,使用maven,导入两个jar包,依赖如下:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.logging.log4j</groupId>
  4. <artifactId>log4j-api</artifactId>
  5. <version>2.6.2</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.logging.log4j</groupId>
  9. <artifactId>log4j-core</artifactId>
  10. <version>2.6.2</version>
  11. </dependency>
  12. </dependencies>

然后在自己的resource文件下,新建

log4j.properties,运行不了。


修改依赖文件:
<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 org.apache.log4j.FileAppender and the second one the org.apache.log4j.ConsoleAppender. The first appender has a fileattribute, where the logging file is set, whereas the second one has a target attribute which is set to System.out. Both appenders use a layout for their log files. The org.apache.log4j.PatternLayout is used and the ConversionPattern is set to%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n:

%d{dd/MMM/yyyy HH:mm:ss,SSS} is used to set the date pattern, %c{1} is used to print the class name, %m to print the message, and %n 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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值