001package org.consensusj.jsonrpc.cli; 002 003import java.io.IOException; 004import java.io.InputStream; 005import java.util.logging.Level; 006import java.util.logging.LogManager; 007import java.util.logging.Logger; 008 009// TODO: Create a command-line option to set finer-grained log levels 010/** 011 * GraalVM-compatible support for using Java Logging. 012 * <p> 013 * See: <a href="https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/guides/add-logging-to-native-executable.md">add-logging-to-native-executable.md"LOGGING.md</a> 014 * <p> 015 * The default log-level for command-line tools configured in {@code logging.properties} should be {@link Level#WARNING}. 016 * The {@code -v} command-line switch should set the level to {@link Level#FINE}. 017 * Request-logging in {@link org.consensusj.jsonrpc.JsonRpcClientHttpUrlConnection} is at the {@link Level#FINE} (slf4j {@code debug}) level. 018 */ 019public class JavaLoggingSupport { 020 private final static String loggingPropertiesResource = "/logging.properties"; 021 private static String loggerName = ""; 022 023 /** 024 * Configure logging. 025 * Should be one of the first things called in `main()` 026 */ 027 public static void configure(String loggerName) { 028 InputStream inputStream = GenericJsonRpcTool.class.getResourceAsStream(loggingPropertiesResource); 029 if (inputStream != null) { 030 try { 031 LogManager.getLogManager().readConfiguration(inputStream); 032 } catch (IOException e) { 033 e.printStackTrace(); 034 System.err.println("JavaLoggingSupport: failed to process " + loggingPropertiesResource); 035 } 036 } else { 037 System.err.println("JavaLoggingSupport: failed to load " + loggingPropertiesResource); 038 } 039 JavaLoggingSupport.loggerName = loggerName; 040 } 041 042 /** 043 * Change log level (eg. as a result of `-v` command-line option) 044 * This sets the level to {@link Level#FINE} 045 */ 046 public static void setVerbose() { 047 setLogLevel(Level.FINE); 048 } 049 050 /** 051 * Change log level (eg. as a result of `-log=level` command-line option) 052 * @param level j.u.logging log level 053 */ 054 public static void setLogLevel(Level level) { 055 final Logger app = Logger.getLogger(loggerName); 056 app.setLevel(level); 057 } 058 059}