关于这两者的组合应用带来的好处,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.out
log4j.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 message log4j.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.FileAppender
log4j.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.PatternLayout
log4j.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=INFO
log4j.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,添加到路径中,就解决问题了
但我这边是不能下这个包,加了会报错

所以。总的加那三个包就可以了

版权声明:本文为博主原创文章,未经博主允许不得转载。

slf4j+log4j的初次使用的更多相关文章

  1. slf4j log4j logback关系详解和相关用法

    slf4j log4j logback关系详解和相关用法 写java也有一段时间了,一直都有用slf4j log4j输出日志的习惯.但是始终都是抱着"拿来主义"的态度,复制粘贴下配 ...

  2. slf4j,log4j,logback 初步使用

    log4j,slf4j,logback简单介绍见 LogBack简易教程 Logback浅析 简单的将,slf4j是一个日志的框架,有各种日志的接口,但是并不包含实际的写日志的方法. log4j,lo ...

  3. slf4j+log4j在Java中实现日志记录

    小Alan今天来跟大家聊聊开发中既简单又常用但必不可少的一样东西,那是什么呢?那就是日志记录,日志输出,日志保存. 后面就统一用日志记录四个字来形容啦. 日志记录是项目的开发中必不可少的一个环节,特别 ...

  4. Spring中配置使用slf4j + log4j

    本人也是查看别人博客获得的方法,详细讲解请参照 https://www.cnblogs.com/yuxiaole/p/9297266.html 下面进入正题: 1.在pom.xml中注入相关的依赖 & ...

  5. 常见java日志系统的搭配详解:关于slf4j log4j log4j2 logback jul jcl commons-logging jdk-logging

    先看一张图: 是不是有点晕, 晕就对了.这个仅仅是 slf4j 的情况,实际上, 我们不仅要接触到 slf4j ,有时候还会接触其他的日志系统.且看下文分解. 1 直接使用各个日志系统 1.1 直接使 ...

  6. 在android中配置 slf4j + log4j 日志记录框架

    需求: 在项目开发中,需要记录 操作日志 .起初自己写了个简单的日志记录文本写入到文本的方法,后来随着项目的膨胀,需要考虑更多的操作,开始考虑性能问题. 实现: 考虑使用 slf4j + log4j ...

  7. slf4j + log4j 是如何初始化的

    SLF4J的全称是 Simple Logging Facade for Java(简单java日志门面) SLF4J自己不提供具体的日志功能实现,只是提供了一个统一的日志门面,在这个统一的门面之下,用 ...

  8. slf4j log4j logback相关用法

    Java的简单日志门面( Simple Logging Facade for Java SLF4J)作为一个简单的门面或抽象,用来服务于各种各样的日志框架,比如java.util.logging.lo ...

  9. slf4j log4j logback log4j2关系详解和相关用法

    来源:slf4j log4j logback关系详解和相关用法https://www.cnblogs.com/Sinte-Beuve/p/5758971.html The Simple Logging ...

随机推荐

  1. Source Insight 创建工程(linux-2.6.22.6内核源码)

    1. 软件设置 安装完Source Insight,需要对其进行设置添加对“.S”汇编文件的支持: 2. 新建linux-2.6.22.6工程 1)选择工程存放的路径: 2)下载linux-2.6.2 ...

  2. watir-webdriver 中根据html5中的data-*属性设置元素

    def jscript(key="",*hashdict) key_dict=hashdict[0].keys[0] value_dict=hashdict[0][key_dict ...

  3. OpenID Connect Core 1.0(一)介绍

    IdentityServer4是基于OpenID Connect and OAuth 2.0框架,OpenID Connect Core 1.0是IdentityServer4最重要的文档 By 道法 ...

  4. 浅析 golang interface 实现原理

    interface 在 golang 中是一个非常重要的特性.它相对于其它语言有很多优势: duck typing.大多数的静态语言需要显示的声明类型的继承关系.而 golang 通过 interfa ...

  5. JQuery第二天——JQuery的DOM操作

    JQuery拥有隐式迭代和显式迭代 因为JQuery为类数组对象,可以使用手动遍历实现显式 .each():也可以使用 $("p").click(function(){ var t ...

  6. 一个java程序员该具备的工具

    http://itindex.net/detail/37115-java-%E7%A8%8B%E5%BA%8F%E5%91%98-%E5%B7%A5%E5%85%B7 文章很详细.

  7. [WC2011]最大XOR和路径 线性基

    [WC2011]最大XOR和路径 LG传送门 需要充分发掘经过路径的性质:首先注意不一定是简单路径,但由于统计的是异或值,重复走是不会被统计到的,考虑对于任意一条从\(1\)到\(n\)的路径的有效部 ...

  8. 菜鸟vimer成长记——第2.1章、normal模式

    目的 掌握normal模式下常用操作的语法和概念,这些操作对应的应用场景以及实用技巧. 通过normal模式举一反三掌握cmd-line和visual的常用文本操作. 简介 文本操作的理想状态为:一个 ...

  9. 如何打war包和jar包

    1.jar包的导出 答:右键项目,export—jarfile  生成. 2.war包的导入 答:方法一:右键项目,export—WAR file 生成. 方法二:[ant文件]—[deploy-be ...

  10. vue 组件间的通信

    (1)props:用于父组件向子组件传递消息 使用方法: 在父组件中,使用子组件时,<Child v-bind:data="data"/>,通过v-bind把子组件需要 ...