13
0
geforkt von Mirrors/Paper

[Bleeding] Added Plugin.getLogger() which returns a java.util.Logger that prefixes messages with the plugin name.

Note: the server when enabling or disabling a plugin will now by default log this.

By: rmichela <deltahat@gmail.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2011-12-15 00:31:13 -05:00
Ursprung 81c2625a0e
Commit c02d8155ad
5 geänderte Dateien mit 67 neuen und 3 gelöschten Zeilen

Datei anzeigen

@ -3,6 +3,8 @@ package org.bukkit.plugin;
import com.avaje.ebean.EbeanServer;
import java.io.File;
import java.io.InputStream;
import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.command.CommandExecutor;
import org.bukkit.configuration.file.FileConfiguration;
@ -149,6 +151,14 @@ public interface Plugin extends CommandExecutor {
*/
public ChunkGenerator getDefaultWorldGenerator(String worldName, String id);
/**
* Returns the primary logger associated with this server instance. The returned logger automatically
* tags all log messages with the plugin's name.
*
* @return Logger associated with this server
*/
public Logger getLogger();
public long getTiming(Event.Type type);
public void incTiming(Event.Type type, long delta);

Datei anzeigen

@ -0,0 +1,34 @@
package org.bukkit.plugin;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
/**
* The PluginLogger class is a modified {@link java.util.logging.Logger} that prepends all logging calls with the name of the
* plugin doing the logging.
*
* The API for PluginLogger is exactly the same as {@link java.util.logging.Logger}.
*/
public class PluginLogger extends Logger {
private String pluginName;
/**
* Creates a new PluginLogger that extracts the name from a plugin.
* @param context
*/
public PluginLogger(Plugin context) {
super(context.getClass().getCanonicalName(), null);
pluginName = "[" + context.getDescription().getName() + "] ";
setParent(context.getServer().getLogger());
setLevel(Level.ALL);
}
@Override
public void log(LogRecord logRecord) {
logRecord.setMessage(pluginName + logRecord.getMessage());
super.log(logRecord);
}
}

Datei anzeigen

@ -25,6 +25,7 @@ import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginLogger;
import org.bukkit.util.config.Configuration;
/**
@ -45,6 +46,7 @@ public abstract class JavaPlugin implements Plugin {
private FileConfiguration newConfig = null;
private File configFile = null;
private long[] timings = new long[Event.Type.values().length];
private PluginLogger logger = null;
public JavaPlugin() {}
@ -365,9 +367,11 @@ public abstract class JavaPlugin implements Plugin {
gen.runScript(true, gen.generateDropDdl());
}
@Override
public String toString() {
return getDescription().getFullName();
public Logger getLogger() {
if (logger == null) {
logger = new PluginLogger(this);
}
return logger;
}
public long getTiming(Event.Type type) {
@ -381,4 +385,9 @@ public abstract class JavaPlugin implements Plugin {
public void resetTimings() {
timings = new long[Event.Type.values().length];
}
@Override
public String toString() {
return getDescription().getFullName();
}
}

Datei anzeigen

@ -977,6 +977,9 @@ public class JavaPluginLoader implements PluginLoader {
}
if (!plugin.isEnabled()) {
String message = String.format("[%s] Loading %s.", plugin.getDescription().getName(), plugin.getDescription().getFullName());
server.getLogger().info(message);
JavaPlugin jPlugin = (JavaPlugin) plugin;
String pluginName = jPlugin.getDescription().getName();
@ -1003,6 +1006,9 @@ public class JavaPluginLoader implements PluginLoader {
}
if (plugin.isEnabled()) {
String message = String.format("[%s] Unloading %s.", plugin.getDescription().getName(), plugin.getDescription().getFullName());
server.getLogger().info(message);
server.getPluginManager().callEvent(new PluginDisableEvent(plugin));
JavaPlugin jPlugin = (JavaPlugin) plugin;

Datei anzeigen

@ -12,6 +12,7 @@ import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginLogger;
import org.bukkit.util.config.Configuration;
public class TestPlugin implements Plugin {
@ -57,6 +58,10 @@ public class TestPlugin implements Plugin {
throw new UnsupportedOperationException("Not supported.");
}
public PluginLogger getLogger() {
throw new UnsupportedOperationException("Not supported.");
}
public PluginLoader getPluginLoader() {
throw new UnsupportedOperationException("Not supported.");
}