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

查看:449
本文介绍了SLF4J-Log4J似乎没有禁用日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎将日志级别设置为INFO,但SLF4J仍在评估表达式.

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属性文件:

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

运行输出:

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

修改运行输出,如所示,对表达式求值,但不记录日志消息.表达式不应按照 SLF4J的性能记录"

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"

推荐答案

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

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

更仔细地阅读给定的链接. slf4j仅跳过对跳过的日志语句的对象参数调用toString().跳过函数调用不起作用.

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()方法.

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

这篇关于SLF4J-Log4J似乎没有禁用日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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