diff --git a/src/main/java/ch/njol/skript/effects/EffLog.java b/src/main/java/ch/njol/skript/effects/EffLog.java index bce9d3bae76..a3b7cc23f44 100644 --- a/src/main/java/ch/njol/skript/effects/EffLog.java +++ b/src/main/java/ch/njol/skript/effects/EffLog.java @@ -46,28 +46,31 @@ import ch.njol.util.Closeable; import ch.njol.util.Kleenean; -/** - * @author Peter Güttinger - */ @Name("Log") @Description({"Writes text into a .log file. Skript will write these files to /plugins/Skript/logs.", "NB: Using 'server.log' as the log file will write to the default server log. Omitting the log file altogether will log the message as '[Skript] [<script>.sk] <message>' in the server log."}) -@Examples({"on place of TNT:", - " log \"%player% placed TNT in %world% at %location of block%\" to \"tnt/placement.log\""}) -@Since("2.0") +@Examples({ + "on join:", + "\tlog \"%player% has just joined the server!\"", + "on world change:", + "\tlog \"Someone just went to %event-world%!\" to file \"worldlog/worlds.log\"", + "on command:", + "\tlog \"%player% just executed %full command%!\" to file \"server/commands.log\" with a severity of warning" +}) +@Since("2.0, INSERT VERSION (severities)") public class EffLog extends Effect { static { - Skript.registerEffect(EffLog.class, "log %strings% [(to|in) [file[s]] %-strings%]"); + Skript.registerEffect(EffLog.class, "log %strings% [(to|in) [file[s]] %-strings%] [with [the|a] severity [of] (1:warning|2:severe)]"); } - private final static File logsFolder = new File(Skript.getInstance().getDataFolder(), "logs"); + private static final File logsFolder = new File(Skript.getInstance().getDataFolder(), "logs"); final static HashMap writers = new HashMap<>(); static { Skript.closeOnDisable(new Closeable() { @Override public void close() { - for (final PrintWriter pw : writers.values()) + for (PrintWriter pw : writers.values()) pw.close(); } }); @@ -77,42 +80,55 @@ public void close() { private Expression messages; @Nullable private Expression files; - + + private Level logLevel = Level.INFO; + private static String getLogPrefix(Level logLevel) { + String timestamp = SkriptConfig.formatDate(System.currentTimeMillis()); + if (logLevel == Level.INFO) + return "[" + timestamp + "]"; + return "[" + timestamp + " " + logLevel + "]"; + } + @SuppressWarnings({"unchecked", "null"}) @Override - public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) { + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { messages = (Expression) exprs[0]; files = (Expression) exprs[1]; + if (parser.mark == 1) { + logLevel = Level.WARNING; + } else if (parser.mark == 2) { + logLevel = Level.SEVERE; + } return true; } @SuppressWarnings("resource") @Override - protected void execute(final Event e) { - for (final String message : messages.getArray(e)) { + protected void execute(Event event) { + for (String message : messages.getArray(event)) { if (files != null) { - for (String s : files.getArray(e)) { - s = s.toLowerCase(Locale.ENGLISH); - if (!s.endsWith(".log")) - s += ".log"; - if (s.equals("server.log")) { - SkriptLogger.LOGGER.log(Level.INFO, message); + for (String logFile : files.getArray(event)) { + logFile = logFile.toLowerCase(Locale.ENGLISH); + if (!logFile.endsWith(".log")) + logFile += ".log"; + if (logFile.equals("server.log")) { + SkriptLogger.LOGGER.log(logLevel, message); continue; } - PrintWriter w = writers.get(s); - if (w == null) { - final File f = new File(logsFolder, s); // REMIND what if s contains '..'? + PrintWriter logWriter = writers.get(logFile); + if (logWriter == null) { + File logFolder = new File(logsFolder, logFile); // REMIND what if logFile contains '..'? try { - f.getParentFile().mkdirs(); - w = new PrintWriter(new BufferedWriter(new FileWriter(f, true))); - writers.put(s, w); - } catch (final IOException ex) { - Skript.error("Cannot write to log file '" + s + "' (" + f.getPath() + "): " + ExceptionUtils.toString(ex)); + logFolder.getParentFile().mkdirs(); + logWriter = new PrintWriter(new BufferedWriter(new FileWriter(logFolder, true))); + writers.put(logFile, logWriter); + } catch (IOException ex) { + Skript.error("Cannot write to log file '" + logFile + "' (" + logFolder.getPath() + "): " + ExceptionUtils.toString(ex)); return; } } - w.println("[" + SkriptConfig.formatDate(System.currentTimeMillis()) + "] " + message); - w.flush(); + logWriter.println(getLogPrefix(logLevel) + " " + message); + logWriter.flush(); } } else { Trigger t = getTrigger(); @@ -122,13 +138,15 @@ protected void execute(final Event e) { if (script != null) scriptName = script.getConfig().getFileName(); } - Skript.info("[" + scriptName + "] " + message); + SkriptLogger.LOGGER.log(logLevel, "[" + scriptName + "] " + message); } } } - + @Override - public String toString(final @Nullable Event e, final boolean debug) { - return "log " + messages.toString(e, debug) + (files != null ? " to " + files.toString(e, debug) : ""); + public String toString(@Nullable Event event, boolean debug) { + return "log " + messages.toString(event, debug) + + (files != null ? " to " + files.toString(event, debug) : "") + + (logLevel != Level.INFO ? "with severity " + logLevel.toString().toLowerCase(Locale.ENGLISH) : ""); } }