Log4J:我可以在运行时启用/禁用类的日志记录吗? [英] Log4J: can I enable/disable logging from a class at runtime?

查看:64
本文介绍了Log4J:我可以在运行时启用/禁用类的日志记录吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,仅在执行代码的特定阶段(例如,出于调试目的)才需要在运行时启用/禁用日志记录. 现在,我使用Java和Log4J进行调试.这是我的log4j.properties文件:

Sometimes logging needs to be enabled/disabled at runtime only during a specific phase of the execution of a code (e.g. for debugging purposes). Now, I use Java and Log4J to debug. This is my log4j.properties file:

# Define the root logger with appender file
log = /Users/Admin/Documents/log4j
log4j.rootLogger = DEBUG, stdout, file

#Logging for class Solution in package org.solver will be disabled
log4j.category.org.solver.Solution = OFF, stdout, file

# Define the file Console appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Define the file file appender
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=${log}/log.out

# Define the layout for file appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.conversionPattern=%m%n

您可能会看到,我同时登录文件和控制台输出.

As you may see, I log both on a file and on the console output.

现在,假设我有一个来自软件包foo的类X.如何在运行时启用和禁用日志记录? 在这里我找到了一个可能的答案,但一个示例将不胜感激.

Now, suppose that I have a class X from package foo. How can I enable and disable the logging at runtime? Here I found a possible answer, but an example would be really appreciated.

推荐答案

尝试使用以下类似内容:

Try using something like :

LogManager.getRootLogger().setLevel(Level.DEBUG);

您还可以将确切的记录器传递类添加到get中.

You can also get the exact logger passing class into the get.

这篇关于Log4J:我可以在运行时启用/禁用类的日志记录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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