001 package org.bukkit.plugin;
002
003 import java.util.logging.Level;
004 import java.util.logging.LogRecord;
005 import java.util.logging.Logger;
006
007 /**
008 * The PluginLogger class is a modified {@link Logger} that prepends all
009 * logging calls with the name of the plugin doing the logging. The API for
010 * PluginLogger is exactly the same as {@link Logger}.
011 *
012 * @see Logger
013 */
014 public class PluginLogger extends Logger {
015 private String pluginName;
016
017 /**
018 * Creates a new PluginLogger that extracts the name from a plugin.
019 *
020 * @param context A reference to the plugin
021 */
022 public PluginLogger(Plugin context) {
023 super(context.getClass().getCanonicalName(), null);
024 String prefix = context.getDescription().getPrefix();
025 pluginName = prefix != null ? new StringBuilder().append("[").append(prefix).append("] ").toString() : "[" + context.getDescription().getName() + "] ";
026 setParent(context.getServer().getLogger());
027 setLevel(Level.ALL);
028 }
029
030 @Override
031 public void log(LogRecord logRecord) {
032 logRecord.setMessage(pluginName + logRecord.getMessage());
033 super.log(logRecord);
034 }
035
036 }