Log4j性能和对象创建

时间:2017-03-16 07:59:23

标签: java logging

我对 log4j 日志的性能有疑问,在我的项目中,我在类中看到了很多日志消息。在生产环境中,日志不打印很多这些(正常的行为)。无论如何,每个日志都创建一个 String 对象。例如:

Log.debug("This my debug message 1");
...
Log.debug("This my debug message 400");

我们为日志创建了很多字符串文字。即使我只显示错误级别,这会对我的生产站点产生很大影响吗?

由于

3 个答案:

答案 0 :(得分:2)

如果构造日志字符串相当复杂(例如,在对象上调用复杂的03-16 10:55:09.434 5521-5521/? D/dalvikvm: Not late-enabling CheckJNI (already on) 03-16 10:55:09.554 5521-5521/app.droid.moring E/Trace: error opening trace file: No such file or directory (2) 03-16 10:55:09.804 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.app.Application.registerOnProvideAssistDataListener, referenced from method com.android.tools.fd.runtime.BootstrapApplication.registerOnProvideAssistDataListener 03-16 10:55:09.804 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve virtual method 232: Landroid/app/Application;.registerOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V 03-16 10:55:09.804 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 03-16 10:55:09.804 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.app.Application.unregisterOnProvideAssistDataListener, referenced from method com.android.tools.fd.runtime.BootstrapApplication.unregisterOnProvideAssistDataListener 03-16 10:55:09.804 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve virtual method 235: Landroid/app/Application;.unregisterOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V 03-16 10:55:09.804 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 03-16 10:55:09.804 5521-5521/app.droid.moring I/InstantRun: Instant Run Runtime started. Android package is app.droid.moring, real application class is null. 03-16 10:55:09.804 5521-5521/app.droid.moring W/InstantRun: No instant run dex files added to classpath 03-16 10:55:09.824 5521-5521/app.droid.moring E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources 03-16 10:55:09.824 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve check-cast 1901 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher; 03-16 10:55:09.824 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x1f at 0x025e 03-16 10:55:09.824 5521-5521/app.droid.moring E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache 03-16 10:55:09.824 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve const-class 1901 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher; 03-16 10:55:09.824 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x1c at 0x0060 03-16 10:55:09.874 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts 03-16 10:55:09.874 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve interface method 16095: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V 03-16 10:55:09.874 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 03-16 10:55:09.874 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 03-16 10:55:09.874 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 03-16 10:55:09.874 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve interface method 16097: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 03-16 10:55:09.874 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 03-16 10:55:09.874 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 03-16 10:55:09.874 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve interface method 16101: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 03-16 10:55:09.874 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 03-16 10:55:09.874 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 03-16 10:55:09.874 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve virtual method 681: Landroid/content/res/TypedArray;.getChangingConfigurations ()I 03-16 10:55:09.874 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 03-16 10:55:09.874 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 03-16 10:55:09.874 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve virtual method 703: Landroid/content/res/TypedArray;.getType (I)I 03-16 10:55:09.874 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008 03-16 10:55:09.904 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild 03-16 10:55:09.904 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve virtual method 16529: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 03-16 10:55:09.904 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002 03-16 10:55:09.904 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList 03-16 10:55:09.904 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve virtual method 418: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList; 03-16 10:55:09.904 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 03-16 10:55:09.914 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable 03-16 10:55:09.914 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve virtual method 644: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 03-16 10:55:09.914 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 03-16 10:55:09.914 5521-5521/app.droid.moring I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity 03-16 10:55:09.914 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve virtual method 646: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 03-16 10:55:09.914 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 03-16 10:55:09.924 5521-5521/app.droid.moring E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 03-16 10:55:09.924 5521-5521/app.droid.moring W/dalvikvm: VFY: unable to resolve instanceof 151 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper; 03-16 10:55:09.924 5521-5521/app.droid.moring D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c 03-16 10:55:09.964 5521-5524/app.droid.moring D/dalvikvm: GC_CONCURRENT freed 211K, 10% free 2590K/2860K, paused 5ms+14ms, total 25ms 03-16 10:55:10.154 5521-5521/app.droid.moring D/libEGL: loaded /system/lib/egl/libEGL_emulation.so 03-16 10:55:10.154 5521-5521/app.droid.moring D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so 03-16 10:55:10.174 5521-5521/app.droid.moring D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so [ 03-16 10:55:10.174 5521: 5521 D/ ] HostConnection::get() New Host Connection established 0xb84e6710, tid 5521 03-16 10:55:10.314 5521-5521/app.droid.moring W/gralloc_ranchu: Gralloc pipe failed 03-16 10:55:10.364 5521-5521/app.droid.moring D/OpenGLRenderer: Enabling debug mode 0 03-16 10:55:32.854 5521-5884/app.droid.moring W/dalvikvm: threadid=16: thread exiting with uncaught exception (group=0xb0ff2908) 03-16 10:55:32.854 5521-5884/app.droid.moring E/AndroidRuntime: FATAL EXCEPTION: Thread-211 java.lang.NumberFormatException: Invalid int: "hi" at java.lang.Integer.invalidInt(Integer.java:138) at java.lang.Integer.parse(Integer.java:375) at java.lang.Integer.parseInt(Integer.java:366) at java.lang.Integer.parseInt(Integer.java:332) at app.droid.moring.MainActivity$2$1.run(MainActivity.java:75) at java.lang.Thread.run(Thread.java:856) ),它可以。

在你的情况下(简单的字符串记录),可能不值得写一个警卫:

toString

请参阅http://logging.apache.org/log4j/1.2/manual.html(底部的“效果”部分)

答案 1 :(得分:0)

固定字符串文字没有问题,没有与之关联的运行时开销(除了方法调用本身)。

另一种情况是构造消息,如"user: " + user。即使日志未激活,也会完成字符串连接。

答案 2 :(得分:0)

这是一个性能问题,基本归结为:衡量它! 我们不知道有多少“很多”日志消息,您的其他代码有多贵等等。太多因素无法给出有根据的答案。

我认为一般的理解是在大多数情况下合理的日志语句不应该影响性能并且使用更快的硬件比开发人员删除日志语句更便宜。但这可能无法涵盖您的具体案例。

如果日志消息不是文字,而是生成的字符串(字符串连接或复杂的toString() - 方法),则日志记录会变得更加昂贵。)有关详细信息,请查看log4j 1.x.log4j 2.x手册的效果部分

删除日志语句可能属于premature optimization,但尝试最小化开销(至少在关键部分中)并不是错误的。在log4J中,这可以完成(如建议的那样):

  • 使用格式模板样式消息(自log4j 2.0起可用):

    logger.debug("Entry number: {} is {}", i, entry[i]);
    
  • 使用警卫

    if (logger.isDebugEnabled()) {
      logger.debug(expensiveMethodReturningLogString());
    }