本文介绍了在记录活动中使用readConfiguration方法 – java程序员分享,有助于帮助完成毕业设计以及求职,是一篇很好的资料。
对技术面试,学习经验等有一些体会,在此分享。
为了在小型Java桌面应用程序中使用日志记录,我试图深入了解某些方法的操作。我使用一个非常愚蠢的小型Java程序对其进行测试。
特别是,在测试LogManager.readConfiguration()方法的行为时,我发现了一些奇怪的东西。在所有测试中,LogManager均从JRE目录中lib / logging.properties中的属性文件中读取其配置。此时,此文件的内容如下:
handlers=java.util.logging.ConsoleHandler myapp2.handlers=java.util.logging.ConsoleHandler myapp2.MyApp2.handlers=java.util.logging.ConsoleHandler .level=OFF java.util.logging.ConsoleHandler.level=ALL java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n myapp2.level=WARNING myapp2.MyApp2.level=INFO
Java程序的代码为:
package myapp2; import java.io.IOException; import java.util.logging.LogManager; import java.util.logging.Logger; public class MyApp2 { private static final Logger LOGGER = Logger.getLogger(MyApp2.class.getPackage().getName()); private static final Logger LOGGER1 = Logger.getLogger(MyApp2.class.getName()); /** * @param args the command line arguments */ public static void main(String[] args) { LOGGER.severe("severe at MyApp2"); LOGGER.warning("warning at MyApp2"); LOGGER.info("info at MyApp2"); LOGGER1.severe("severe1 at MyApp2"); LOGGER1.warning("warning1 at MyApp2"); LOGGER1.info("info1 at MyApp2"); LOGGER1.setLevel(null); LOGGER1.setUseParentHandlers(false); LOGGER1.severe("severe2 at MyApp2"); LOGGER1.warning("warning2 at MyApp2"); LOGGER1.info("info2 at MyApp2"); try { LogManager.getLogManager().readConfiguration(); } catch (IOException ex) { System.out.println("I/O Exception found"); } catch (SecurityException ex) { System.out.println("Error SecurityException found"); } LOGGER.severe("severe3 at MyApp2"); LOGGER1.severe("severe4 at MyApp2"); } }
如果我们在没有围绕readConfiguration()的try-catch的情况下执行它,则将按预期工作,并且输出如下:
SEVERE: severe at MyApp2 [dc. maig 08 14:45:38 CEST 2013] SEVERE: severe at MyApp2 [dc. maig 08 14:45:38 CEST 2013] WARNING: warning at MyApp2 [dc. maig 08 14:45:38 CEST 2013] WARNING: warning at MyApp2 [dc. maig 08 14:45:38 CEST 2013] SEVERE: severe1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] SEVERE: severe1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] SEVERE: severe1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] WARNING: warning1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] WARNING: warning1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] WARNING: warning1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] INFO: info1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] INFO: info1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] INFO: info1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] SEVERE: severe2 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] WARNING: warning2 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] SEVERE: severe3 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] SEVERE: severe3 at MyApp2 [dc. maig 08 14:45:38 CEST 2013] SEVERE: severe4 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
但是,如果我们使用try-catch执行,则输出为:
SEVERE: severe at MyApp2 [dc. maig 08 14:46:51 CEST 2013] SEVERE: severe at MyApp2 [dc. maig 08 14:46:51 CEST 2013] WARNING: warning at MyApp2 [dc. maig 08 14:46:51 CEST 2013] WARNING: warning at MyApp2 [dc. maig 08 14:46:51 CEST 2013] SEVERE: severe1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] SEVERE: severe1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] SEVERE: severe1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] WARNING: warning1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] WARNING: warning1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] WARNING: warning1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] INFO: info1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] INFO: info1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] INFO: info1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] SEVERE: severe2 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] WARNING: warning2 at MyApp2 [dc. maig 08 14:46:51 CEST 2013] SEVERE: severe3 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
读取readConfiguration()方法的API后,应该重新初始化日志记录属性,并从先前命名的文件中重新读取日志记录配置。如果是的话,为什么feature3仅显示一次(由于程序中存在两个LOGGER和转发行为,我预计将显示两次)而严重4丢失(我希望显示一次)?有人可以帮我理解这个吗?
参考方案
我在readConfiguration方法中遇到了很多错误。它不是您期望的那样。我创建了一个小单元测试来说明这一点:
package com.demo; import java.io.IOException; import java.io.InputStream; import java.util.logging.LogManager; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author dhubau */ public class LogTest { private static final Logger logger = LoggerFactory.getLogger(LogTest.class.getCanonicalName()); @Test public void testLogs() throws IOException { logger.trace("BEFORE"); logger.debug("BEFORE"); logger.info("BEFORE"); logger.warn("BEFORE"); logger.error("BEFORE"); InputStream is = LogTest.class.getResourceAsStream("/logging.properties"); LogManager.getLogManager().readConfiguration(is); logger.trace("AFTER"); logger.debug("AFTER"); logger.info("AFTER"); logger.warn("AFTER"); logger.error("AFTER"); } }
启动单元测试时,将读取默认的logging.properties(将所有信息默认为INFO日志记录)。我记录每个级别一次,然后读取自己的logging.properties文件:
# Specify the handlers to create in the root logger handlers = java.util.logging.ConsoleHandler # Set the default logging level for the root logger .level = INFO # Do not use the root handlers com.demo.useParentHandlers = false # DEMO log handlers com.demo.handlers = java.util.logging.ConsoleHandler # DEMO log level com.demo.level = ALL # Set the default logging level for new ConsoleHandler instances java.util.logging.ConsoleHandler.level = ALL
之后,我得到以下输出:
May 24, 2013 11:27:29 AM com.demo.LogTest testLogs INFO: BEFORE May 24, 2013 11:27:29 AM com.demo.LogTest testLogs WARNING: BEFORE May 24, 2013 11:27:29 AM com.demo.LogTest testLogs SEVERE: BEFORE May 24, 2013 11:27:29 AM com.demo.LogTest testLogs INFO: AFTER May 24, 2013 11:27:29 AM com.demo.LogTest testLogs WARNING: AFTER May 24, 2013 11:27:29 AM com.demo.LogTest testLogs SEVERE: AFTER
因此,您看到com.demo程序包未记录TRACE或DEBUG级别。当我将以下参数传递给单元测试时:
java.util.logging.config.file = C: TEMP logging.properties
它给了我以下输出:
May 24, 2013 11:27:29 AM com.demo.LogTest testLogs FINEST: BEFORE May 24, 2013 11:27:29 AM com.demo.LogTest testLogs FINE: BEFORE May 24, 2013 11:27:29 AM com.demo.LogTest testLogs INFO: BEFORE May 24, 2013 11:27:29 AM com.demo.LogTest testLogs WARNING: BEFORE May 24, 2013 11:27:29 AM com.demo.LogTest testLogs SEVERE: BEFORE May 24, 2013 11:27:29 AM com.demo.LogTest testLogs INFO: AFTER May 24, 2013 11:27:29 AM com.demo.LogTest testLogs WARNING: AFTER May 24, 2013 11:27:29 AM com.demo.LogTest testLogs SEVERE: AFTER
Java-搜索字符串数组中的字符串 – java
在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…
NoClassDefFoundError的可能原因 – java
我得到以下NoClassDefFoundError,这很奇怪,因为该类已经存在于库jar中。Exception in thread "main" java.lang.NoClassDefFoundError: abc/test/Test.java at java.lang.ClassLoader.defineClass1(Native M…
Java Double与BigDecimal – java
我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? – java
我正在使用Retrofit来获取JSON答复。这是我实施的一部分[email protected]("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
Java-父类正在从子类中调用方法? – java
抱歉,我还是编码的新手,可能还没有掌握所有术语。希望您仍然能理解我的问题。我想得到的输出是:"Cost for Parent is: 77.77" "Cost for Child is: 33.33" 但是,我得到这个:"Cost for Parent is: 33.33" "Cost f…
最新评论