`
wwwzhouhui
  • 浏览: 362103 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

slf4j+logback 于log4j+commons-logging大PK

阅读更多
离开下班还有点时间,正好整理出2个日志系统对比发现,slf4j+logback 组合果然比log4j+commons-logging 高不少。少说废话看测试代码
1.测试类和方法选择同一个函数,大概是10000条记录放入list中,然后取出,分别用2个日志做日志记录 对比运行时间
测试代码
TestLinkList.JAVA
package com.testLog;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

/***********************************************************************  
*  
*   TestLinkList.java    
*   @copyright       Copyright:   2009-2012    
*   @creator         周辉<br/>  
*   @create-time   May 20, 2009   4:30:14 PM  
*   @revision         $Id:     *  
***********************************************************************/
public class TestLinkList {
private static final Log log = LogFactory.getLog(TestLinkList.class);
//private static final Logger log = LoggerFactory.getLogger(TestLinkList.class);
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
long startTime=System.currentTimeMillis();
//List<StudentBean> list =getList();
List<StudentBean> list =getLinkList();
getListBean2(list);
long end=System.currentTimeMillis();
log.info("list:::;"+list.size());
log.info("time:::;"+String.valueOf(end-startTime));
}


public static List<StudentBean> getList(){
List<StudentBean> list= new ArrayList<StudentBean>();

    for (int i=0;i<10000;i++){
    StudentBean st= new StudentBean();
    st.setId(i);
    st.setPassword("123456"+i);
    st.setUrl("http://127.0.0.1/");
    st.setUsername("zhouhui"+i);
    list.add(st);
    }
    return list;
}

public static List<StudentBean> getLinkList(){
List<StudentBean> list= new LinkedList<StudentBean>();
    for (int i=0;i<10000;i++){
    StudentBean st= new StudentBean();
    st.setId(i);
    st.setPassword("123456"+i);
    st.setUrl("http://127.0.0.1/");
    st.setUsername("zhouhui"+i);
    list.add(st);
    }
    return list;
}

public static void getListBean(List<StudentBean> list){
for (int i=0;i<list.size();i++){
StudentBean studentBean=(StudentBean)list.get(i);
log.info("username:::::::"+studentBean.getUsername());
}
}

public static void getListBean2(List<StudentBean> list){
  for (StudentBean studentBean: list){
  log.info("username:::::::"+studentBean.getUsername());
}
}
   
public static void get(){
long startTime=System.currentTimeMillis();
//List<StudentBean> list =getList();
List<StudentBean> list =getLinkList();
getListBean2(list);
long end=System.currentTimeMillis();
log.info("list:::;"+list.size());
log.info("time:::;"+String.valueOf(end-startTime));
}
}
先用LOG4J+commons-logging 测试
运行结果 使用了1891毫秒
2009-05-20 16:53:33,953 [com.testLog.TestLinkList]-[INFO] list:::;10000
2009-05-20 16:53:33,953 [com.testLog.TestLinkList]-[INFO] time:::;1891

然后修改代码用slf4j+logback 测试
注释代码private static final Log log = LogFactory.getLog(TestLinkList.class);
使用private static final Logger log = LoggerFactory.getLogger(TestLinkList.class);

这里测试用WEB JSP 调用该类
JSP 代码 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <% com.testLog.TestLinkList test = new com.testLog.TestLinkList();
    test.get();
    %>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
 
  <body>
    This is my JSP page. <br>
  </body>
</html>

tomcat 发布程序 运行
http://localhost:8081/testLog/index.jsp
控制台打出运行时间
2009-05-20 04:59:50:234 INFO  c.t.TestLinkList - list:::;10000
2009-05-20 04:59:50:234 INFO  c.t.TestLinkList - time:::;641
运行时间 641毫秒
2个对比看出 slf4j+logback 性能是比log4j+commons-logging 组合高
详细代码见附件
分享到:
评论

相关推荐

    slf4j+logback日志系统介绍

    - **log4j-over-slf4j.jar**:用于替换Log4J,需要注意的是,原有的`log4j.properties`配置文件将不再有效,需要转换为`logback.xml`。 - **jul-to-slf4j.jar**:用于替换JDK自带的日志系统,需要在程序启动时调用`...

    slf4j+logback快速上手教程

    - `jcl-over-slf4j.jar`:用于替换Apache Commons Logging。 - `log4j-over-slf4j.jar`:用于替换Log4j。 - `jul-to-slf4j.jar`:用于替换JDK自带的日志系统。 #### 二、Logback 介绍 **Logback**是一个开源日志...

    slf4j与commons-logging处理日志

    SLF4J(Simple Logging Facade for Java)和Apache Commons Logging是两个在Java开发中广泛使用的日志框架。它们提供了一种抽象层,允许开发者在不修改代码的情况下切换不同的日志实现,如Log4j、Java Util Logging ...

    commons-loggin 与 slf4j的桥接器

    1. **移除或替换旧的日志实现**:如果项目中已经存在commons-logging的实现,如log4j,需要将其移除或者替换为SLF4J的绑定。 2. **添加桥接器**:引入`slf4j-jcl`(Jakarta Commons Logging桥接器)的jar文件,这会...

    commons-logging-1.2.jar

    然而,从Spring 4.x版本开始,Spring逐渐转向使用SLF4J(Simple Logging Facade for Java)作为首选的日志抽象层,但仍然保持对Commons Logging的兼容性,以照顾老版本的用户。 在实际使用时,为了配置Commons ...

    commons-logging-1.1.3.rar

    通过 Commons Logging,你可以方便地切换日志实现,如Log4j、java.util.logging (JUL) 或者 Simple Logging Facade for Java (SLF4J)。这样,如果你需要更换日志框架,只需要更改配置,而不需要修改代码。 在...

    commons-logging-1.2.1.1.jar

    此外,虽然Commons Logging提供了一种抽象的日志解决方案,但随着日志框架的不断发展,一些现代的替代品如SLF4J(Simple Logging Facade for Java)和Logback应运而生。SLF4J提供了一个更简洁的API,并且设计上更...

    commons-logging-1.1.1.jar开发架包

    Commons Logging是Apache软件基金会开发的一个Java日志抽象层,它为各种日志框架提供了一个统一的接口,如Log4j、java.util.logging (JUL) 和Simple Logging Facade for Java (SLF4J)。标题提到的"commons-logging-...

    commons-logging-1.1.1.rar

    然而,随着Java社区的发展,其他更现代的日志框架如SLF4J和Logback逐渐成为首选,它们提供了更高的性能和更强大的功能。尽管如此,了解和掌握Commons Logging对于理解Java日志处理的历史和演变仍然是非常有帮助的。

    slf4j最新jar包下载和jar包

    SLF4J(Simple Logging Facade for Java)是Java中的一种日志抽象层,它提供了一个接口,允许用户在运行时动态地绑定到各种具体的日志框架,如Log4j、Java内置的日志或者Logback等。这个设计使得开发者可以在不修改...

    commons-logging-1.1

    然而,由于它自身的一些设计问题,如性能和类加载问题,以及Logback和SLF4J等更现代替代品的出现, Commons Logging 在某些情况下可能会被替换。但作为一款历史悠久的库,它仍然在许多现有项目中发挥着作用。

    commons-logging-1.1.zip

    在实际开发中,如果你的项目已经依赖了 Commons Logging,并且你想使用更现代的日志框架,如 Logback 或者 SLF4J,你可以通过排除 Commons Logging 的依赖,并引入相应的适配器来实现平滑过渡。例如,引入 SLF4J 的...

    jar_commons-logging-1.2.rar

    Java Commons Logging(简称JCL)是Apache软件基金会提供的一种轻量级的日志接口,它允许开发者在不修改代码的情况下,自由地选择底层的日志实现,如Log4j、Java Util Logging或Logback等。本文将详细探讨JCL的核心...

    JAVA日志框架适配-冲突解决方案.docx

    slf4j(Simple Logging Facade for Java)和jcl(Apache Commons Logging)是JAVA中最主流的日志抽象。日志实现框架则负责具体的日志打印,如log4j、log4j2、logback、jul等。 常见的冲突原因: 1. 项目手动引用了...

    jcl-over-slf4j-1.6.0.jar logbank转log4j 日志转换 使用场景 实例

    在Java世界里,我们常常会遇到多种日志框架并存的情况,比如Jakarta Commons Logging(JCL)和Log4j。本文将详细介绍如何使用`jcl-over-slf4j-1.6.0.jar`这个桥接包,实现从JCL到SLF4J的日志系统转换,并探讨其实际...

    slf4J的所有相关jar

    SLF4J的主要目的是为各种日志框架,如log4j、logback、java.util.logging等,提供一个简单统一的接口,使得最终用户能够在部署时插入他们所选择的日志框架,而无需重新编译代码。SLF4J的设计理念是解耦应用程序的...

    commons-logging

    8. **替代方案**:虽然`commons-logging`历史悠久且广泛使用,但也有其他替代品,如SLF4J(Simple Logging Facade for Java)和Logback,它们提供更现代的功能和更好的性能。 总的来说,`commons-logging`作为日志...

    commons-logging-1.1.1

    这个“commons-logging-1.1.1”是该库的一个版本,它提供了对多种日志系统(如log4j、java.util.logging、JDK1.4 logging等)的统一接口。 1. **Java 日志接口**:Commons Logging 提供了一个简单的API,用于记录...

    commons-beanutils-1.9.4.zip

    3. 对于Apache Commons Logging,还需要根据项目的日志实现(如log4j、slf4j等)配置相应的日志系统。 使用Apache Commons BeanUtils,开发者可以方便地实现JavaBeans之间的属性复制、动态调用方法、处理集合与数组...

Global site tag (gtag.js) - Google Analytics