使用SLF4J设置动态控制台日志记录级别

编程入门 行业动态 更新时间:2024-10-24 04:51:51
本文介绍了使用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设置动态控制台日志记录级别

本文发布于:2023-11-25 04:02:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   级别   动态   日志   SLF4J

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!