码迷,mamicode.com
首页 > 其他好文 > 详细

日志组件系列:(4)SLF4J和log4j实战

时间:2016-08-04 06:51:13      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:java   logging   



参考:

Simple Logging Facade for Java (SLF4J)

http://www.slf4j.org/


SLF4J user manual

http://www.slf4j.org/manual.html



SLF4J warning or error messages and their meanings

http://www.slf4j.org/codes.html






(1)下载组件,添加jar包

(2)配置

(3)使用API


1、下载组件,添加jar包


组件名称jar包下载地址
slf4j-1.7.21.zipslf4j-api-1.7.21.jarhttp://www.slf4j.org/download.html
slf4j-log4j12-1.7.21.jar
log4j-1.2.17.ziplog4j-1.2.17.jarhttp://logging.apache.org/log4j/1.2/download.html


2、配置

在项目的src目录下添加log4j.properties

log4j.rootLogger=debug, console, file

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p %c.%M() -%m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File= ./logs/MyLog.log
log4j.appender.file.MaxFileSize=5KB
log4j.appender.file.MaxBackupIndex=100
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %p %c.%M() -%m%n


3、使用API

package com.rk.test;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
	@Test
	public void test()
	{
		Logger logger = LoggerFactory.getLogger(HelloWorld.class);
		logger.info("Hello World");
		logger.info("你好,{},欢迎来到{}", "小明","南极");
	}
}


4、常见的错误

一个常见的错误提示是:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

This warning is printed because no slf4j binding could be found on your class path.

The warning will disappear as soon as you add a binding to your class path. Assuming you add slf4j-log4j12-1.7.21.jar so that your class path contains:

  • slf4j-api-1.7.21.jar

  • slf4j-log4j12-1.7.21.jar

这个警告提示在classpath中没有找到slf4j binding。这里所说的slf4j binding是指slf4j-log4j12-1.7.21.jar


SLF4J supports various logging frameworks. The SLF4J distribution ships with several jar files referred to as "SLF4J bindings", with each binding corresponding to a supported framework.


  • slf4j-log4j12-1.7.21.jar

  • Binding for log4j version 1.2, a widely used logging framework. You also need to place log4j.jar on your class path.





SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

查看地址:http://www.slf4j.org/codes.html#StaticLoggerBinder


Failed to load class org.slf4j.impl.StaticLoggerBinder

This warning message is reported when the org.slf4j.impl.StaticLoggerBinder class could not be loaded into memory. This happens when no appropriate SLF4J binding could be found on the class path. Placing one (and only one) of slf4j-nop.jarslf4j-simple.jarslf4j-log4j12.jarslf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.





日志组件系列:(4)SLF4J和log4j实战

标签:java   logging   

原文地址:http://lsieun.blog.51cto.com/9210464/1834166

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!