Gradle Log Exit Levels

In my project classes, I used java.util.logging.Logger and added different log output in my code using different log levels, i.e.

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 some config info"); Logger logger = Logger.getLogger(Run.class.getName()); logger.log(Level.INFO, "Just logging some info"); logger.log(Level.FINE, "Fine logging"); logger.log(Level.FINER, "Finer logging"); logger.log(Level.WARNING, "This is a warning log!"); } } 

Currently, when I run gradle -i test , all log messages with the specified Level.INFO , but none of the configuration, warning, or thin messages are displayed.

I tried updating the build.gradle file so 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 turned on:

 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 

Duration:

 gradle run 

I get:

 :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :run Hello World BUILD SUCCESSFUL 

And when I start gradle -i, I get: Successfully started the process "command" / Library / Java / JavaVirtualMachines / jdk 1.8.0_20.jdk / Contents / Home / bin / java '' Hello world: run (Thread [main, 5, main]) is completed. Took 0.202 seconds.

 BUILD SUCCESSFUL 

t. no registration information. However, commenting on the properties of system.properties from the task of starting and restarting gradle -i, I 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: Just logging some info Nov 05, 2014 12:07:42 PM Run main WARNING: This is a warning log! :run (Thread[main,5,main]) completed. Took 0.229 secs. BUILD SUCCESSFUL 

Information level logs and warnings, but not subtle or small.

TL; DR;

How to get configuration logs of a thinner and more accurate level for logging into the console in a general java gradle project?

+6
source share
1 answer

Several parameters ( I personally prefer option 2.2 ):

1) Custom logging.properties file:

The Java logging API has a default logging configuration file in <JRE_HOME>/lib/logging.properties . You can use your own configuration file by setting the 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 = ALL java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter 

You must set useParentHandlers = false to avoid duplicate fingerprints of parent handlers.

1.1) Set the property above with an absolute path

Didn't even try; -)

1.2) Download the user file as follows: Run.java

Download it in Run.java as follows:

  InputStream inputStream = Run.class.getResourceAsStream("mylogging.properties"); try { LogManager.getLogManager().readConfiguration(inputStream); } catch(Exception e) { e.printStackTrace(); } 

2) Custom system property (i.e.: logLevel )

Define systemProperty in build.gradle :

 run { systemProperty 'logLevel', System.getProperty('logLevel') } 

Add defaultLogLevel to Run.java :

 public static Level defaultLevel = Level.INFO; 

Get the value of the logLevel property:

 String logLevel = System.getProperty("logLevel"); 

And set the given level in the log:

 Logger logger = Logger.getLogger(Run.class.getName()); logger.setLevel(logLevel.isEmpty() ? defaultLevel : Level.parse(logLevel)); 

2.1) Create a new ConsoleHandler and disconnect printing from 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 the parent ConsoleHandler and set a specific level

  Logger topLogger = Logger.getLogger(""); Handler consoleHandler = null; for (Handler handler : topLogger.getHandlers()) { if (handler instanceof ConsoleHandler) { //found the console handler consoleHandler = handler; break; } } if (consoleHandler == null) { // not found, create a new one consoleHandler = new ConsoleHandler(); topLogger.addHandler(consoleHandler); } //set the console handler level consoleHandler.setLevel(logLevel.isEmpty() ? defaultLevel : Level.parse(logLevel)); 

In this case, gradle run displays messages above the default level (INFO). Via

 gradle run -DlogLevel=FINE 

You can control which messages are displayed.

+9
source

Source: https://habr.com/ru/post/977424/


All Articles