slf4j+log4j的初次使用
来源:互联网 发布:软件行业研究报告 编辑:程序博客网 时间:2024/06/06 01:14
关于这两者的组合应用带来的好处,google都有 就不说了。
首先说下配置,
工作笔记:在myeclipse 中创建一个java project
创建一个 TestSlf4J 类
package com.joyce.quartz.main;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class TestSlf4J { private static final Logger logger =LoggerFactory.getLogger(TestSlf4J.class); public static void main(String[] args) { logger.error("Hello world {}"); System.out.println("2323");try {int i = 1/0;} catch (Exception e) {logger.error("出现异常",e);}} }
同时把slf4j-api-1.1.jar slf4j-log4j12-1.6.1.jar log4j-1.2.16.jar 放入项目里。
再添加一个log4j.properties
# An example log4j configuration file that outputs both to System.out# and a file named 'test'.# For the general syntax of property based configuration files see the# documenation of org.apache.log4j.PropertyConfigurator.# WARNING: Location information can be useful but is very costly in# terms of computation.# The root logger uses the appender called A1. # The root logger uses the appenders called A1 and A2. Since no level# is specified, note the empty string between the comma (",") and the# equals sign ("="), the level of the root logger remains# untouched. Log4j always initializes the level for the root logger to# DEBUG. The root logger is the only logger that has a default# level. Bu default, all other loggers do not have an assigned level,# such that they inherit their level instead.log4j.rootLogger=, A1, A2# A1 is set to be ConsoleAppender sending its output to System.outlog4j.appender.A1=org.apache.log4j.ConsoleAppender# A1 uses PatternLayout.log4j.appender.A1.layout=org.apache.log4j.PatternLayout# The conversion pattern consists of date in ISO8601 format, level,# thread name, logger name truncated to its rightmost two components# and left justified to 17 characters, location information consisting# of file name (padded to 13 characters) and line number, nested# diagnostic context, the and the application supplied messagelog4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n# Appender A2 writes to the file "test".log4j.appender.A2=org.apache.log4j.FileAppenderlog4j.appender.A2.File=e:/test.log# Truncate 'test' if it aleady exists.log4j.appender.A2.Append=false# Appender A2 uses the PatternLayout.log4j.appender.A2.layout=org.apache.log4j.PatternLayoutlog4j.appender.A2.layout.ConversionPattern=%-5r %-5p [%t] %c{2} %d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p]%m%n# In this example, we are not interested in INNER loop or SWAP# messages. You might try to set INNER and SWAP to DEBUG for more# verbose output.log4j.logger.org.apache.log4j.examples.SortAlgo.INNER=INFOlog4j.logger.org.apache.log4j.examples.SortAlgo.SWAP=INFO
项目结构如图:
最后 运行TestSlf4J类在e盘就生成了一个test.log文件 生成内容如下:
注意:
slf4j-log4j12-1.6.1.jar是连接包,我们在java里直接用slf4j创建的log,如果没有这个连接包,是创建不出log的
还要注意的是,在没加这个连接包的时候,
会报
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”
有解决办法是说:下载slf4j-nop.jar,添加到路径中,就解决问题了
但我这边是不能下这个包,加了会报错
所以。总的加那三个包就可以了
0 0
- slf4j+log4j的初次使用
- slf4j、log4j 的使用
- slf4j、log4j 的使用
- log4j和slf4j的使用
- SLF4J和log4j的使用
- SLF4J和log4j的使用
- SLF4J和log4j的使用
- slf4j和log4j的使用
- Log4j 2 + Slf4j 的配置和使用
- slf4j与log4j的配置和使用
- Log4j 2 + Slf4j 的配置和使用
- Log4j 和 Slf4j 的介绍和使用
- SLF4J和log4j的整合使用
- log4j与log4j2的配合使用、slf4j
- Log4j 2 + Slf4j 的配置和使用
- Log4j 2 + Slf4j 的配置和使用
- slf4j的使用 log4j实现方式
- Log4j 2 + Slf4j 的配置和使用
- CentOS上使用OpenStack的一些问题
- POJ 1241
- Java Reflection Tutorial for Classes, Methods, Fields, Constructors, Annotations and much more
- stochastic regression
- 从服务器获取时间的年月日三级联动
- slf4j+log4j的初次使用
- 条款 22: 尽量用“传引用”而不用“传值”
- 多线程 -- 信号量和互斥量
- 一个简单Net 类似ORM工具
- 天龙八部回归记
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
- ORACLE 用户密码过期
- spring Ioc 的学习
- ANROID SDK 搭建问题小结