的src /主/爪哇/ 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!"); } }
目前,当我运行gradle -i test时,会显示所有已定义Level.INFO的日志消息,但不输出config,warn或fine消息.
我已经尝试更新我的build.gradle文件,以便:
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'] }
我已经包括:
systemProperties = ['java.util.logging.config.file' : 'logging.properties']
然后创建/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
运行:
gradle run
我明白了:
:compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :run Hello World BUILD SUCCESSFUL
当运行gradle -i run时,我得到:
成功启动进程’command’/ Library / Java / JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/bin/java”
你好,世界
:run(Thread [main,5,main])完成.花了0.202秒.
BUILD SUCCESSFUL
即.没有记录信息.但是,从运行任务中注释掉system.properties并重新运行gradle -i run我得到:
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,main]) completed. Took 0.229 secs. BUILD SUCCESSFUL
信息和警告级别日志,但不是精细或更精细的日志.
tldr;
如何获得配置,精细和&更精细的日志记录到通用gradle java项目中的控制台?
解决方法
Java Logging API在< JRE_HOME> /lib/logging.properties上有一个默认的日志记录配置文件.您可以使用自己的配置文件设置JVM属性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
您必须设置useParentHandlers = false以避免从父处理程序获取重复打印.
甚至没试过;-)
在Run.java中按如下方式加载它:
InputStream inputStream = Run.class.getResourceAsStream("mylogging.properties"); try { LogManager.getLogManager().readConfiguration(inputStream); } catch(Exception e) { e.printStackTrace(); }
在build.gradle中定义systemProperty:
run { systemProperty 'logLevel',System.getProperty('logLevel') }
在Run.java中添加defaultLogLevel:
public static Level defaultLevel = Level.INFO;
String logLevel = System.getProperty("logLevel");
并在logger中设置定义的级别:
Logger logger = Logger.getLogger(Run.class.getName()); logger.setLevel(logLevel.isEmpty() ? defaultLevel : Level.parse(logLevel));
2.1)从父处理程序创建一个新的ConsoleHandler并关闭打印
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)查找父ConsoleHandler并设置定义的级别
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));
这样,gradle run会产生高于默认级别(INFO)的消息.同
gradle run -DlogLevel=FINE
您可以控制显示哪些消息.