关于这两者的组合应用带来的好处,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. MAC上安装GCC失败

    问题 在用brew安装GCC时, 报了如下错误. ➜ ~ brew install gcc ==> Installing dependencies for gcc: isl, mpfr and ...

  2. windows下beautifulsoup使用lxml解析使用报错

    s4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need ...

  3. PostgreSQL的psql客户端各种连接错误总结

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@g ...

  4. SpringMVC 异常信息ASM ClassReader failed to parse class file的问题解决

    1.  环境信息: Spring 3.2.0,  JDK 1.8.0 2.  运行简单的程序,出现以下错误信息: 2.  运行简单的程序,出现以下错误信息: Caused by: org.spring ...

  5. [Selenium]如何通过Selenium实现Ctrl+click,即按住Ctrl的同时进行单击操作

    [以下是不负责任的转载……] 在自动化测试的过程中,经常会出现这样的场景: 按住Ctrl的同时,进行单击操作,已达到多选的目的 Actions a = new Actions(driver); a.k ...

  6. 手动搭建openstack的痛苦经历

    openstack真的是一个十分痛苦的东西,好在有自动部署工具,虽然有自动部署工具可以方便我们部署使用,但是学习的话,第一次最好手动部署,因为手动部署更能我们了解openstack的工作流程和各组建之 ...

  7. 电商打折套路分析 —— Python数据分析练习

    电商打折套路分析 ——2016天猫双十一美妆数据分析 数据简介 此次分析的数据来自于城市数据团对2016年双11天猫数据的采集和整理,原始数据为.xlsx格式 包括update_time/id/tit ...

  8. Qt-网易云音乐界面实现-5 收藏列表,播放列表实现 QListWidget QTableWidget

    先上目前完成的内容吧,发现后面越写越多.在看看点击量,心凉凉. 完成了左侧的导航列表,还有就是右下角的播放列表. //创建的歌单 my_Create_Music_List = new QListWid ...

  9. myBatis逆向工程的使用

    使用myBatis Generator可以快速生成实体类.dao类和mapper文件.有两种方式,现在说的是比较灵活的方式.本文栗子使用的是IDEA,目录结构为maven项目的结构. 1.在pom.x ...

  10. windows上的mysql配置过程

    个人电脑的mysql配置,记录下来留作备忘 1. 首先去官网下载最新的mysql安装包,我下的是5.7.25,地址是 https://dev.mysql.com/downloads/windows/ ...