Geyser/connector/src/main/java/org/geysermc/connector/console/GeyserLogger.java

136 lines
4.9 KiB
Java
Raw Normal View History

2019-07-08 17:55:14 +00:00
/*
2019-07-11 21:30:35 +00:00
* Copyright (c) 2019 GeyserMC. http://geysermc.org
2019-07-08 17:55:14 +00:00
*
2019-07-11 21:30:35 +00:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2019-07-08 17:55:14 +00:00
*
2019-07-11 21:30:35 +00:00
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
2019-07-08 17:55:14 +00:00
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.console;
import io.sentry.Sentry;
import org.geysermc.api.ChatColor;
2019-07-08 17:55:14 +00:00
import java.io.File;
import java.io.IOException;
2019-07-08 17:55:14 +00:00
import java.util.Date;
2019-07-13 19:00:51 +00:00
import java.util.logging.*;
2019-07-08 17:55:14 +00:00
2019-07-08 23:22:50 +00:00
public class GeyserLogger implements org.geysermc.api.logger.Logger {
2019-07-08 17:55:14 +00:00
private boolean colored = true;
private boolean debug = false;
2019-07-08 17:55:14 +00:00
2019-07-13 19:00:51 +00:00
public static final GeyserLogger DEFAULT = new GeyserLogger();
2019-07-08 17:55:14 +00:00
2019-07-13 19:00:51 +00:00
private GeyserLogger() {
2019-07-08 17:55:14 +00:00
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.INFO);
consoleHandler.setFormatter(new SimpleFormatter() {
private static final String format = "[%1$tT][%2$-5s] %3$s %n";
@Override
public synchronized String format(LogRecord lr) {
return String.format(format,
new Date(lr.getMillis()),
lr.getLevel().getLocalizedName(),
lr.getMessage()
);
}
});
try {
File logDir = new File("logs");
logDir.mkdir();
File logFile = new File(logDir, "latest.log");
int maxLogFileSize = 20;//Mo
if (logFile.exists() && (logFile.length()) > maxLogFileSize * 1024L * 1024L)
2019-07-13 19:00:51 +00:00
this.warning("Your log file is larger than " + maxLogFileSize + "Mo, you should backup and clean it !");
2019-07-08 17:55:14 +00:00
FileHandler fileHandler = new FileHandler(logFile.getCanonicalPath(), true);
fileHandler.setLevel(Level.INFO);
fileHandler.setFormatter(new SimpleFormatter() {
private static final String format = "[%1$tF %1$tT][%2$-5s] %3$s %n";
@Override
public synchronized String format(LogRecord lr) {
return String.format(format,
new Date(lr.getMillis()),
lr.getLevel().getLocalizedName(),
lr.getMessage()
);
}
});
} catch (IOException | SecurityException ex) {
Logger.getLogger(GeyserLogger.class.getName()).log(Level.SEVERE, null, ex);
}
if (System.getenv().containsKey("DP_SENTRY_CLIENT_KEY")) {
Handler sentryHandler = new io.sentry.jul.SentryHandler();
sentryHandler.setLevel(Level.SEVERE);
Sentry.init(System.getenv().get("DP_SENTRY_CLIENT_KEY"));
}
}
2019-07-13 19:00:51 +00:00
@Override
public void severe(String message) {
System.out.println(printConsole(ChatColor.DARK_RED + message, colored));
2019-07-08 17:55:14 +00:00
}
2019-09-15 07:57:02 +00:00
@Override
public void severe(String message, Throwable error) {
System.out.println(printConsole(ChatColor.DARK_RED + message + "\n" + error.getMessage(), colored));
}
2019-07-13 19:00:51 +00:00
@Override
public void error(String message) {
System.out.println(printConsole(ChatColor.RED + message, colored));
2019-07-08 17:55:14 +00:00
}
2019-09-15 07:57:02 +00:00
@Override
public void error(String message, Throwable error) {
System.out.println(printConsole(ChatColor.RED + message + "\n" + error, colored));
2019-09-15 07:57:02 +00:00
}
2019-07-13 19:00:51 +00:00
@Override
2019-07-08 17:55:14 +00:00
public void warning(String message) {
2019-07-13 19:00:51 +00:00
System.out.println(printConsole(ChatColor.YELLOW + message, colored));
}
@Override
public void info(String message) {
System.out.println(printConsole(ChatColor.WHITE + message, colored));
2019-07-08 17:55:14 +00:00
}
2019-07-13 19:00:51 +00:00
@Override
2019-07-08 17:55:14 +00:00
public void debug(String message) {
if (debug)
System.out.println(printConsole(ChatColor.GRAY + message, colored));
2019-07-08 17:55:14 +00:00
}
public static String printConsole(String message, boolean colors) {
return colors ? ChatColor.toANSI(message + ChatColor.RESET) : ChatColor.stripColors(message + ChatColor.RESET);
}
@Override
public void setDebug(boolean debug) {
this.debug = debug;
}
2019-07-08 17:55:14 +00:00
}