Add option to save command line logs (#102)

* Add logger that uses java.util.Logger
* Add file log handler and options to save command line log
* Support subdirectories and only use LoggerLogger as global log when saving the log file
This commit is contained in:
Ryan 2020-10-21 13:38:20 -06:00 committed by GitHub
parent 86b8deb3f3
commit d509c48025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,107 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* 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:
*
* 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.
*/
package de.bluecolored.bluemap.core.logger;
import java.io.File;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import org.apache.commons.lang3.exception.ExceptionUtils;
public class LoggerLogger extends AbstractLogger {
private static LoggerLogger instance = null;
private Logger logger;
private SimpleFormatter formatter;
private LoggerLogger() {
this.logger = Logger.getLogger("bluemap");
this.logger.setUseParentHandlers(false);
ConsoleHandler cHandler = new ConsoleHandler();
formatter = new SimpleFormatter() {
@Override
public synchronized String format(LogRecord record) {
String stackTrace = record.getThrown() == null ? "" : ExceptionUtils.getStackTrace(record.getThrown());
return String.format("[%1$s] %2$s%3$s%n", record.getLevel(), record.getMessage(), stackTrace);
}
};
cHandler.setFormatter(formatter);
this.logger.addHandler(cHandler);
}
public static LoggerLogger getInstance() {
if (instance == null) {
instance = new LoggerLogger();
}
return instance;
}
public void addFileHandler(String filename, boolean append) {
try {
FileHandler fHandler = new FileHandler(filename, append);
fHandler.setFormatter(formatter);
this.logger.addHandler(fHandler);
} catch (NoSuchFileException e) {
// Directory may not exist. Create it and try again.
File parent = Paths.get(e.getFile()).getParent().toFile();
if (!parent.exists()) {
parent.mkdirs();
addFileHandler(filename, append);
} else {
de.bluecolored.bluemap.core.logger.Logger.global.logError("Error while opening log file!", e);
}
} catch (IOException e) {
de.bluecolored.bluemap.core.logger.Logger.global.logError("Error while opening log file!", e);
}
}
@Override
public void logError(String message, Throwable throwable) {
logger.log(Level.SEVERE, message, throwable);
}
@Override
public void logWarning(String message) {
logger.log(Level.WARNING, message);
}
@Override
public void logInfo(String message) {
logger.log(Level.INFO, message);
}
@Override
public void logDebug(String message) {
logger.log(Level.FINE, message);
}
}

View File

@ -57,6 +57,7 @@
import de.bluecolored.bluemap.core.MinecraftVersion;
import de.bluecolored.bluemap.core.config.WebServerConfig;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.logger.LoggerLogger;
import de.bluecolored.bluemap.core.metrics.Metrics;
import de.bluecolored.bluemap.core.render.hires.HiresModelManager;
import de.bluecolored.bluemap.core.web.FileRequestHandler;
@ -254,6 +255,11 @@ public static void main(String[] args) {
try {
CommandLine cmd = parser.parse(BlueMapCLI.createOptions(), args, false);
if (cmd.hasOption("l")) {
Logger.global = LoggerLogger.getInstance();
((LoggerLogger) Logger.global).addFileHandler(cmd.getOptionValue("l"), cmd.hasOption("a"));
}
//help
if (cmd.hasOption("h")) {
BlueMapCLI.printHelp();
@ -375,6 +381,16 @@ private static Options createOptions() {
.desc("Sets the minecraft-version, used e.g. to load resource-packs correctly. Defaults to the latest compatible version.")
.build()
);
options.addOption(
Option.builder("l")
.longOpt("log-file")
.hasArg()
.argName("file-name")
.desc("Sets a file to save the log to. If not specified, no log will be saved.")
.build()
);
options.addOption("a", "append", false, "Causes log save file to be appended rather than replaced.");
options.addOption("w", "webserver", false, "Starts the web-server, configured in the 'webserver.conf' file");
options.addOption("b", "verbose", false, "Causes the web-server to log requests to the console");