001/*
002 * Copyright (C) 2023-present The Prometheus jmx_exporter Authors
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 */
016
017package io.prometheus.jmx.logger;
018
019import java.util.logging.Level;
020
021/** Class to implement a Logger */
022public class Logger {
023
024    private final java.util.logging.Logger LOGGER;
025
026    private final boolean JMX_PROMETHEUS_EXPORTER_DEVELOPER_DEBUG =
027            "true".equals(System.getenv("JMX_PROMETHEUS_EXPORTER_DEVELOPER_DEBUG"))
028                    || "true".equals(System.getProperty("jmx.prometheus.exporter.developer.debug"));
029
030    /**
031     * Constructor
032     *
033     * @param clazz clazz
034     */
035    Logger(Class<?> clazz) {
036        LOGGER = java.util.logging.Logger.getLogger(clazz.getName());
037    }
038
039    /**
040     * Method to return whether a log level is enabled
041     *
042     * @param level level
043     * @return true if the log level is enabled, else false
044     */
045    public boolean isLoggable(Level level) {
046        return LOGGER.isLoggable(level);
047    }
048
049    /**
050     * Method to log a message
051     *
052     * @param level level
053     * @param message message
054     * @param objects objects
055     */
056    public void log(Level level, String message, Object... objects) {
057        if (LOGGER.isLoggable(level)) {
058            LOGGER.log(level, String.format(message, objects));
059        }
060
061        if (JMX_PROMETHEUS_EXPORTER_DEVELOPER_DEBUG) {
062            System.out
063                    .format("[%s] %s %s", level, LOGGER.getName(), String.format(message, objects))
064                    .println();
065        }
066    }
067}