原文: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.

  • src

    • main

      • resources

        • logback.xml
    • test
      • resources

        • logback-test.xml

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.

转载于:https://www.cnblogs.com/davidwang456/p/4344758.html

How to setup SLF4J and LOGBack in a web app - fast--转载相关推荐

  1. SLF4J 之logback.xml配置文件实例及其说明

    为什么80%的码农都做不了架构师?>>>    对于java后端程序员来说,如何记录日志是一个小话题,只要在网上随便找一个log4j的简单说明,就可以用了.但是,要真正了解日志记录的 ...

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

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

  3. SpringMVC学习(三)——SpringMVC+Slf4j+Log4j+Logback日志集成实战分享

    文章目录 1.概述 1.1 说明 1.2 日志体系 1.2.1 JCL日志面门介绍 1.2.2 Slf4j日志面门介绍 2.几种日志系统介绍: 2.1 Slf4j 2.2 Commons-loggin ...

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

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

  5. 结合使用slf4j和Logback教程

    在当前文章中,我将向您展示如何配置您的应用程序以使用slf4j和logback作为记录器解决方案. Java简单日志记录外观(slf4j)是各种日志记录框架的简单外观,例如JDK日志记录(java.u ...

  6. SLF4J with Logback in a Maven Project | Mograblog

    SLF4J with Logback in a Maven Project | Mograblog

  7. SLF4J和Logback日志框架详解

    SLF4J和Logback日志框架详解 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 本文讲述SLF4J和Logback日志框架.    SLF4J是一 ...

  8. 【Java从0到架构师】日志处理 - SLF4J、Logback、Log4j 2.x

    日志处理 - SLF4J.Logback.Log4j 2.x SLF4J + Log4j 1.x SLF4J + Logback Logback - 配置文件 Logback - 控制台彩色打印 Lo ...

  9. slf4j log4j logback关系详解和相关用法 【by Sinte-Beuve】

    slf4j log4j logback关系详解和相关用法 slf4j log4j logback的关系 The Simple Logging Facade for Java是什么? log4j和log ...

最新文章

  1. Python入门100题 | 第030题
  2. BurpSuite技巧之二重代理
  3. ftp 客户端 使用http代理 源码_代理服务器连接HTTPS过程
  4. SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
  5. 接口管理工具 - 资源篇
  6. 转自云界漫步:同步容灾100公里的限制来自哪里
  7. AcWing 1057. 股票买卖 IV
  8. 手工制作机器人用彩泥_印度神奇芒果干制作过程,看一遍顶三遍,游客:不会再吃了...
  9. VS2013中安装配置和使用Boost库
  10. 将pandas中object类型转换为int类型
  11. Phaser帧动画没有效果
  12. 计算机与手机联网,手机与电脑无线连接怎么实现
  13. 你今天Git了吗?上传资源上Github最新教程!
  14. go语言webSocket框架——gorilla
  15. python3斐波纳契数列
  16. 钟汉良日记:凡夫俗子一定要眼见为实,菩萨才能见因知果!
  17. Vue动态组件、组件缓存、组件激活和非激活、组件插槽、组件name
  18. mysql usleep_PHP 暂停函数 sleep() 与 usleep() 的区别
  19. Android实训-家庭财务管理系统
  20. 什么是多态?为什么使用多态?

热门文章

  1. 在java中如何实现声音,我如何在Java中播放声音?
  2. php读取usb设备信息,急,请问如何获取USB设备的路径,非HID类型
  3. DynamicArray
  4. C++中逗号操作符的重载
  5. 上级对下级用通知合适吗_用报纸练书法,真的合适吗吗?
  6. java八种语言_Java语言八种基本类型
  7. C语言指针是什么?1分钟彻底理解C语言指针的概念
  8. malloc 就是返回开辟内存空间的首地址
  9. tensorflow加载模型
  10. io密集型和cpu密集型java,如何设计CPU密集型与I/O密集型程序