Skip to content

Commit ef00c97

Browse files
committed
slf4j使用示例
1 parent 9cb2075 commit ef00c97

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
在这里,通过文章和代码,把这些知识点和技术的主要内容记录并汇总,供自己快速回顾,也分享给他人。
55

6+
注:部分例子基于JDK8。
7+
68
## 关键字
79
* [transient与序列化](src/cn/aofeng/demo/java/lang/serialization/TransientDemo.java)
810

@@ -107,6 +109,10 @@
107109
* [HMAC-SHA1签名算法](src/cn/aofeng/demo/encrypt/HmacSha1.java)
108110

109111
## 开源组件
112+
113+
### Slf4j
114+
* [slf4j使用示例](src/cn/aofeng/demo/slf4j/HelloSlf4j.java)
115+
110116
### Redis
111117
* [Redis客户端Jedis使用示例](src/cn/aofeng/demo/redis/JedisDemo.java)
112118

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.aofeng.demo.slf4j;
2+
3+
import java.io.IOException;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
/**
9+
* slf4j使用示例。
10+
*
11+
* @author <a href="mailto:[email protected]">聂勇</a>
12+
*/
13+
public class HelloSlf4j {
14+
15+
private static Logger _logger = LoggerFactory.getLogger(HelloSlf4j.class);
16+
17+
public static void main(String[] args) {
18+
// 直接输出字符串
19+
_logger.info("Hello, slf4j logger.");
20+
21+
// 输出格式字符串(携带多个填充参数)
22+
_logger.info("{} + {} = {}", 1, 2, (1+2));
23+
24+
// 输出错误信息和异常堆栈
25+
_logger.error("错误信息", new IOException("测试抛出IO异常信息"));
26+
27+
// 输出错误信息(携带多个填充参数)和异常堆栈
28+
_logger.error("两个参数。agrs1:{};agrs2:{}的info级别日志", "args1", "args2", new IOException("测试抛出IO异常信息"));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)