开发者

Is it possible to specify a java.util.log formatter via a -D option or some way that doesn't require a code change?

开发者 https://www.devze.com 2023-03-12 08:38 出处:网络
I have a java program which is using the default XML logging as no formatter is defined, I am wondering if there is a way to change this outside of modifying the code or adding a logging.properties fi

I have a java program which is using the default XML logging as no formatter is defined, I am wondering if there is a way to change this outside of modifying the code or adding a logging.properties file(it has none atm). Is there a way to specify/change the default file handler from XML to SimpleFormatter for Java.util.log? It's logging configuration is hard coded:

fh = new FileHandler("/path/to/logfile",true);
logger.addHandler(fh);

Rather then having to add fh.setFormatter(new SimpleFormatter()) in the code here, I am wondering if开发者_Go百科 there is anyway via command line that I can specify SimpleFormatter to be used as the default formatter rather then the XML formatter which it defaults to?

Thanks for any thoughts


You can specify how logging should be done in several ways, and they are all described in the javadoc for LogManager.

You can set the system property java.util.logging.config.file and use a file for configuration, or you can set java.util.logging.config.class and use a class to configure the logging.

The logging.properties file in the JRE/lib directory will be used if none of those properties are set (unless the code specifies another logging configuration)


Looks like you can set the system property java.util.logging.FileHandler.formatter according to the Java Doc.

0

精彩评论

暂无评论...
验证码 换一张
取 消