001/*
002 * Copyright 2019-2021 M. Sean Gilligan.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package app.supernaut.logging;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.logging.Level;
021import java.util.logging.LogManager;
022import java.util.logging.Logger;
023
024/**
025 * GraalVM-compatible Support for using Java Logging
026 * See: https://github.com/oracle/graal/blob/master/substratevm/LOGGING.md
027 *
028 * The default log-level for command-line tools configured in logging.properties
029 * should be `WARNING`. The `-v` command-line switch should set the level to `FINE`.
030 * Request-logging in RpcClient is at the `FINE` (slf4j `debug`) level.
031 *
032 * TODO: Create a command-line option to set finer-grained log levels
033 */
034public class JavaLoggingSupport {
035    /** The default path of the logging properties file */
036    public static final String DEFAULT_PROPERTIES_PATH = "/logging.properties";
037    private static String loggerName;
038
039    /**
040     * Configure logging.
041     * Should be one of the first things called in `main()`
042     *
043     * @param rootClass root class for calling {@code }getResourceAsStream()}
044     * @param loggingPropertiesResource Path to logging configuration properties resource file
045     * @param loggerName The logger name
046     */
047    public static void configure(Class<?> rootClass, String loggingPropertiesResource, String loggerName) {
048        InputStream inputStream = rootClass.getResourceAsStream(loggingPropertiesResource);
049        if (inputStream != null) {
050            try {
051                LogManager.getLogManager().readConfiguration(inputStream);
052            } catch (IOException e) {
053                e.printStackTrace();
054                System.err.println("JavaLoggingSupport: failed to process " + loggingPropertiesResource);
055            }
056        } else {
057            System.err.println("JavaLoggingSupport: failed to load " + loggingPropertiesResource);
058        }
059        JavaLoggingSupport.loggerName = loggerName;
060    }
061
062    /**
063     * Configure logging using default resource file/path
064     *
065     * @param rootClass root class for calling {@code }getResourceAsStream()}
066     * @param loggerName The logger name
067     */
068    public static void configure(Class<?> rootClass, String loggerName) {
069        configure(rootClass, DEFAULT_PROPERTIES_PATH, loggerName);
070    }
071
072    /**
073     * Change log level (eg. as a result of `-v` command-line option)
074     */
075    public static void setVerbose() {
076        final Logger app = Logger.getLogger(loggerName);
077        app.setLevel(Level.FINE);
078    }
079}