Skip to content

Commit

Permalink
Support for LogLevel in EffLog (#6659)
Browse files Browse the repository at this point in the history
* Improved EffLog

Added wats to declare severity when logging

* Update EffLog.java

* Update EffLog.java

Updated `@Since` to include when severity to logs was added

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

Adds the `[Skript]` prefix to `Level.INFO`

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

Fixes a spacing issue

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

changes to use .toString instead of String.valueOf

* Update EffLog.java

* Update EffLog.java

* Update EffLog.java

---------

Co-authored-by: sovdee <[email protected]>
Co-authored-by: Moderocky <[email protected]>
  • Loading branch information
3 people authored Jul 1, 2024
1 parent b48f66d commit e65fe2f
Showing 1 changed file with 51 additions and 33 deletions.
84 changes: 51 additions & 33 deletions src/main/java/ch/njol/skript/effects/EffLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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] [&lt;script&gt;.sk] &lt;message&gt;' 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<String, PrintWriter> 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();
}
});
Expand All @@ -77,42 +80,55 @@ public void close() {
private Expression<String> messages;
@Nullable
private Expression<String> 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<String>) exprs[0];
files = (Expression<String>) 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();
Expand All @@ -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) : "");
}
}

0 comments on commit e65fe2f

Please sign in to comment.