Skip to content

Commit

Permalink
Allows for the log level to be changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Ezeh committed Oct 1, 2017
1 parent 235dd43 commit 410e127
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
33 changes: 32 additions & 1 deletion src/main/java/cn/nukkit/Nukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import cn.nukkit.command.CommandReader;
import cn.nukkit.network.protocol.ProtocolInfo;
import cn.nukkit.utils.LogLevel;
import cn.nukkit.utils.MainLogger;
import cn.nukkit.utils.ServerKiller;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

/**
* `_ _ _ _ _ _
* | \ | | | | | | (_) |
Expand Down Expand Up @@ -50,16 +55,42 @@ public static void main(String[] args) {
}
}

LogLevel logLevel = LogLevel.DEFAULT_LEVEL;
int index = -1;
boolean skip = false;
//启动参数
for (String arg : args) {
index++;
if (skip) {
skip = false;
continue;
}

switch (arg) {
case "disable-ansi":
ANSI = false;
break;

case "--verbosity":
case "-v":
skip = true;
try {
String levelName = args[index + 1];
Set<String> levelNames = Arrays.stream(LogLevel.values()).map(level -> level.name().toLowerCase()).collect(Collectors.toSet());
if (!levelNames.contains(levelName.toLowerCase())) {
System.out.printf("'%s' is not a valid log level, using the default\n", levelName);
continue;
}
logLevel = Arrays.stream(LogLevel.values()).filter(level -> level.name().equalsIgnoreCase(levelName)).findAny().orElse(LogLevel.DEFAULT_LEVEL);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("You must enter the requested log level, using the default\n");
}

}
}

MainLogger logger = new MainLogger(DATA_PATH + "server.log");
MainLogger logger = new MainLogger(DATA_PATH + "server.log", logLevel);
System.out.printf("Using log level '%s'\n", logLevel);

try {
if (ANSI) {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/cn/nukkit/utils/LogLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
* author: MagicDroidX
* Nukkit Project
*/
public enum LogLevel {
public enum LogLevel implements Comparable<LogLevel> {
NONE,
EMERGENCY,
ALERT,
CRITICAL,
ERROR,
WARNING,
NOTICE,
INFO,
DEBUG
DEBUG;
public static final LogLevel DEFAULT_LEVEL = INFO;

int getLevel() {
return ordinal();
}
}
41 changes: 25 additions & 16 deletions src/main/java/cn/nukkit/utils/MainLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,75 +22,84 @@ public class MainLogger extends ThreadedLogger {
protected final String logPath;
protected final ConcurrentLinkedQueue<String> logBuffer = new ConcurrentLinkedQueue<>();
protected boolean shutdown;
protected boolean logDebug = false;
protected LogLevel logLevel = LogLevel.DEFAULT_LEVEL;
private final Map<TextFormat, String> replacements = new EnumMap<>(TextFormat.class);
private final TextFormat[] colors = TextFormat.values();

protected static MainLogger logger;

public MainLogger(String logFile) {
this(logFile, false);
this(logFile, LogLevel.DEFAULT_LEVEL);
}

public MainLogger(String logFile, boolean logDebug) {
public MainLogger(String logFile, LogLevel logLevel) {

if (logger != null) {
throw new RuntimeException("MainLogger has been already created");
}
logger = this;
this.logPath = logFile;
this.logDebug = logDebug;
this.start();
}

public MainLogger(String logFile, boolean logDebug) {
this(logFile, logDebug ? LogLevel.DEBUG : LogLevel.INFO);
}

public static MainLogger getLogger() {
return logger;
}

@Override
public void emergency(String message) {
this.send(TextFormat.RED + "[EMERGENCY] " + message);
if (logLevel.getLevel() <= LogLevel.EMERGENCY.getLevel())
this.send(TextFormat.RED + "[EMERGENCY] " + message);
}

@Override
public void alert(String message) {
this.send(TextFormat.RED + "[ALERT] " + message);
if (logLevel.getLevel() <= LogLevel.ALERT.getLevel())
this.send(TextFormat.RED + "[ALERT] " + message);
}

@Override
public void critical(String message) {
this.send(TextFormat.RED + "[CRITICAL] " + message);
if (logLevel.getLevel() <= LogLevel.CRITICAL.getLevel())
this.send(TextFormat.RED + "[CRITICAL] " + message);
}

@Override
public void error(String message) {
this.send(TextFormat.DARK_RED + "[ERROR] " + message);
if (logLevel.getLevel() <= LogLevel.ERROR.getLevel())
this.send(TextFormat.DARK_RED + "[ERROR] " + message);
}

@Override
public void warning(String message) {
this.send(TextFormat.YELLOW + "[WARNING] " + message);
if (logLevel.getLevel() <= LogLevel.WARNING.getLevel())
this.send(TextFormat.YELLOW + "[WARNING] " + message);
}

@Override
public void notice(String message) {
this.send(TextFormat.AQUA + "[NOTICE] " + message);
if (logLevel.getLevel() <= LogLevel.NOTICE.getLevel())
this.send(TextFormat.AQUA + "[NOTICE] " + message);
}

@Override
public void info(String message) {
this.send(TextFormat.WHITE + "[INFO] " + message);
if (logLevel.getLevel() <= LogLevel.INFO.getLevel())
this.send(TextFormat.WHITE + "[INFO] " + message);
}

@Override
public void debug(String message) {
if (!this.logDebug) {
return;
}
this.send(TextFormat.GRAY + "[DEBUG] " + message);
if (logLevel.getLevel() <= LogLevel.DEBUG.getLevel())
this.send(TextFormat.GRAY + "[DEBUG] " + message);
}

public void setLogDebug(Boolean logDebug) {
this.logDebug = logDebug;
this.logLevel = logDebug ? LogLevel.INFO : LogLevel.DEBUG;
}

public void logException(Exception e) {
Expand Down

0 comments on commit 410e127

Please sign in to comment.