Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ SLF4J Tutorial     - Oracle how to maintain the consistency of read? (Database)

- Linux package management operations Basic entry (Linux)

- Computer security protection remove local and remote system log files (Linux)

- Linux into single user mode to modify the administrator password (Linux)

- Seven kinds of DDoS attack methods and techniques Defensive (Linux)

- Hibernate Search and Lucene quick introduction (Linux)

- Use MD5 transform algorithm to prevent exhaustive decipher passwords (Linux)

- OpenWRT environment to build (Linux)

- Java multi-threaded in a three way (inheritance, implementation, anonymous inner classes) (Programming)

- Use JMS listener Oracle AQ, trigger the execution of Java programs in the database changes (Database)

- HBase table data processing tab (Database)

- Memcached distributed caching (Server)

- Linux start the process (Linux)

- JQuery implements the same content merge cells (Programming)

- Java Collections Framework interfaces map (Programming)

- Ubuntu will be written in a command file, executable file, source command (Linux)

- How to extend / remove swap partitions (Linux)

- Ubuntu Learning Advanced article - to teach you to further enhance system security (Linux)

- Fedora 8 configured Heartbeat (Linux)

- How to create a new file system / partitions under Linux terminal (Linux)

 
         
  SLF4J Tutorial
     
  Add Date : 2017-08-03      
         
       
         
  SLF4J just a facade (facet), it does not contain specific implementation, but some log4j, java.logging packaging, etc. to achieve a unified interface.

Interface commons-logging and slf4j are logs for users to use, and does not provide true!

Truly log4j, logback and so is the log.

When we call interface, the interface will automatically find the factory proper implementation, returns an instance of an implementation of service to me. These processes are transparent, users do not need to do anything!

There is a small story, then convince Apache log4j and other logs to prepared in accordance with commons-logging standard, but due to the commons-logging class loading a problem, not to implement a friendly, so log4j's authors wrote slf4j, also So with the two world commons-logging. As for the use of which in the end, to the user to decide.

Thus, slf4j emerged, through the simple implementation to find classes in line with their own interfaces to achieve, if not meet their standards, log, you can achieve some intermediate such as the above slf4j-log4j12.jar to be adapted.

Well, Closer to home, how to use slf4j?

First, use the Quick Start slf4j

First, the project must be included slf4j-api.jar, moreover, should include specific adapter slf4j implementation provides (such as slf4j-log4j12.jar), as well as the concrete realization of the jar package (such as log4j-1. ** .jar).

We have the following code as an example:

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

public class Slf4jDemo {
    private static final Logger LOG = LoggerFactory.getLogger (Slf4jDemo.class);

    public static void main (String [] args) {
        LOG.error ( "Error Message!");
        LOG.warn ( "Warn Message!");
        LOG.info ( "Info Message!");
        LOG.debug ( "Debug Message!");
        LOG.trace ( "Trace Message!");
    }
}
Depending on the specific implementation used, the log output have different results. This reaction by using slf4j, so it can easily replace the log system.

(A) Slf4j-simple

slf4j comes with a simple implementation can be used for small projects, but can not configure the log level and the like.

The official document is described as: Binding for Simple implementation, which outputs all events to System.err Only messages of level INFO and higher are printed This binding may be useful in the context of small applications...

Join slf4j-1.6.6.jar and slf4j-simple-1.6.6.jar in build_path project.

The output is as follows:

2 [main] ERROR Slf4jDemo - Error Message!
2 [main] WARN Slf4jDemo - Warn Message!
2 [main] INFO Slf4jDemo - Info Message!
(B) slf4j-jdk
Use jkd own logging system, added slf4j-1.6.6.jar and slf4j-jdk14-1.6.6.jar in build_path project.
The output is as follows:
February 16, 2015 11:09:36 PM Slf4jDemo main
Serious: Error Message!
February 16, 2015 11:09:36 PM Slf4jDemo main
Warning: Warn Message!
February 16, 2015 11:09:36 PM Slf4jDemo main
Information: Info Message!

(C) slf4j-log4j
log4j is currently the most used log system, it is more suitable for large-scale projects.
Join slf4j-1.6.6.jar and slf4j-log4j-1.6.6.jar, and implementation log4j in build_path projects, such as log4j-1.2.16.jar.

The output is as follows:

log4j: WARN No appenders could be found for logger (Slf4jDemo).
log4j: WARN Please initialize the log4j system properly.
log4j: WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
You can configure output log level configuration file.

Second, some considerations

1, note build_path can not have more than one log to achieve, otherwise it will lead to the realization slf4j do not know which to use, and thus the following error

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar: file: /Users/liaoliuqing/99_Project/1_myCodes/5_JavaEEDemo/lib/slf4j-log4j12-1.6.6.jar /org/slf4j/impl/StaticLoggerBinder.class!]
SLF4J: Found binding in [jar: file: /Users/liaoliuqing/99_Project/1_myCodes/5_JavaEEDemo/lib/slf4j-jdk14-1.6.6.jar /org/slf4j/impl/StaticLoggerBinder.class!]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j: WARN No appenders could be found for logger (Slf4jDemo).
log4j: WARN Please initialize the log4j system properly.
log4j: WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
     
         
       
         
  More:      
 
- How to Install Telegram instant messaging software on Ubuntu (Linux)
- Advanced Linux security settings (Linux)
- Linux upgrade Glibc (Linux)
- 7 JavaScript interview questions (Programming)
- Java List add duplicate the same object (Programming)
- Ubuntu 14.04 LTS NTFS partition can not access solution (Linux)
- Actual SSH port forwarding (Linux)
- Java NIO in the world (Programming)
- Examples of testing and installation Mesos on CentOS (Linux)
- Definition Format Oracle basis of various statements (Database)
- Android code performance optimization tips (Programming)
- Thunderbird 24.4.2 has been released for use with PPA updates (Linux)
- Node.js developers must know four JavaScript concepts (Programming)
- Binary search is really easy as you think you do (Programming)
- Bootable ISO image using GRUB (Linux)
- Laravel cache paged results (Server)
- Supervisor Installation and Configuration (Server)
- Ubuntu install Vendetta Online 14.04 (Linux)
- MongoDB 2.6 deployment replica set + partitions (Database)
- Taught you how to build your own VPS server (Server)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.