log4j性能 [英] log4j performance

查看:157
本文介绍了log4j性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网络应用程序,我想记录一些信息,以帮助我改进和观察应用程序。 (我正在使用Tomcat6)

I'm developing a web app, and I'd like to log some information to help me improve and observe the app. (I'm using Tomcat6)

首先我想我会使用StringBuilders,将日志附加到它们,并且任务会像每2分钟一样将它们保存到数据库中。因为我担心开箱即用的日志记录系统的性能。然后我做了一些测试。特别是log4j。

First I thought I would use StringBuilders, append the logs to them and a task would persist them into the database like every 2 minutes. Because I was worried about the out-of-the-box logging system's performance. Then I made some test. Especially with log4j.

这是我的代码:

Main.java

Main.java

public static void main(String[] args) {
  Thread[] threads = new Thread[LoggerThread.threadsNumber];

  for(int i = 0; i < LoggerThread.threadsNumber; ++i){
   threads[i] = new Thread(new LoggerThread("name - " + i));
  }
  LoggerThread.startTimestamp = System.currentTimeMillis();

  for(int i = 0; i < LoggerThread.threadsNumber; ++i){
   threads[i].start();
  }

LoggerThread.java

LoggerThread.java

public class LoggerThread implements Runnable{
 public static int threadsNumber = 10;
 public static long startTimestamp;
 private static int counter = 0;
 private String name;

 public LoggerThread(String name) {
  this.name = name;
 }
 private Logger log = Logger.getLogger(this.getClass());

 @Override
 public void run() {
  for(int i=0; i<10000; ++i){
   log.info(name + ": " + i);

   if(i == 9999){
    int c = increaseCounter();

    if(c == threadsNumber){
     System.out.println("Elapsed time: " + 
       (System.currentTimeMillis() - startTimestamp));
    }
   }

  }
 }

 private synchronized int increaseCounter(){
  return ++counter;
 }

}
     }

log4j。属性

log4j.logger.main.LoggerThread=debug, f
log4j.appender.f=org.apache.log4j.RollingFileAppender
log4j.appender.f.layout=org.apache.log4j.PatternLayout
log4j.appender.f.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.f.File=c:/logs/logging.log
log4j.appender.f.MaxFileSize=15000KB
log4j.appender.f.MaxBackupIndex=50

我认为这是log4j非常常见的配置。
首先我使用log4j 1.2.14然后我意识到有一个更新的版本,所以我切换到1.2.16

I think this is a very common configuration for log4j. First I used log4j 1.2.14 then I realized there was a newer version, so I switched to 1.2.16

以下是数字(全部以毫秒为单位) )

Here are the figures (all in millisec)

LoggerThread.threadsNumber = 10

1.2.14: 4235, 4267, 4328, 4282
1.2.16: 2780, 2781, 2797, 2781

LoggerThread.threadsNumber = 100

1.2.14: 41312, 41014, 42251
1.2.16: 25606, 25729, 25922

我觉得这很快。不要忘记:在每个循环中,run方法不仅仅是登录文件,还必须连接字符串(name +:+ i),并检查一个如果测试(i == 9999)

I think this is very fast. Don't forget that: in every cycle the run method not just log into the file, it has to concatenate strings (name + ": " + i), and check an if test (i == 9999).

当threadsNumber为10时,有100,000个记录,如果测试和级联。当它为100时,有1.000.000个记录,如果测试和连接。 (我已经读过某个地方JVM使用StringBuilder的追加连接,而不是简单的连接)。

When threadsNumber is 10, there are 100.000 loggings and if tests and concatenations. When it is 100, there are 1.000.000 loggings and if tests and concatenations. (I've read somewhere JVM uses StringBuilder's append for concatenation, not simple concatenation).

我错过了什么吗?难道我做错了什么?我是否忘记了任何可能降低性能的因素?
如果这些数字是正确的,我认为我不必担心log4j的性能即使我记录很多,是吗?

Did I miss something? Am I doing something wrong? Did I forget any factor that could decrease the performance? If these figures are correct I think I don't have to worry about log4j's performance even if I heavily log, do I?

我读过那个:实际记录的典型成本约为100到300微秒。这是对的吗? ( log4J手册

I've read that: "The typical cost of actually logging is about 100 to 300 microseconds." Is it correct? (log4J manual)

推荐答案

是的,由于其实现者的有意识的努力,已知Log4J很快。另请参阅本 Log4J简介末尾的性能部分。

Yes, Log4J is known to be fast, due to the conscious effort of its implementors. See also the section "Performance" at the end of this introduction to Log4J.

这篇关于log4j性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆