原文:https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast

Logback is intended as a successor to the popular log4j project. It was designed, in addition to many individual contributors, by Ceki Gülcü, the founder of log4j. It builds upon experience gained in building industrial-strength logging systems going back as far as 1999. Logback-classic natively implements the SLF4J API so that you can readily switch back and forth between logback and other logging frameworks such as log4j or java.util.logging (JUL).

The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.

If your working with a Maven web-app project, this procedure will get you setup to log with LOGBack through SLF4J super fast.

Step 0 - Add LOGBack dependency libraries.

If you are using Maven skip this step.

Import the following libraries to your WEB-INF/lib folder:

  • WEB-INF

    • lib

      • logback-classic.x.x.x.jar
      • logback-core.x.x.x.jar
      • slf4j-api-x.x.x.jar

Step 1 - Add LOGBack dependency to your Maven POM

Declare the following dependency in your Maven 2 pom.xml and Maven will grab the appropriate libraries for you during the build.

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.0.13</version>
</dependency>

Step 2 - Import existing (starter) XML configuration files

You will likely want to start with a base configuration file that you can build upon. In Maven you can have a logging configuration for your main source and another for your testing. You can download starter configuration files for your project by clicking the links in the hierarchy below. Put them in your project according to the position indicated by the hierarchy shown.

Step 3 - Customize the XML configuration just enough to test

Open up the logback.xml file. If you used the starter provided in the link above, you'll find the following:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
    </layout>
  </appender>
   
  <logger name="com.base22" level="TRACE"/>
   
 
  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

You will notice that one logger is defined at a package level ("com.base22"). You can simply change that to match your application's package base. You can also declare additional loggers (packages and/or classes) if desired.

Step 4 - Put logging code in your classes

The last thing you need to do is drop some logging code in a class and test this whole setup.

Add the following to the imports section of your java code:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Add the following at the top of your class in the global section (just under the line that declares your class public class Whatever extends Whatever). Change the name of the class (MyClassName) in the getLogger method call, of course. Name it the same as the class you're dropping this code into.

static final Logger LOG = LoggerFactory.getLogger(MyClassName.class);

Throw some logging statements in your code somewhere where you know they'll be fired right away when you run your app. For example:

LOG.trace("Hello World!");
LOG.debug("How are you today?");
LOG.info("I am fine.");
LOG.warn("I love programming.");
LOG.error("I am programming.");

Alternatively, you can just download this simple console test app and run it as a Java app from the command line or from within your IDE:

SLF4JConsoleTest.java

This class has a main method so it runs as a Java app and it will log one statement at each level.

Step 5 - Run your app and make sure it works

Finally, run your app and make sure it works. You should see log lines in your console. If it doesn't work, just review these steps a little more carefully and fiddle with it.

How to setup SLF4J and LOGBack in a web app - fast--转载的更多相关文章

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

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

  2. lombok+slf4j+logback SLF4J和Logback日志框架详解

    maven 包依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...

  3. slf4j,log4j,logback 初步使用

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

  4. 跨过slf4j和logback,直接晋级log4j 2

    今年一直关注log4j 2,但至今还没有出正式版.等不及了,今天正式向大家介绍一下log4j的升级框架,log4j 2. log4j,相信大家都熟悉,至今对java影响最大的logging系统,至今仍 ...

  5. java日志,(commons-loging 、log4j 、slf4j 、LogBack介绍)

    如果对于commons-loging .log4j .slf4j .LogBack 等都已经非常清楚了,可以忽略本文.几次解决日志冲突问题时对这几个概念的简单总结,希望对这块基础没有理解透的同学能有所 ...

  6. slf4j 之logback日志之环境安装【一】

    一.maven引用. 传送门:http://www.slf4j.org/manual.html#projectDep <dependency> <groupId>ch.qos. ...

  7. IDEA项目搭建十——使用slf4j和logback进行日志记录

    .简介 java里面日志分为两部分一个门面.一个实现,我们所熟知的SLF4j.Log4j.Log4j2.Logback的日志组件slf4j是门面提供的统一的入口,具体实现由log4j.log4j2.l ...

  8. Java日志框架(Commons-logging,SLF4j,Log4j,Logback)

    简介 在系统开发中,日志是很重要的一个环节,日志写得好对于我们开发调试,线上问题追踪等都有很大的帮助.但记日志并不是简单的输出信息,需要考虑很多问题,比如日志输出的速度,日志输出对于系统内存,CPU的 ...

  9. java日志之slf4j与logback简单使用

    最近在开发遇到日志是使用slf4j与logback.xml的配置,所以就记录下来了. 1.导入这几个jar包: Logback 分为三个模块:logback-core,logback-classic, ...

随机推荐

  1. EF Code First学习笔记:数据库创建

    控制数据库的位置 默认情况下,数据库是创建在localhost\SQLEXPRESS服务器上,并且默认的数据库名为命名空间+context类名,例如我们前面的BreakAway.BreakAwayCo ...

  2. Asp.net MVC 4 异步方法

    今天我们来看一下,同样功能在 Asp.net MVC 4 下的实现,基于.net framework 4.5 下的async支持,让我们的代码更加简单,看下面片断代码名叫Index的Action方法: ...

  3. java jvm学习笔记九(策略文件)

    欢迎装载请说明出处:http://blog.csdn.net/yfqnihao/article/details/8271407 课程源码:http://download.csdn.net/detail ...

  4. position属性

    所有主流浏览器支持position属性: 任何版本的ie浏览器都不支持属性值“inherit”. position属性规定元素的定位类型,任何元素都可以定位,不过绝对定位或固定元素会生成一个块级框,不 ...

  5. 查看本机IP地址及子网掩码(netmask)

    通常有两种方式来查看本机IP地址及子网掩码,一种是在网路和共享中心中查看网路信息:一种是在cmd命令窗口查看. 下面分别介绍这两种方法: 1.在网路和共享中心中查看本机IP地址及子网掩码 (1)打开控 ...

  6. 4.1Reduction模型

    1. Reduction Reduction是一种广泛使用的计算模型,特别是在并行计算领域.简单地来说,Reduction就是一系列的划分(Partition)和汇总(Summarize)操作的集合: ...

  7. ACM2040

    关于亲和数的详细解释如下: http://www.kepu.net.cn/gb/basic/szsx/8/8_83/8_83_1004.htm /* 亲和数 时间限制:2000/1000 MS(JAV ...

  8. mfc的一个串口类

    这几天一直再看串口相关知识,对于其总结为如下串口类: 头文件声明如下: #pragma once // 声明当串口接收到线程的时候调用的函数指针 // 参数: 1,接收到的数据: 2,数据长度: 3, ...

  9. java 读取文件到String(解决中文乱码)

    在改写V&View(维视)时用到了文件管理,需要从html文档读取字符串,可是一直出现中文乱码,一直解决不了.而且很是意外,我在本地运行代码时就能正常读取中文,当放到tomcat上时全是乱码, ...

  10. cocos2d-x 3.0版本已经加了socket部分

    cocos2d-x开发者的福音到了,在3.0版本中新增了HttpClient.HttpRequest.SocketIO.Websocket库,需要网络交互的同学应该可以用到,并且它已经与cocos2d ...