首页 > 代码库 > slf4j的使用
slf4j的使用
1.导入jar包
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.生成配置文件 src\main\resources\log4j.propertes
log4j.rootLogger=info,ROLLING_FILE
log4j.appender.ROLLING_FILE = org.apache.log4j.ConsoleAppender
log4j.appender.ROLLING_FILE.encoding=utf-8 (* utf-8后面不能有空格,utf为小写)
log4j.appender.ROLLING_FILE.File = stock.log
log4j.appender.ROLLING_FILE.Append = true
log4j.appender.ROLLING_FILE.MaxFileSize= 20MB
log4j.appender.ROLLING_FILE.MaxBackupIndex= 2
log4j.appender.ROLLING_FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[log] %d - %c - %-4r [%t] %-5p %c %x - %m%n
补充:文件格式为utf-8 (idea右下角显示)
File ->settings... -> File Encoding ->IDE Encoding (UTF-8) Project Encoding (UTF-8)
File -> other settings -> default Encoding -> File Encoding ->IDE Encoding (UTF-8) Project Encoding (UTF-8)
3.在类中使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private Logger logger = LoggerFactory.getLogger(xxx.class);
logger.info("write log message")
logger.info(e.getMessage());
slf4j的使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。