/*
* 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 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(boolean)} 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 { // 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 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 Level CONFIG_VALUE_DEFAULT_LOG_LEVEL; private static boolean CONFIG_VALUE_SHOW_HEADER; private static boolean CONFIG_VALUE_SHOW_LOG_NAME; private static boolean CONFIG_VALUE_SHOW_SHORT_LOG_NAME; private static boolean CONFIG_VALUE_SHOW_THREAD_NAME; // Initialization lock. private static final Object INITIALIZATION_LOCK = new Object(); // serialVersionUID private static final long serialVersionUID = -2270127287235697381L; // 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; } /** * (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; BukkitPluginLoggerAdapter.BUKKIT_PLUGIN_NAME = null; } else if (BukkitPluginLoggerAdapter.BUKKIT_PLUGIN != null) { return; } // Get a reference to the plugin in this classloader. if (BukkitPluginLoggerAdapter.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); BukkitPluginLoggerAdapter.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(BukkitPluginLoggerAdapter.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_VALUE_DEFAULT_LOG_LEVEL = BukkitPluginLoggerAdapter.stringToLevel(BukkitPluginLoggerAdapter.getStringProperty(BukkitPluginLoggerAdapter.CONFIG_KEY_DEFAULT_LOG_LEVEL, BukkitPluginLoggerAdapter.CONFIG_FALLBACK_DEFAULT_LOG_LEVEL)); if (BukkitPluginLoggerAdapter.CONFIG_VALUE_DEFAULT_LOG_LEVEL == null) BukkitPluginLoggerAdapter.CONFIG_VALUE_DEFAULT_LOG_LEVEL = BukkitPluginLoggerAdapter.stringToLevel(BukkitPluginLoggerAdapter.CONFIG_FALLBACK_DEFAULT_LOG_LEVEL); BukkitPluginLoggerAdapter.CONFIG_VALUE_SHOW_HEADER = BukkitPluginLoggerAdapter.getBooleanProperty(BukkitPluginLoggerAdapter.CONFIG_KEY_SHOW_HEADER, BukkitPluginLoggerAdapter.CONFIG_FALLBACK_SHOW_HEADER); BukkitPluginLoggerAdapter.CONFIG_VALUE_SHOW_LOG_NAME = BukkitPluginLoggerAdapter.getBooleanProperty(BukkitPluginLoggerAdapter.CONFIG_KEY_SHOW_LOG_NAME, BukkitPluginLoggerAdapter.CONFIG_FALLBACK_SHOW_LOG_NAME); BukkitPluginLoggerAdapter.CONFIG_VALUE_SHOW_SHORT_LOG_NAME = BukkitPluginLoggerAdapter.getBooleanProperty(BukkitPluginLoggerAdapter.CONFIG_KEY_SHOW_SHORT_LOG_NAME, BukkitPluginLoggerAdapter.CONFIG_FALLBACK_SHOW_SHORT_LOG_NAME); BukkitPluginLoggerAdapter.CONFIG_VALUE_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); if ("true".equalsIgnoreCase(prop)) return true; if ("false".equalsIgnoreCase(prop)) return false; return defaultValue; } } /** * 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; } } private static java.util.logging.Level slf4jLevelIntToBukkitJULLevel(final Level slf4jLevel) { java.util.logging.Level julLevel; switch (slf4jLevel) { // In Bukkit, Only the SEVERE, WARNING and INFO JUL levels are enabled, so // SLF4J's TRACE and DEBUG levels must be logged at Bukkit's INFO level. case TRACE: case DEBUG: case INFO: julLevel = java.util.logging.Level.INFO; break; case WARN: julLevel = java.util.logging.Level.WARNING; break; case ERROR: julLevel = java.util.logging.Level.SEVERE; break; default: throw new IllegalStateException("Level " + slf4jLevel + " is not recognized."); } return julLevel; } /* * Logger API implementations */ private static Level stringToLevel(final String levelStr) { if ("trace".equalsIgnoreCase(levelStr)) { return Level.TRACE; } else if ("debug".equalsIgnoreCase(levelStr)) { return Level.DEBUG; } else if ("info".equalsIgnoreCase(levelStr)) { return Level.INFO; } else if ("warn".equalsIgnoreCase(levelStr)) { return Level.WARN; } else if ("error".equalsIgnoreCase(levelStr)) { return Level.ERROR; } else { return null; } } /** * A simple implementation which logs messages of level DEBUG according * to the format outlined above. */ @Override public void debug(final String msg) { if (!this.isDebugEnabled()) { return; } this.log(BukkitPluginLoggerAdapter.CLASS_SELF, 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) { if (!this.isDebugEnabled()) { return; } this.formatAndLog(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) { if (!this.isDebugEnabled()) { return; } this.formatAndLog(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) { if (!this.isDebugEnabled()) { return; } this.formatAndLog(Level.DEBUG, format, param1, param2); } /** Log a message of level DEBUG, including an exception. */ @Override public void debug(final String msg, final Throwable t) { if (!this.isDebugEnabled()) { return; } this.log(BukkitPluginLoggerAdapter.CLASS_SELF, 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) { if (!this.isErrorEnabled()) { return; } this.log(BukkitPluginLoggerAdapter.CLASS_SELF, 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) { if (!this.isErrorEnabled()) { return; } this.formatAndLog(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) { if (!this.isErrorEnabled()) { return; } this.formatAndLog(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) { if (!this.isErrorEnabled()) { return; } this.formatAndLog(Level.ERROR, format, arg1, arg2); } /** Log a message of level ERROR, including an exception. */ @Override public void error(final String msg, final Throwable t) { if (!this.isErrorEnabled()) { return; } this.log(BukkitPluginLoggerAdapter.CLASS_SELF, 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) { if (!this.isInfoEnabled()) { return; } this.log(BukkitPluginLoggerAdapter.CLASS_SELF, 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) { if (!this.isInfoEnabled()) { return; } this.formatAndLog(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) { if (!this.isInfoEnabled()) { return; } this.formatAndLog(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) { if (!this.isInfoEnabled()) { return; } this.formatAndLog(Level.INFO, format, arg1, arg2); } /** Log a message of level INFO, including an exception. */ @Override public void info(final String msg, final Throwable t) { if (!this.isInfoEnabled()) { return; } this.log(BukkitPluginLoggerAdapter.CLASS_SELF, Level.INFO, msg, t); } /** Are {@code debug} messages currently enabled? */ @Override public boolean isDebugEnabled() { return this.isLevelEnabled(Level.DEBUG); } /** Are {@code error} messages currently enabled? */ @Override public boolean isErrorEnabled() { return this.isLevelEnabled(Level.ERROR); } /** Are {@code info} messages currently enabled? */ @Override public boolean isInfoEnabled() { return this.isLevelEnabled(Level.INFO); } /** Are {@code trace} messages currently enabled? */ @Override public boolean isTraceEnabled() { return this.isLevelEnabled(Level.TRACE); } /** Are {@code warn} messages currently enabled? */ @Override public boolean isWarnEnabled() { return this.isLevelEnabled(Level.WARN); } /** * A simple implementation which logs messages of level TRACE according * to the format outlined above. */ @Override public void trace(final String msg) { if (!this.isTraceEnabled()) { return; } this.log(BukkitPluginLoggerAdapter.CLASS_SELF, 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) { if (!this.isTraceEnabled()) { return; } this.formatAndLog(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) { if (!this.isTraceEnabled()) { return; } this.formatAndLog(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) { if (!this.isTraceEnabled()) { return; } this.formatAndLog(Level.TRACE, format, param1, param2); } /** Log a message of level TRACE, including an exception. */ @Override public void trace(final String msg, final Throwable t) { if (!this.isTraceEnabled()) { return; } this.log(BukkitPluginLoggerAdapter.CLASS_SELF, 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) { if (!this.isWarnEnabled()) { return; } this.log(BukkitPluginLoggerAdapter.CLASS_SELF, 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) { if (!this.isWarnEnabled()) { return; } this.formatAndLog(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) { if (!this.isWarnEnabled()) { return; } this.formatAndLog(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) { if (!this.isWarnEnabled()) { return; } this.formatAndLog(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) { if (!this.isWarnEnabled()) { return; } this.log(BukkitPluginLoggerAdapter.CLASS_SELF, Level.WARN, msg, t); } private String computeShortName() { final List