summaryrefslogtreecommitdiffstats
path: root/src/main/java/org
diff options
context:
space:
mode:
authorrmichela <deltahat@gmail.com>2011-12-15 00:31:13 -0500
committerErik Broes <erikbroes@grum.nl>2012-01-17 19:45:36 +0100
commit5ca940b907ea92168775e3f27035e8661149879f (patch)
tree1e82cb232e034387ad96744100fd222245dcea4f /src/main/java/org
parentb52aa71501d59654003d6482883b6eb76524cd1d (diff)
downloadbukkit-5ca940b907ea92168775e3f27035e8661149879f.tar
bukkit-5ca940b907ea92168775e3f27035e8661149879f.tar.gz
bukkit-5ca940b907ea92168775e3f27035e8661149879f.tar.lz
bukkit-5ca940b907ea92168775e3f27035e8661149879f.tar.xz
bukkit-5ca940b907ea92168775e3f27035e8661149879f.zip
[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.
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/bukkit/plugin/Plugin.java10
-rw-r--r--src/main/java/org/bukkit/plugin/PluginLogger.java34
-rw-r--r--src/main/java/org/bukkit/plugin/java/JavaPlugin.java15
-rw-r--r--src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java6
4 files changed, 62 insertions, 3 deletions
diff --git a/src/main/java/org/bukkit/plugin/Plugin.java b/src/main/java/org/bukkit/plugin/Plugin.java
index c03fbf3b..80cb0acb 100644
--- a/src/main/java/org/bukkit/plugin/Plugin.java
+++ b/src/main/java/org/bukkit/plugin/Plugin.java
@@ -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);
diff --git a/src/main/java/org/bukkit/plugin/PluginLogger.java b/src/main/java/org/bukkit/plugin/PluginLogger.java
new file mode 100644
index 00000000..f7324211
--- /dev/null
+++ b/src/main/java/org/bukkit/plugin/PluginLogger.java
@@ -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);
+ }
+
+}
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
index 4dc638f6..5bc74be7 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
@@ -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();
+ }
}
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
index d73df9d2..423598ee 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
@@ -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;