Skip to content

Commit

Permalink
feat: chat & console logger
Browse files Browse the repository at this point in the history
  • Loading branch information
topi-banana committed Jan 23, 2025
1 parent 5a3b01b commit 14704cb
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 26 deletions.
11 changes: 10 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@

- `logLevel`
- `logLevel_chat`

Changes the log output level.

Type: `String` (case-insensitive)

Allow: [`info`, `off`, `debug`, `error`, `warn`, `all`]


- `consoleLogLevel`

Changes the log output level.

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/vulpeus/kyoyu/Kyoyu.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.vulpeus.kyoyu.placement.KyoyuPlacement;
import net.minecraft.server.level.ServerPlayer;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
Expand All @@ -20,7 +18,7 @@ public class Kyoyu {
public static final String MOD_ID = "kyoyu";
public static final String MOD_VERSION = /*$ mod_version*/ "unknown";

public static final Logger LOGGER = LogManager.getLogger(MOD_ID);
public static final KyoyuLogger LOGGER = new KyoyuLogger();

public static KyoyuConfig CONFIG = null;

Expand Down Expand Up @@ -49,7 +47,8 @@ public static void loadConfig() {
}
String json = new String(Files.readAllBytes(configFile), StandardCharsets.UTF_8);
CONFIG = KyoyuConfig.fromJson(json);
CONFIG.setLogLevel();
LOGGER.setChatLevel(CONFIG.chatLogLevel());
LOGGER.setConsoleLevel(CONFIG.consoleLogLevel());
} catch (IOException e) {
LOGGER.error("cannot read config");
LOGGER.error(e);
Expand Down
47 changes: 28 additions & 19 deletions src/main/java/com/vulpeus/kyoyu/KyoyuConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,48 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;

import java.util.ArrayList;
import java.util.List;

public class KyoyuConfig {

private final String logLevel = "info";
private final String logLevel_chat;
private final String logLevel_console;

private final String modify = "blacklist";
private final List<String> modify_whitelist = new ArrayList<>();
private final List<String> modify_blacklist = new ArrayList<>();
private final String modify;
private final List<String> modify_whitelist;
private final List<String> modify_blacklist;

private final String remove = "blacklist";
private final List<String> remove_whitelist = new ArrayList<>();
private final List<String> remove_blacklist = new ArrayList<>();
private final String remove;
private final List<String> remove_whitelist;
private final List<String> remove_blacklist;

public KyoyuConfig() {
this.logLevel_chat = "info";
this.logLevel_console = "info";

this.modify = "blacklist";
this.modify_whitelist = new ArrayList<>();
this.modify_blacklist = new ArrayList<>();

this.remove = "blacklist";
this.remove_whitelist = new ArrayList<>();
this.remove_blacklist = new ArrayList<>();
}

public KyoyuLogger.Level chatLogLevel() {
return KyoyuLogger.Level.fromString(logLevel_chat);
}
public KyoyuLogger.Level consoleLogLevel() {
return KyoyuLogger.Level.fromString(logLevel_console);
}

public static KyoyuConfig fromJson(String json) {
Gson gson = new Gson();
return gson.fromJson(json, KyoyuConfig.class);
}

public void setLogLevel() {
String log_level = this.logLevel.toUpperCase();
if (log_level.equals("OFF")) Configurator.setLevel(Kyoyu.MOD_ID, Level.OFF);
if (log_level.equals("INFO")) Configurator.setLevel(Kyoyu.MOD_ID, Level.INFO);
if (log_level.equals("DEBUG")) Configurator.setLevel(Kyoyu.MOD_ID, Level.DEBUG);
if (log_level.equals("ERROR")) Configurator.setLevel(Kyoyu.MOD_ID, Level.ERROR);
if (log_level.equals("WARN")) Configurator.setLevel(Kyoyu.MOD_ID, Level.WARN);
if (log_level.equals("ALL")) Configurator.setLevel(Kyoyu.MOD_ID, Level.ALL);
}

private boolean containsCaseInsensitive(List<String> list, String value) {
for (String otherValue: list) {
if (value.equalsIgnoreCase(otherValue)) return true;
Expand Down
100 changes: 100 additions & 0 deletions src/main/java/com/vulpeus/kyoyu/KyoyuLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.vulpeus.kyoyu;

import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;

public class KyoyuLogger {
private final String prefix;
private Level chatLevel;
private Level consoleLevel;

private final Logger logger = LogManager.getLogger(Kyoyu.MOD_ID);

public KyoyuLogger() {
this.prefix = "Kyoyu";
this.chatLevel = Level.INFO;
this.consoleLevel = Level.INFO;
}
public void info(Object obj) {
error("{}", obj);
}
public void info(String fmt, Object... objects) {
log(Level.INFO, fmt, objects);
}

public void warn(Object obj) {
error("{}", obj);
}
public void warn(String fmt, Object... objects) {
log(Level.WARN, fmt, objects);
}

public void debug(Object obj) {
error("{}", obj);
}
public void debug(String fmt, Object... objects) {
log(Level.DEBUG, fmt, objects);
}

public void error(Object obj) {
error("{}", obj);
}
public void error(String fmt, Object... objects) {
log(Level.ERROR, fmt, objects);
}

private void log(Level level, String fmt, Object... objects) {
String message = format(fmt, objects);
if (shouldLog(level, this.chatLevel)) {
for (ServerPlayer serverPlayer: Kyoyu.PLAYERS) {
serverPlayer.sendSystemMessage(Component.empty().append(message));
}
}
if (shouldLog(level, this.consoleLevel)) {
logger.info("[{}] {} : {}", this.prefix, level.level, message);
}
}
private boolean shouldLog(Level messageLevel, Level configuredLevel) {
if (configuredLevel == Level.OFF) return false;
if (configuredLevel == Level.ALL) return true;
return messageLevel.ordinal() >= configuredLevel.ordinal();
}

private String format(String formatter, Object[] objects) {
return ParameterizedMessage.format(formatter, objects);
}

public void setChatLevel(Level level) {
this.chatLevel = level;
}
public void setConsoleLevel(Level level) {
this.consoleLevel = level;
}

public enum Level {
ALL("all"),
DEBUG("debug"),
INFO("info"),
WARN("warn"),
ERROR("error"),
OFF("off");

private final String level;

Level(String str) {
this.level = str.toUpperCase();
}

public static Level fromString(String str) {
if (str.equalsIgnoreCase("all")) return Level.ALL;
if (str.equalsIgnoreCase("debug")) return Level.DEBUG;
if (str.equalsIgnoreCase("info")) return Level.INFO;
if (str.equalsIgnoreCase("warn")) return Level.WARN;
if (str.equalsIgnoreCase("error")) return Level.ERROR;
return Level.OFF;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Explorer_WidgetList(int x, int y, int width, int height, ISelectionListen
super(x, y, width, height, selectionListener);
this.browserEntryHeight = 22;
this.kyoyuPlacement = kyoyuPlacement;
Kyoyu.LOGGER.info("{}", getAllEntries());
Kyoyu.LOGGER.info(getAllEntries());
setSize(width, height);
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/kyoyu/kyoyu.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"logLevel": "info",
"logLevel_chat": "info",
"logLevel_console": "info",

"modify": "blacklist",
"modify_whitelist": [],
Expand Down

0 comments on commit 14704cb

Please sign in to comment.