Add custom console logger.

Add custom console logger, move metrics to separate class, add license header, add bukkit dependencies.
This commit is contained in:
Jesse Prescott 2017-08-11 22:53:26 +01:00
parent e1135bc55c
commit 23e20f699b
6 changed files with 209 additions and 4 deletions

5
.gitignore vendored
View File

@ -20,4 +20,7 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/
/target/
#Netbeans
nb-configuration.xml

14
licenseheader.txt Normal file
View File

@ -0,0 +1,14 @@
/*
* ItemCase 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 <http://www.gnu.org/licenses/gpl.html>.
*/

12
pom.xml
View File

@ -34,6 +34,18 @@
<artifactId>bstats-bukkit</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.12.1-R0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.12.1-R0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,123 @@
/*
* ItemCase 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 <http://www.gnu.org/licenses/gpl.html>.
*/
package com.gmail.bleedobsidian.itemcase;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.bukkit.plugin.java.JavaPlugin;
import org.fusesource.jansi.Ansi;
/**
* A custom logger used to add color to the console when using warnings and errors.
*
* @author Jesse Prescott (BleedObsidian)
*/
public final class ConsoleLogger extends Logger {
/**
* The Bukkit JavaPlugin.
*/
private JavaPlugin plugin;
/**
* The plugin prefix.
*/
private final String prefix;
/**
* White color string for console.
*/
private final String whiteColor;
/**
* Yellow color string for console.
*/
private final String yellowColor;
/**
* Red color string for console.
*/
private final String redColor;
/**
* Constructor.
*
* @param plugin The B
*/
public ConsoleLogger(JavaPlugin plugin) {
// Call parent constructor.
super(plugin.getName(), null);
// Set logger settings.
super.setParent(plugin.getServer().getLogger());
super.setLevel(Level.ALL);
// Set console color strings.
this.whiteColor = Ansi.ansi().fg(Ansi.Color.WHITE).boldOff().toString();
this.yellowColor = Ansi.ansi().fg(Ansi.Color.YELLOW).bold().toString();
this.redColor = Ansi.ansi().fg(Ansi.Color.RED).bold().toString();
// Set plugin prefix.
this.prefix = "[" + plugin.getName() + "] ";
}
@Override
public void log(LogRecord logRecord) {
// Default white text.
logRecord.setMessage(this.prefix + logRecord.getMessage());
// Parent.
super.log(logRecord);
}
@Override
public void warning(String message) {
// Add color to message.
message = this.yellowColor + message + this.whiteColor;
// Log message.
this.log(Level.WARNING, message);
}
@Override
public void severe(String message) {
// Add color to message.
message = this.redColor + message + this.whiteColor;
// Log message.
this.log(Level.SEVERE, message);
}
/**
* Severe message with throwable exception.
*
* @param message The message to display.
* @param throwable The exception to display.
*/
public void severe(String message, Throwable throwable) {
// Add color to message.
message = this.redColor + message + this.whiteColor;
// Log message.
this.log(Level.SEVERE, message, throwable);
}
}

View File

@ -15,7 +15,6 @@
package com.gmail.bleedobsidian.itemcase;
import org.bstats.bukkit.Metrics;
import org.bukkit.plugin.java.JavaPlugin;
/**
@ -24,13 +23,29 @@ import org.bukkit.plugin.java.JavaPlugin;
*
* @author Jesse Prescott (BleedObsidian)
*/
public class ItemCase extends JavaPlugin {
public final class ItemCase extends JavaPlugin {
/**
* Custom plugin console logger.
*/
private ConsoleLogger consoleLogger;
@Override
public void onEnable() {
// Start metrics.
Metrics metrics = new Metrics(this);
PluginMetrics metrics = new PluginMetrics(this);
// Set custom plugin console logger.
this.consoleLogger = new ConsoleLogger(this);
}
/**
* @return Custom plugin console logger.
*/
public ConsoleLogger getConsoleLogger() {
// Custom plugin console logger.
return this.consoleLogger;
}
}

View File

@ -0,0 +1,38 @@
/*
* ItemCase 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 <http://www.gnu.org/licenses/gpl.html>.
*/
package com.gmail.bleedobsidian.itemcase;
import org.bstats.bukkit.Metrics;
import org.bukkit.plugin.java.JavaPlugin;
/**
* Handles bStats plugin metrics and any custom graphs.
*
* @author Jesse Prescott (BleedObsidian)
*/
public class PluginMetrics {
/**
* Constructor, starts bStats.
*
* @param plugin The Bukkit JavaPlugin.
*/
public PluginMetrics(JavaPlugin plugin) {
// Start bStats.
Metrics metrics = new Metrics(plugin);
}
}