/*
* This entire file is sublicensed to you under GPLv3 or (at your option) any
* later version. The original copyright notice is retained below.
*/
/*
* Portions of this file are
* Copyright (C) 2016 Ronald Jack Jenkins Jr.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* A merger of SLF4J's {@code SimpleLogger} and {@code JDK14LoggerAdapter}, * wired to log all messages to the enclosing Bukkit plugin. The plugin is * identified by reading the "name" attribute from {@code plugin.yml} in the * current classloader. *
* ** Plugins that include SLF4Bukkit can use the following values in * {@code config.yml} to configure the behavior of SLF4Bukkit. SLF4Bukkit uses * Bukkit's plugin configuration API to retrieve config values, so both on-disk * and built-in {@code config.yml} behavior is supported. *
* *slf4j.defaultLogLevel - Default log level for all SLF4Bukkit
* loggers in this plugin. Must be one of "trace", "debug", "info", "warn", or
* "error". Defaults to "info".slf4j.log.a.b.c - Logging detail level for an
* SLF4Bukkit logger instance in this plugin named "a.b.c". Right-side value
* must be one of "trace", "debug", "info", "warn", or "error". When a logger
* named "a.b.c" is initialized, its level is assigned from this property. If
* unspecified, the level of the nearest parent logger will be used. If no
* parent logger level is set, then the value specified by
* slf4j.defaultLogLevel for this plugin will be used.slf4j.showHeader -Set to true if you want to
* output the {@code [SLF4J]} header. Defaults to false.slf4j.showThreadName -Set to true if you want
* to output the current thread name. Defaults to false.slf4j.showLogName - Set to true if you want the
* logger instance name to be included in output messages. Defaults to
* false.slf4j.showShortLogName - Set to true if you
* want the logger instance's short name to be included in output messages. The
* short name is equal to the full name with every dot-separated portion of the
* full name (except the last portion) truncated to its first character.
* Defaults to true.* SLF4J messages at level {@code TRACE} or {@code DEBUG} are logged to Bukkit * at level {@code INFO} because Bukkit does not enable any levels higher than * {@code INFO}. Therefore, only SLF4J messages at level {@code TRACE} or * {@code DEBUG} show their SLF4J level in the message that is logged to the * server console. *
* ** Because SLF4Bukkit's configuration comes from the plugin configuration, * SLF4Bukkit supports configuration reloading. To achieve this, call * {@link #init()} with argument {@code true} after calling * {@link Plugin#reloadConfig()}. *
* ** It is possible for SLF4J loggers to be used before the plugin is registered * with Bukkit's plugin manager. SLF4Bukkit is considered to be * uninitialized as long as the plugin cannot be retrieved from Bukkit's * plugin manager. While in the uninitialized state, SLF4Bukkit: *
* ** For this reason, it is strongly recommended that you not emit any log * messages via SLF4Bukkit until your plugin's {@link Plugin#onLoad() onLoad()} * method has begun execution. (You can safely log messages inside the * {@code onLoad()} method, because your plugin is registered by that time.) * Logging inside static initializers, the plugin class constructor and other * pre-plugin-registration areas of your code is discouraged. *
* ** With no configuration, the default output includes the logger short name and * the message, followed by the line separator for the host. *
* * @author Ceki Gülcü * @author Scott Sanders * @author Rod Waldhoff * @author Robert Burrell Donkin * @author Cédrik LIME * @author Peter Royal * @author Ronald Jack Jenkins Jr. */ public final class BukkitPluginLoggerAdapter extends MarkerIgnoringBase implements LocationAwareLogger { // Plugin reference. private static transient Plugin BUKKIT_PLUGIN; private static transient String BUKKIT_PLUGIN_NAME; // Constants for JUL record creation. private static final String CLASS_SELF = BukkitPluginLoggerAdapter.class.getName(); private static final String CLASS_SUPER = MarkerIgnoringBase.class.getName(); // Configuration parameters. private static int CONFIG_DEFAULT_LOG_LEVEL; private static final String CONFIG_FALLBACK_DEFAULT_LOG_LEVEL = "info"; private static final boolean CONFIG_FALLBACK_SHOW_HEADER = false; private static final boolean CONFIG_FALLBACK_SHOW_LOG_NAME = false; private static final boolean CONFIG_FALLBACK_SHOW_SHORT_LOG_NAME = true; private static final boolean CONFIG_FALLBACK_SHOW_THREAD_NAME = false; private static final String CONFIG_KEY_DEFAULT_LOG_LEVEL = "slf4j.defaultLogLevel"; private static final String CONFIG_KEY_PREFIX_LOG = "slf4j.log."; private static final String CONFIG_KEY_SHOW_HEADER = "slf4j.showHeader"; private static final String CONFIG_KEY_SHOW_LOG_NAME = "slf4j.showLogName"; private static final String CONFIG_KEY_SHOW_SHORT_LOG_NAME = "slf4j.showShortLogName"; private static final String CONFIG_KEY_SHOW_THREAD_NAME = "slf4j.showThreadName"; private static boolean CONFIG_SHOW_HEADER; private static boolean CONFIG_SHOW_LOG_NAME; private static boolean CONFIG_SHOW_SHORT_LOG_NAME; private static boolean CONFIG_SHOW_THREAD_NAME; // Initialization lock. private static final Object INITIALIZATION_LOCK = new Object(); // Logging level constants. private static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT; private static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT; private static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT; private static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT; private static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT; // serialVersionUID private static final long serialVersionUID = -2270127287235697381L; /** The current log level */ protected int currentLogLevel = BukkitPluginLoggerAdapter.LOG_LEVEL_INFO; /** The short name of this simple log instance */ private transient String shortLogName = null; // NOTE: BukkitPluginLoggerAdapter constructor should have only package access // so that only BukkitPluginLoggerFactory be able to create one. BukkitPluginLoggerAdapter(final String name) { this.name = name; final String levelString = this.recursivelyComputeLevelString(); if (levelString != null) { this.currentLogLevel = BukkitPluginLoggerAdapter.stringToLevel(levelString); } else { this.currentLogLevel = BukkitPluginLoggerAdapter.CONFIG_DEFAULT_LOG_LEVEL; } } /** * (Re)initializes all SLF4Bukkit loggers, relying on the YAML configuration * of the enclosing plugin. * * @param reinitialize * set to {@code true} to reinitialize all loggers, e.g. after * reloading the plugin config. */ public static void init(final boolean reinitialize) { synchronized (BukkitPluginLoggerAdapter.INITIALIZATION_LOCK) { // Do not re-initialize unless requested. if (reinitialize) { BukkitPluginLoggerAdapter.BUKKIT_PLUGIN = null; BUKKIT_PLUGIN_NAME = null; } else if (BukkitPluginLoggerAdapter.BUKKIT_PLUGIN != null) { return; } // Get a reference to the plugin in this classloader. if (BUKKIT_PLUGIN_NAME == null) { InputStream pluginYmlFile = null; try { pluginYmlFile = BukkitPluginLoggerAdapter.class.getClassLoader() .getResource("plugin.yml") .openStream(); final Yaml yaml = new Yaml(); @SuppressWarnings("rawtypes") final Map pluginYml = (Map) yaml.load(pluginYmlFile); BUKKIT_PLUGIN_NAME = (String) pluginYml.get("name"); } catch (final IOException e) { throw new IllegalStateException(e); } finally { if (pluginYmlFile != null) { try { pluginYmlFile.close(); } catch (final IOException e) { e.printStackTrace(); } } } } // Try to get the plugin. The logging system will be considered // uninitialized until this becomes non-null. While it is null, the Bukkit // server logger will be used instead of the plugin logger, and all // default configuration options will be used. BukkitPluginLoggerAdapter.BUKKIT_PLUGIN = Bukkit.getPluginManager() .getPlugin(BUKKIT_PLUGIN_NAME); // Get the configuration values. // 1. Look in the plugin's on-disk config. // 2. If the value is absent, use the plugin's built-in config. // 3. If the value is absent, use the default values hardcoded above. // (1 and 2 are handled by using the Bukkit API.) BukkitPluginLoggerAdapter.CONFIG_DEFAULT_LOG_LEVEL = BukkitPluginLoggerAdapter.stringToLevel(BukkitPluginLoggerAdapter.getStringProperty(BukkitPluginLoggerAdapter.CONFIG_KEY_DEFAULT_LOG_LEVEL, BukkitPluginLoggerAdapter.CONFIG_FALLBACK_DEFAULT_LOG_LEVEL)); BukkitPluginLoggerAdapter.CONFIG_SHOW_HEADER = BukkitPluginLoggerAdapter.getBooleanProperty(BukkitPluginLoggerAdapter.CONFIG_KEY_SHOW_HEADER, BukkitPluginLoggerAdapter.CONFIG_FALLBACK_SHOW_HEADER); BukkitPluginLoggerAdapter.CONFIG_SHOW_LOG_NAME = BukkitPluginLoggerAdapter.getBooleanProperty(BukkitPluginLoggerAdapter.CONFIG_KEY_SHOW_LOG_NAME, BukkitPluginLoggerAdapter.CONFIG_FALLBACK_SHOW_LOG_NAME); BukkitPluginLoggerAdapter.CONFIG_SHOW_SHORT_LOG_NAME = BukkitPluginLoggerAdapter.getBooleanProperty(BukkitPluginLoggerAdapter.CONFIG_KEY_SHOW_SHORT_LOG_NAME, BukkitPluginLoggerAdapter.CONFIG_FALLBACK_SHOW_SHORT_LOG_NAME); BukkitPluginLoggerAdapter.CONFIG_SHOW_THREAD_NAME = BukkitPluginLoggerAdapter.getBooleanProperty(BukkitPluginLoggerAdapter.CONFIG_KEY_SHOW_THREAD_NAME, BukkitPluginLoggerAdapter.CONFIG_FALLBACK_SHOW_THREAD_NAME); } } private static boolean getBooleanProperty(final String name, final boolean defaultValue) { synchronized (BukkitPluginLoggerAdapter.INITIALIZATION_LOCK) { if (BukkitPluginLoggerAdapter.BUKKIT_PLUGIN == null) { return defaultValue; } final String prop = BukkitPluginLoggerAdapter.BUKKIT_PLUGIN.getConfig() .getString(name); return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop); } } /** * Returns the most appropriate logger. * * @return the logger for the plugin if available; otherwise the server * logger. Never null. */ private static Logger getBukkitLogger() { synchronized (BukkitPluginLoggerAdapter.INITIALIZATION_LOCK) { return BukkitPluginLoggerAdapter.BUKKIT_PLUGIN == null ? Bukkit.getLogger() : BukkitPluginLoggerAdapter.BUKKIT_PLUGIN.getLogger(); } } private static String getStringProperty(final String name, final String defaultValue) { synchronized (BukkitPluginLoggerAdapter.INITIALIZATION_LOCK) { if (BukkitPluginLoggerAdapter.BUKKIT_PLUGIN == null) { return defaultValue; } final String prop = BukkitPluginLoggerAdapter.BUKKIT_PLUGIN.getConfig() .getString(name); return (prop == null) ? defaultValue : prop; } } /* * Logger API implementations */ private static int stringToLevel(final String levelStr) { if ("trace".equalsIgnoreCase(levelStr)) { return BukkitPluginLoggerAdapter.LOG_LEVEL_TRACE; } else if ("debug".equalsIgnoreCase(levelStr)) { return BukkitPluginLoggerAdapter.LOG_LEVEL_DEBUG; } else if ("info".equalsIgnoreCase(levelStr)) { return BukkitPluginLoggerAdapter.LOG_LEVEL_INFO; } else if ("warn".equalsIgnoreCase(levelStr)) { return BukkitPluginLoggerAdapter.LOG_LEVEL_WARN; } else if ("error".equalsIgnoreCase(levelStr)) { return BukkitPluginLoggerAdapter.LOG_LEVEL_ERROR; } else { return BukkitPluginLoggerAdapter.LOG_LEVEL_INFO; } } /** * A simple implementation which logs messages of level DEBUG according * to the format outlined above. */ @Override public void debug(final String msg) { this.log(BukkitPluginLoggerAdapter.CLASS_SELF, BukkitPluginLoggerAdapter.LOG_LEVEL_DEBUG, msg, null); } /** * Perform single parameter substitution before logging the message of level * DEBUG according to the format outlined above. */ @Override public void debug(final String format, final Object param1) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_DEBUG, format, param1, null); } /** * Perform double parameter substitution before logging the message of level * DEBUG according to the format outlined above. */ @Override public void debug(final String format, final Object... argArray) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_DEBUG, format, argArray); } /** * Perform double parameter substitution before logging the message of level * DEBUG according to the format outlined above. */ @Override public void debug(final String format, final Object param1, final Object param2) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_DEBUG, format, param1, param2); } /** Log a message of level DEBUG, including an exception. */ @Override public void debug(final String msg, final Throwable t) { this.log(BukkitPluginLoggerAdapter.CLASS_SELF, BukkitPluginLoggerAdapter.LOG_LEVEL_DEBUG, msg, t); } /** * A simple implementation which always logs messages of level ERROR according * to the format outlined above. */ @Override public void error(final String msg) { this.log(BukkitPluginLoggerAdapter.CLASS_SELF, BukkitPluginLoggerAdapter.LOG_LEVEL_ERROR, msg, null); } /** * Perform single parameter substitution before logging the message of level * ERROR according to the format outlined above. */ @Override public void error(final String format, final Object arg) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_ERROR, format, arg, null); } /** * Perform double parameter substitution before logging the message of level * ERROR according to the format outlined above. */ @Override public void error(final String format, final Object... argArray) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_ERROR, format, argArray); } /** * Perform double parameter substitution before logging the message of level * ERROR according to the format outlined above. */ @Override public void error(final String format, final Object arg1, final Object arg2) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_ERROR, format, arg1, arg2); } /** Log a message of level ERROR, including an exception. */ @Override public void error(final String msg, final Throwable t) { this.log(BukkitPluginLoggerAdapter.CLASS_SELF, BukkitPluginLoggerAdapter.LOG_LEVEL_ERROR, msg, t); } /** * A simple implementation which logs messages of level INFO according * to the format outlined above. */ @Override public void info(final String msg) { this.log(BukkitPluginLoggerAdapter.CLASS_SELF, BukkitPluginLoggerAdapter.LOG_LEVEL_INFO, msg, null); } /** * Perform single parameter substitution before logging the message of level * INFO according to the format outlined above. */ @Override public void info(final String format, final Object arg) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_INFO, format, arg, null); } /** * Perform double parameter substitution before logging the message of level * INFO according to the format outlined above. */ @Override public void info(final String format, final Object... argArray) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_INFO, format, argArray); } /** * Perform double parameter substitution before logging the message of level * INFO according to the format outlined above. */ @Override public void info(final String format, final Object arg1, final Object arg2) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_INFO, format, arg1, arg2); } /** Log a message of level INFO, including an exception. */ @Override public void info(final String msg, final Throwable t) { this.log(BukkitPluginLoggerAdapter.CLASS_SELF, BukkitPluginLoggerAdapter.LOG_LEVEL_INFO, msg, t); } /** Are {@code debug} messages currently enabled? */ @Override public boolean isDebugEnabled() { return this.isLevelEnabled(BukkitPluginLoggerAdapter.LOG_LEVEL_DEBUG); } /** Are {@code error} messages currently enabled? */ @Override public boolean isErrorEnabled() { return this.isLevelEnabled(BukkitPluginLoggerAdapter.LOG_LEVEL_ERROR); } /** Are {@code info} messages currently enabled? */ @Override public boolean isInfoEnabled() { return this.isLevelEnabled(BukkitPluginLoggerAdapter.LOG_LEVEL_INFO); } /** Are {@code trace} messages currently enabled? */ @Override public boolean isTraceEnabled() { return this.isLevelEnabled(BukkitPluginLoggerAdapter.LOG_LEVEL_TRACE); } /** Are {@code warn} messages currently enabled? */ @Override public boolean isWarnEnabled() { return this.isLevelEnabled(BukkitPluginLoggerAdapter.LOG_LEVEL_WARN); } /** * Location-aware logging capability. The marker and argArray are ignored. */ @Override public void log(final Marker marker, final String callerFQCN, final int level, final String message, final Object[] argArray, final Throwable t) { if (!this.isLevelEnabled(level)) { return; } this.log(callerFQCN, level, message, t); } /** * A simple implementation which logs messages of level TRACE according * to the format outlined above. */ @Override public void trace(final String msg) { this.log(BukkitPluginLoggerAdapter.CLASS_SELF, BukkitPluginLoggerAdapter.LOG_LEVEL_TRACE, msg, null); } /** * Perform single parameter substitution before logging the message of level * TRACE according to the format outlined above. */ @Override public void trace(final String format, final Object param1) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_TRACE, format, param1, null); } /** * Perform double parameter substitution before logging the message of level * TRACE according to the format outlined above. */ @Override public void trace(final String format, final Object... argArray) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_TRACE, format, argArray); } /** * Perform double parameter substitution before logging the message of level * TRACE according to the format outlined above. */ @Override public void trace(final String format, final Object param1, final Object param2) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_TRACE, format, param1, param2); } /** Log a message of level TRACE, including an exception. */ @Override public void trace(final String msg, final Throwable t) { this.log(BukkitPluginLoggerAdapter.CLASS_SELF, BukkitPluginLoggerAdapter.LOG_LEVEL_TRACE, msg, t); } /** * A simple implementation which always logs messages of level WARN according * to the format outlined above. */ @Override public void warn(final String msg) { this.log(BukkitPluginLoggerAdapter.CLASS_SELF, BukkitPluginLoggerAdapter.LOG_LEVEL_WARN, msg, null); } /** * Perform single parameter substitution before logging the message of level * WARN according to the format outlined above. */ @Override public void warn(final String format, final Object arg) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_WARN, format, arg, null); } /** * Perform double parameter substitution before logging the message of level * WARN according to the format outlined above. */ @Override public void warn(final String format, final Object... argArray) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_WARN, format, argArray); } /** * Perform double parameter substitution before logging the message of level * WARN according to the format outlined above. */ @Override public void warn(final String format, final Object arg1, final Object arg2) { this.formatAndLog(BukkitPluginLoggerAdapter.LOG_LEVEL_WARN, format, arg1, arg2); } /* * Logic from SimpleLogger/JDK14LoggerAdapter */ /** Log a message of level WARN, including an exception. */ @Override public void warn(final String msg, final Throwable t) { this.log(BukkitPluginLoggerAdapter.CLASS_SELF, BukkitPluginLoggerAdapter.LOG_LEVEL_WARN, msg, t); } private String computeShortName() { final List