java - Use of readConfiguration method in logging activities -
in order use logging in small java desktop application i'm trying understand in depth operation of methods. use stupid , small java program test them.
in particular, when testing behaviour of logmanager.readconfiguration() method i've found strange. in tests logmanager reads configuration properties files located in lib/logging.properties in jre directory. @ time, contents of file follows :
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
the code of java program :
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 command line arguments */ public static void main(string[] args) { logger.severe("severe @ myapp2"); logger.warning("warning @ myapp2"); logger.info("info @ myapp2"); logger1.severe("severe1 @ myapp2"); logger1.warning("warning1 @ myapp2"); logger1.info("info1 @ myapp2"); logger1.setlevel(null); logger1.setuseparenthandlers(false); logger1.severe("severe2 @ myapp2"); logger1.warning("warning2 @ myapp2"); logger1.info("info2 @ 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 @ myapp2"); logger1.severe("severe4 @ myapp2"); } }
if execute without try-catch surrounding readconfiguration(), works expected , output following :
severe: severe @ myapp2 [dc. maig 08 14:45:38 cest 2013] severe: severe @ myapp2 [dc. maig 08 14:45:38 cest 2013] warning: warning @ myapp2 [dc. maig 08 14:45:38 cest 2013] warning: warning @ myapp2 [dc. maig 08 14:45:38 cest 2013] severe: severe1 @ myapp2 [dc. maig 08 14:45:38 cest 2013] severe: severe1 @ myapp2 [dc. maig 08 14:45:38 cest 2013] severe: severe1 @ myapp2 [dc. maig 08 14:45:38 cest 2013] warning: warning1 @ myapp2 [dc. maig 08 14:45:38 cest 2013] warning: warning1 @ myapp2 [dc. maig 08 14:45:38 cest 2013] warning: warning1 @ myapp2 [dc. maig 08 14:45:38 cest 2013] info: info1 @ myapp2 [dc. maig 08 14:45:38 cest 2013] info: info1 @ myapp2 [dc. maig 08 14:45:38 cest 2013] info: info1 @ myapp2 [dc. maig 08 14:45:38 cest 2013] severe: severe2 @ myapp2 [dc. maig 08 14:45:38 cest 2013] warning: warning2 @ myapp2 [dc. maig 08 14:45:38 cest 2013] severe: severe3 @ myapp2 [dc. maig 08 14:45:38 cest 2013] severe: severe3 @ myapp2 [dc. maig 08 14:45:38 cest 2013] severe: severe4 @ myapp2 [dc. maig 08 14:45:38 cest 2013]
but, if execute try-catch, output :
severe: severe @ myapp2 [dc. maig 08 14:46:51 cest 2013] severe: severe @ myapp2 [dc. maig 08 14:46:51 cest 2013] warning: warning @ myapp2 [dc. maig 08 14:46:51 cest 2013] warning: warning @ myapp2 [dc. maig 08 14:46:51 cest 2013] severe: severe1 @ myapp2 [dc. maig 08 14:46:51 cest 2013] severe: severe1 @ myapp2 [dc. maig 08 14:46:51 cest 2013] severe: severe1 @ myapp2 [dc. maig 08 14:46:51 cest 2013] warning: warning1 @ myapp2 [dc. maig 08 14:46:51 cest 2013] warning: warning1 @ myapp2 [dc. maig 08 14:46:51 cest 2013] warning: warning1 @ myapp2 [dc. maig 08 14:46:51 cest 2013] info: info1 @ myapp2 [dc. maig 08 14:46:51 cest 2013] info: info1 @ myapp2 [dc. maig 08 14:46:51 cest 2013] info: info1 @ myapp2 [dc. maig 08 14:46:51 cest 2013] severe: severe2 @ myapp2 [dc. maig 08 14:46:51 cest 2013] warning: warning2 @ myapp2 [dc. maig 08 14:46:51 cest 2013] severe: severe3 @ myapp2 [dc. maig 08 14:46:51 cest 2013]
reading api of readconfiguration() method supposed reinitialize logging properties , reread logging configuration file named previously. if so, why severe3 showing once (i expect displayed 2 times because of 2 loggers present in program , forwarding behaviour) , severe4 missing (i expect displayed once)?. may me undestand this?.
i have encountered many more mistakes readconfiguration method. not expect do. have created small unit test illustrate this:
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 = logtest.class.getresourceasstream("/logging.properties"); logmanager.getlogmanager().readconfiguration(is); logger.trace("after"); logger.debug("after"); logger.info("after"); logger.warn("after"); logger.error("after"); } }
when starting unit test, default logging.properties read (defaulting info logging). log every level once , read in own logging.properties file:
# specify handlers create in root logger handlers = java.util.logging.consolehandler # set default logging level root logger .level = info # not use root handlers com.demo.useparenthandlers = false # demo log handlers com.demo.handlers = java.util.logging.consolehandler # demo log level com.demo.level = # set default logging level new consolehandler instances java.util.logging.consolehandler.level =
after following output:
may 24, 2013 11:27:29 com.demo.logtest testlogs info: before may 24, 2013 11:27:29 com.demo.logtest testlogs warning: before may 24, 2013 11:27:29 com.demo.logtest testlogs severe: before may 24, 2013 11:27:29 com.demo.logtest testlogs info: after may 24, 2013 11:27:29 com.demo.logtest testlogs warning: after may 24, 2013 11:27:29 com.demo.logtest testlogs severe: after
so see, com.demo package not logging trace or debug levels. when pass following parameter unit test:
java.util.logging.config.file = c:\temp\logging.properties
it gives me following output:
may 24, 2013 11:27:29 com.demo.logtest testlogs finest: before may 24, 2013 11:27:29 com.demo.logtest testlogs fine: before may 24, 2013 11:27:29 com.demo.logtest testlogs info: before may 24, 2013 11:27:29 com.demo.logtest testlogs warning: before may 24, 2013 11:27:29 com.demo.logtest testlogs severe: before may 24, 2013 11:27:29 com.demo.logtest testlogs info: after may 24, 2013 11:27:29 com.demo.logtest testlogs warning: after may 24, 2013 11:27:29 com.demo.logtest testlogs severe: after
Comments
Post a Comment