How to setup SLF4J and LOGBack in a web app - fast--转载

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

时间: 2024-10-13 16:16:51

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

java日志之slf4j与logback简单使用

最近在开发遇到日志是使用slf4j与logback.xml的配置,所以就记录下来了. 1.导入这几个jar包: Logback 分为三个模块:logback-core,logback-classic,logback-access logback-core 是核心: logback-classic 改善了 log4j,且自身实现了 SLF4J API,所以即使用 Logback 你仍然可以使用其他的日志实现,如原始的 Log4J,java.util.logging 等: logback-acces

Java日志:集成slf4j和logback

Java日志方案有很多,包括:java.util.logging.Apache的commons-logging和log4j.slf4j以及logback. 一个大型项目会用到众多第三方jar包,这些jar包可能会用到上述各种日志方案,如何在新的项目中使用slf4j+logback的组合,让所有其他jar包的日志也输出到logback,并避免冲突和异常? SLF4J is a simple facade for logging systems allowing the end-user to pl

SLF4J和Logback日志框架详解

SLF4J和Logback日志框架详解 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 本文讲述SLF4J和Logback日志框架.   SLF4J是一套简单的日志外观模式的Java API,帮助在项目部署时对接各种日志实现. LogBack在运行时使用JMX帮助修改日志配置,在生产状态下无需重启应用程序. SLF4J SLF4J是简单的日志外观模式框架,抽象了各种日志框架例如Logback.Log4j.Commons-logging和JDK自带的l

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

一.maven引用. 传送门:http://www.slf4j.org/manual.html#projectDep <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.13</version> </dependency> 二.日志文件配置. 非spring的se

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

如果对于commons-loging .log4j .slf4j .LogBack 等都已经非常清楚了,可以忽略本文.几次解决日志冲突问题时对这几个概念的简单总结,希望对这块基础没有理解透的同学能有所帮助,当然如果对这块有更深刻理解的同学,也贡献出自己的知识和见解. 一.     概念 Commons-logging : apache最早提供的日志的门面接口.避免和具体的日志方案直接耦合.类似于JDBC 的api 接口,具体的的JDBC driver 实现由各数据库提供商实现.通过统一接口解耦,

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

.简介 java里面日志分为两部分一个门面.一个实现,我们所熟知的SLF4j.Log4j.Log4j2.Logback的日志组件slf4j是门面提供的统一的入口,具体实现由log4j.log4j2.logback来实现 log4j由于太老作者自己也不打算重构了所以放弃 log4j2是apach的一个项目很好,但支持上面略有欠缺所以放弃 logback是之前log4j的作者自己开源的一个新的log组件,做了大量的调整及优化,性能及使用性上都有很大的提高,再加上沉淀了很多年又完全实现了slf4j,所

日志之slf4j和logback日志系统(二)

这篇文章我们讲一下,如何使用slf4j和logback组合来搭建一套日志系统. 介绍 如果我们的系统十个新系统,也就是之前没有引入其他的日志工具,那么只需要引入 依赖引入 logback配置文件 业务中使用 学习链接 slf4j源码剖析 原文地址:https://www.cnblogs.com/htyj/p/12023541.html

使用SLF4J和LOGBACK (一 : 基本使用)

1.SLF4J是什么? slf4j是一个日志门面,它不是具体的日志实现框架,而是提供了通用的日志接口,按个人理解来说,是通过接口实现多态,来满足应用在不同日志框架间切换的需求. 例如在程序中我们需要记录日志,使用SLF4J提供的接口来调用: Logger logger = LoggerFactory.getLogger(Class.class); logger.info(); 上边两行代码中的Logger和LoggerFactory对象均来自SLF4J包中. 具体的日志实现框架,我们可以选择LO

slf4j与logback的结合使用

参考:http://my.oschina.net/ydsakyclguozi/blog/412240 一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access.logback-core是其它两个模块的基础模块.logback-classic是log4j的一个 改良版本.此外logback-classic完整实现SLF4J API使你可以很方便地更换