英文:
Java Util Logger entring and exiting methods do not process
问题
I tried to use the java.util.logging.Logger.entering()
and exiting()
operations, however, they don't seem to be processing. Do I need to specify a Handler for the Logger or does it work without one similar to Logger.log()
? In the code below, the logger.log()
is the only method prints to the console but I do not see any messages from logger.entering()
and logger.exiting()
to the console. Any insights will be great.
通过为记录器添加处理程序,entering()
和 exiting()
操作可以正常工作,但更新后的代码的困扰之处在于每个日志记录都会出现两次。以下是控制台输出的一部分:
通过在构造函数中移除向记录器添加处理程序,entering()
和 exiting()
操作不会出现在日志中,但双重日志条目的问题将消失。以下是输出的示例:
英文:
I tried to use the java.util.logging.Logger.entring() and the exiting() operations, however, they don't seem to be processing. Do I need to specify a Handler for the Logger or does it work without one similar to Logger.log()? In the code below, the logger.log() is the only method prints to the console but I do not see any messages from logger.entring() and logger.exiting() to the console. Any insights will be great.
Thanks,
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author hrh74
*/
public class SimpleLogger {
private static Logger logger =null;
public SimpleLogger(){
logger = Logger.getLogger(SimpleLogger.class.getName());
logger.setLevel(Level.FINER);
//ConsoleHandler h = new ConsoleHandler();
//h.setLevel(Level.FINER);
// logger.addHandler(h);
for(Handler handler: logger.getHandlers()){
handler.setLevel(Level.FINER);
}
}
public void simplyLog(){
logger.entering(getClass().getName(),"In simplyLog");
logger.log(Level.INFO, "FYI..");
logger.exiting(getClass().getName(), "Leaving simplyLog");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SimpleLogger simple = new SimpleLogger();
simple.simplyLog();
}
}
By adding a Handler to the logger, the entering() and existing() operations are working, however, the troubling aspect of the updated code is the dual entries for each log record. Below is the snippet of the output to the console:
Aug 12, 2020 10:51:32 PM org.me.logger.SimpleLogger In simplyLog
FINER: ENTRY
Aug 12, 2020 10:51:32 PM org.me.logger.SimpleLogger simplyLog
INFO: FYI..
Aug 12, 2020 10:51:32 PM org.me.logger.SimpleLogger simplyLog
INFO: FYI..
Aug 12, 2020 10:51:32 PM org.me.logger.SimpleLogger Leaving simplyLog
FINER: RETURN
Now, if I remove adding a Handler to the Logger in the constructor, then entering() and existing() operations do not appear in the log but the problem with the dual log entries will go away. Here is a sample of the ouptut
Aug 13, 2020 9:43:55 AM org.me.logger.SimpleLogger simplyLog
INFO: FYI..
答案1
得分: 0
请看文档以了解这些方法。它们以FINER级别记录日志。
只有当日志条目的级别高于Logger的级别和输出它的处理程序的级别时,才会打印日志条目。在您的情况下,您的记录器正在使用根记录器的默认处理程序,即ConsoleHandler。
您需要设置根记录器的处理程序以记录所需级别:
Logger rootLogger = Logger.getLogger("");
for (Handler handler: rootLogger.getHandlers()) {
handler.setLevel(Level.FINER);
}
英文:
See the documentation for these methods. They log at FINER level.
A log entry gets printed only if its level is above that of the Logger and that of the handler that outputs it. In your case, your logger is using the root logger's default handler, which is a ConsoleHandler.
You need to set the root logger's handlers to log at the required level:
Logger rootLogger = Logger.getLogger("");
for (Handler handler: rootLogger.getHandlers()) {
handler.setLevel(Level.FINER);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论