[BLEEDING] Add option to set prefixes for the logging messages.

This commit is contained in:
asofold 2013-06-11 01:21:50 +02:00
parent 2bf28c4982
commit 1d53115abe
3 changed files with 64 additions and 9 deletions

View File

@ -27,6 +27,10 @@ import fr.neatmonster.nocheatplus.utilities.ColorUtil;
*/
public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
private static final String PREFIX_CHAT = ChatColor.RED + "NCP: "+ ChatColor.WHITE ;
private static final String PREFIX_CONSOLE= "[NoCheatPlus] ";
private static final String PREFIX_FILE = "";
// TODO: pull down to providers for (console), !chat!, (file) - then move to NCPCompat.
// Some flags to decide where the log message should show up, based on the configuration file.
@ -38,6 +42,9 @@ public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
/** Log to file? */
public final boolean toFile;
/** Message prefixes. */
public final String prefixChat, prefixConsole, prefixFile;
/**
* Instantiates a new log action.
@ -60,11 +67,36 @@ public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
public LogAction(final String name, final int delay, final int repeat, final boolean toChat,
final boolean toConsole, final boolean toFile, final String message) {
super(name, delay, repeat, message);
// Might switch to only store the prefixes (null = deactivated).
this.toChat = toChat;
this.toConsole = toConsole;
this.toFile = toFile;
// TODO: already use && flagfromconfig.
prefixChat = PREFIX_CHAT;
prefixConsole = PREFIX_CONSOLE;
prefixFile = PREFIX_FILE;
}
/**
* Constructor for optimized actions.
* @param name
* @param delay
* @param repeat
* @param prefixChat Prefixes set to null means deactivated.
* @param prefixConsole
* @param prefixFile
* @param message
*/
private LogAction(final String name, final int delay, final int repeat, final String prefixChat,
final String prefixConsole, final String prefixFile, final String message) {
super(name, delay, repeat, message);
this.prefixChat = prefixChat;
this.prefixConsole = prefixConsole;
this.prefixFile = prefixFile;
toChat = prefixChat != null;
toConsole = prefixConsole != null;
toFile = prefixFile != null;
}
/*
* (non-Javadoc)
@ -77,9 +109,9 @@ public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
public boolean execute(final ViolationData violationData) {
if (!violationData.player.hasPermission(violationData.getPermissionSilent())) {
final String message = super.getMessage(violationData);
if (toChat) NCPAPIProvider.getNoCheatPlusAPI().sendAdminNotifyMessage(ChatColor.RED + "NCP: " + ChatColor.WHITE + ColorUtil.replaceColors(message));
if (toConsole) LogUtil.logInfo("[NoCheatPlus] " + ColorUtil.removeColors(message));
if (toFile) StaticLogFile.fileLogger.info(ColorUtil.removeColors(message));
if (toChat) NCPAPIProvider.getNoCheatPlusAPI().sendAdminNotifyMessage(ColorUtil.replaceColors(prefixChat + message));
if (toConsole) LogUtil.logInfo(ColorUtil.removeColors(prefixConsole + message));
if (toFile) StaticLogFile.fileLogger.info(ColorUtil.removeColors(prefixFile + message));
}
return false;
}
@ -98,11 +130,28 @@ public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
@Override
public Action<ViolationData, ActionList> getOptimizedCopy(final ConfigFileWithActions<ViolationData, ActionList> config, final Integer threshold) {
if (!config.getBoolean(ConfPaths.LOGGING_ACTIVE)) return null;
final boolean toConsole = this.toConsole && config.getBoolean(ConfPaths.LOGGING_BACKEND_CONSOLE_ACTIVE);
final boolean toFile = this.toFile&& config.getBoolean(ConfPaths.LOGGING_BACKEND_FILE_ACTIVE);
final boolean toChat= this.toChat&& config.getBoolean(ConfPaths.LOGGING_BACKEND_INGAMECHAT_ACTIVE);
if (!toChat && ! toConsole && !toFile) return null;
return new LogAction(name, delay, repeat, toChat, toConsole, toFile, message);
final String prefixChat = filterPrefix(config, ConfPaths.LOGGING_BACKEND_INGAMECHAT_PREFIX, PREFIX_CHAT, this.toChat && config.getBoolean(ConfPaths.LOGGING_BACKEND_INGAMECHAT_ACTIVE));
final String prefixConsole = filterPrefix(config, ConfPaths.LOGGING_BACKEND_CONSOLE_PREFIX, PREFIX_CONSOLE, this.toConsole && config.getBoolean(ConfPaths.LOGGING_BACKEND_CONSOLE_ACTIVE));
final String prefixFile = filterPrefix(config, ConfPaths.LOGGING_BACKEND_FILE_PREFIX, PREFIX_FILE, this.toFile && config.getBoolean(ConfPaths.LOGGING_BACKEND_FILE_ACTIVE));
if (allNull(toChat, toConsole, toFile)){
return null;
}
return new LogAction(name, delay, repeat, prefixChat, prefixConsole, prefixFile, message);
}
private static boolean allNull(Object... objects){
for (int i = 0; i < objects.length; i++){
if (objects[i] != null){
return false;
}
}
return true;
}
private static final String filterPrefix(final ConfigFileWithActions<ViolationData, ActionList> config, final String path, final String defaultValue, final boolean use){
if (!use) return null;
final String prefix = config.getString(path);
return prefix == null ? defaultValue : prefix;
}
}

View File

@ -42,12 +42,15 @@ public abstract class ConfPaths {
private static final String LOGGING_BACKEND = LOGGING + "backend.";
private static final String LOGGING_BACKEND_CONSOLE = LOGGING_BACKEND + "console.";
public static final String LOGGING_BACKEND_CONSOLE_ACTIVE = LOGGING_BACKEND_CONSOLE + "active";
public static final String LOGGING_BACKEND_CONSOLE_PREFIX = LOGGING_BACKEND_CONSOLE + "prefix";
private static final String LOGGING_BACKEND_FILE = LOGGING_BACKEND + "file.";
public static final String LOGGING_BACKEND_FILE_ACTIVE = LOGGING_BACKEND_FILE + "active";
public static final String LOGGING_BACKEND_FILE_FILENAME = LOGGING_BACKEND_FILE + "filename";
public static final String LOGGING_BACKEND_FILE_PREFIX = LOGGING_BACKEND_FILE + "prefix";
private static final String LOGGING_BACKEND_INGAMECHAT = LOGGING_BACKEND + "ingamechat.";
public static final String LOGGING_BACKEND_INGAMECHAT_ACTIVE = LOGGING_BACKEND_INGAMECHAT + "active";
public static final String LOGGING_BACKEND_INGAMECHAT_SUBSCRIPTIONS = LOGGING_BACKEND_INGAMECHAT + "subscriptions";
public static final String LOGGING_BACKEND_INGAMECHAT_PREFIX = LOGGING_BACKEND_INGAMECHAT + "prefix";
// Deprecated.
@Moved(newPath=LOGGING_BACKEND_CONSOLE_ACTIVE)

View File

@ -52,9 +52,12 @@ public class DefaultConfig extends ConfigFile {
set(ConfPaths.LOGGING_ACTIVE, true);
set(ConfPaths.LOGGING_DEBUG, false);
set(ConfPaths.LOGGING_BACKEND_CONSOLE_ACTIVE, true);
set(ConfPaths.LOGGING_BACKEND_CONSOLE_PREFIX, "[NoCheatPlus] ");
set(ConfPaths.LOGGING_BACKEND_FILE_ACTIVE, true);
set(ConfPaths.LOGGING_BACKEND_FILE_PREFIX, "");
set(ConfPaths.LOGGING_BACKEND_FILE_FILENAME, "nocheatplus.log");
set(ConfPaths.LOGGING_BACKEND_INGAMECHAT_ACTIVE, true);
set(ConfPaths.LOGGING_BACKEND_INGAMECHAT_PREFIX, "&cNCP: &f");
// Notifications settings.
set(ConfPaths.LOGGING_BACKEND_INGAMECHAT_SUBSCRIPTIONS, false);