使用SLF4J设置动态控制台日志记录级别 [英] Set Dynamic Console Logging Level Using SLF4J

查看:2437
本文介绍了使用SLF4J设置动态控制台日志记录级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在logger内置的jdk上使用slf4j记录外观.我很困惑如何使用SLF4J的ConsoleHandler设置日志记录级别.

I'm using slf4j logging facade over the jdk built in logger. I'm confused how to set the logging level using a ConsoleHandler for SLF4J.

public Class foo(){
   Logger log = Logger.getLogger(foo.class()) //logger from SLF
   public void run(){
       log.info("info")
    }
   private static void main(String[] args){
     ConsoleHandler con = new ConsoleHandeler(); //from java.util.logging
     con.setLevel(Level.OFF)
     run()
    }
   }


  >INFO: info

是否应关闭信息?我不想设置日志记录属性文件,因为我希望用户从命令行界面输入他们想要的日志记录级别.我只关心控制台附加.这是否类似于:使用java.util.logging进行记录在控制台上

Shouldn't the info be turned off? I didn't want to set up a logging properties file because I want the user to input the level of logging they want from a command line interface. I only care about console appending. Is this similar to this: Using java.util.logging to log on the console

推荐答案

请勿混合使用SLF4J和日志记录实现. SLF4J的想法是,您只需替换jar和配置文件即可切换记录器实现.您的代码中将没有任何日志记录实现的引用(lkog4j,util日志记录等).由于SLF4J是日志记录的外观,因此您将只能控制日志级别和所记录的内容.处理程序等是日志记录实现的一部分.
您应该让用户决定要记录的位置,要记录的级别等.

除此之外;禁用util logger中的控制台日志记录.做

Do not mix using SLF4J and a logging implementation. The idea of SLF4J is that you can switch the logger implementation by just replacing a jar and a config file. There will be no reference in your code to any logging impolementation (lkog4j, util logging, etc). Since SLF4J is a logging facade, you will be able to control only the log levels and what is being logged. Handlers etc are part of logging implementation.
You should leave the user to decide where to log, what level to log etc.

Apart from that; to disable console logging in util logger. Do

获取root记录器Logger logger = Logger.getLogger("");
获取所有处理程序logger.getHandlers();
迭代并检查是否有任何instanceof ConsoleHandler
设置该处理程序con.setLevel(Level.OFF)

Get the root logger Logger logger = Logger.getLogger("");
get all its handlers logger.getHandlers();
iterate and check if any of it is instanceof ConsoleHandler
set level for that handler con.setLevel(Level.OFF)

在您的main方法中,您刚刚创建了一个新的ConsoleHandler.它没有与任何记录器相关联,因此不会发生任何事情.

In your main method you just created a new ConsoleHandler. It is not associated to any logger so nothing will happen.

这篇关于使用SLF4J设置动态控制台日志记录级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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