使用slf4j 实现日志系统

##SLF4J程序员

背景

项目里面打印log,
* 用System.out.println,属于2B程序员。
* 用Log4j,logger.info(...), logger.errr(...),属于普通程序员
* 用SLF4J , 说明你已经注意细节,向文艺程序猿发展了。

为何要使用SLF4J

为何这么说呢,
    “a” + "b" + "c" ...这样的字符串拼接,会在常量池生成大量字符,效率低下。
咱们都知道避免这样的操做,须要使用StringBuffer,StringBuilder。

开发中咱们的log打印都会碰到logger.info("current time is " + currentTime);
这样的状况。难倒都要改为StringBuffer,StringBuilder吗?

NO,SLf4J就能够解决这个问题。

如何引入

在maven项目中添加如下依赖
		<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-api</artifactId>
		<version>1.7.6</version>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>1.7.6</version>
	</dependency>
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
	</dependency>

###如何使用api

Logger logger = LoggerFactory.getLogger(xxx.class);

logger.info("current time is {} " , currentTime);

SLF4J会用currentTime变量替代 前面的{},你能够添加多个,顺序对应就能够了
相关文章
相关标签/搜索