Saturday, 15 February 2014

java - Gradle Logging Output Levels -



java - Gradle Logging Output Levels -

in project classes i've used java.util.logging.logger , added various log output's throughout code, using various log levels ie.

src/main/java/run.java

import java.util.logging.level; import java.util.logging.logger; public class run{ public static void main( string args[] ){ system.out.println("hello world"); logger.log(level.config, "just config info"); logger logger = logger.getlogger(run.class.getname()); logger.log(level.info, "just logging info"); logger.log(level.fine, "fine logging"); logger.log(level.finer, "finer logging"); logger.log(level.warning, "this warning log!"); } }

currently when run gradle -i test log messages level.info defined shown none of config, warn or fine messages output.

i've tried updating build.gradle file such that:

apply plugin: 'java' apply plugin:'application' mainclassname = "run" repositories { mavencentral() } dependencies { testcompile "junit:junit:4.11" } run{ systemproperties = ['java.util.logging.config.file' : 'logging.properties'] }

i've included:

systemproperties = ['java.util.logging.config.file' : 'logging.properties']

then created /src/main/resource/logging.propertiess

handlers= java.util.logging.consolehandler .level= config java.util.logging.consolehandler.level = finer java.util.logging.consolehandler.formatter = java.util.logging.simpleformatter

running:

gradle run

i get:

:compilejava up-to-date :processresources up-to-date :classes up-to-date :run hello world build successful

and when running gradle -i run get: started process 'command '/library/java/javavirtualmachines/jdk1.8.0_20.jdk/contents/home/bin/java'' hello world :run (thread[main,5,main]) completed. took 0.202 secs.

build successful

ie. no logging information. commenting out system.properties within run task , re-running gradle -i run get:

successfully started process 'command '/library/java/javavirtualmachines/jdk1.8.0_20.jdk/contents/home/bin/java'' hello world nov 05, 2014 12:07:42 pm run main info: logging info nov 05, 2014 12:07:42 pm run main warning: warning log! :run (thread[main,5,main]) completed. took 0.229 secs. build successful

the info , warning level logs, not fine or finer ones.

tldr;

how config, fine & finer level logs log console in generic gradle java project ?

several options (i prefer alternative 2.2):

1) custom logging.properties file:

the java logging api has default logging configuration file @ <jre_home>/lib/logging.properties. can utilize own config file setting jvm property java.util.logging.config.file.

handlers = java.util.logging.consolehandler run.handlers = java.util.logging.consolehandler run.level = finer run.useparenthandlers = false java.util.logging.consolehandler.level = java.util.logging.consolehandler.formatter = java.util.logging.simpleformatter

you have set useparenthandlers = false avoid getting duplicate prints parent handlers.

1.1) set above property absolute path

did not seek ;-)

1.2) load custom file follows in run.java

loading follows in run.java:

inputstream inputstream = run.class.getresourceasstream("mylogging.properties"); seek { logmanager.getlogmanager().readconfiguration(inputstream); } catch(exception e) { e.printstacktrace(); }

2) custom scheme property (i.e.: loglevel)

define systemproperty in build.gradle:

run { systemproperty 'loglevel', system.getproperty('loglevel') }

add defaultloglevel in run.java:

public static level defaultlevel = level.info;

get value of loglevel property:

string loglevel = system.getproperty("loglevel");

and set defined level in logger:

logger logger = logger.getlogger(run.class.getname()); logger.setlevel(loglevel.isempty() ? defaultlevel : level.parse(loglevel));

2.1) create new consolehandler , switch-off print parent handlers

system.out.println(run.class.getname()); logger logger = logger.getlogger(run.class.getname()); logger.setlevel(loglevel.isempty() ? defaultlevel : level.parse(loglevel)); handler consolehandler = new consolehandler(); consolehandler.setlevel(loglevel.isempty() ? defaultlevel : level.parse(loglevel)); logger.addhandler(consolehandler); logger.setuseparenthandlers(false);

2.2) find parent consolehandler , set defined level

logger toplogger = logger.getlogger(""); handler consolehandler = null; (handler handler : toplogger.gethandlers()) { if (handler instanceof consolehandler) { //found console handler consolehandler = handler; break; } } if (consolehandler == null) { // not found, create new 1 consolehandler = new consolehandler(); toplogger.addhandler(consolehandler); } //set console handler level consolehandler.setlevel(loglevel.isempty() ? defaultlevel : level.parse(loglevel));

with that, gradle run yields messages above default level (info).

gradle run -dloglevel=fine

you can command messages shown.

java logging gradle

No comments:

Post a Comment