-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a3b01b
commit 14704cb
Showing
6 changed files
with
144 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters