SLF4J-Log4J似乎没有禁用记录功能[英] SLF4J-Log4J does not appear to have disabled logging

本文是小编为大家收集整理的关于SLF4J-Log4J似乎没有禁用记录功能的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

看来,尽管日志级别设置为信息,但SLF4J仍在评估表达式.

package com.ab.test.slf4j;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleTest {
    static final Logger logger = LoggerFactory.getLogger(SimpleTest.class);

    public static void main(String[] args) {
        PropertyConfigurator.configure("log4j.properties");

        logger.debug("Test " + testEnter());

        logger.debug("Test {}", testEnter());
    }

    public static String testEnter() {
        System.out
                .println("If you see this it means your expression is evaluated :(");

        return "test";
    }
}

log4j属性文件:

log4j.rootLogger=INFO, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

运行输出:

If you see this it means your expression is evaluated :(
If you see this it means your expression is evaluated :(

编辑:修改了运行输出,如所见,对表达式进行了评估,但是日志消息不是.表达式不应按照 slf4j的"绩效记录"

推荐答案

不是SLF4J,但是JVM需要在函数调用之前计算每个传递的参数.

更仔细地阅读给定链接. SLF4J仅跳过调用ToString()以获取跳过日志语句的对象参数.它不适用于跳过功能调用.

但是您可以为此创建自定义函数对象:

    logger.debug(new Object() {
        @Override
        public String toString() {
            return "some expensive test data";
        }
    });

tostring()方法只有在不跳过调试时才会调用.

本文地址:https://www.itbaoku.cn/post/1575008.html

问题描述

It seems that although the log level was set to INFO, SLF4J is still evaluating the expression.

package com.ab.test.slf4j;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleTest {
    static final Logger logger = LoggerFactory.getLogger(SimpleTest.class);

    public static void main(String[] args) {
        PropertyConfigurator.configure("log4j.properties");

        logger.debug("Test " + testEnter());

        logger.debug("Test {}", testEnter());
    }

    public static String testEnter() {
        System.out
                .println("If you see this it means your expression is evaluated :(");

        return "test";
    }
}

Log4J Property file:

log4j.rootLogger=INFO, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Run output:

If you see this it means your expression is evaluated :(
If you see this it means your expression is evaluated :(

EDIT: Amended the run output, as seen, the expression is evaluated, however the log message is not. Expression should not be evaluated as per SLF4J's "Performance Logging"

推荐答案

It's not slf4j but JVM need to compute every passed parameter before function call.

Read the given link more carefully. slf4j only skips calling toString() for Object params of skipped log statements. It's not working for skipping function calls.

But you can create custom functor object for this:

    logger.debug(new Object() {
        @Override
        public String toString() {
            return "some expensive test data";
        }
    });

toString() method would be called only if debug is not skipped.