Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for LogLevel in EffLog #6659

Merged
merged 36 commits into from
Jul 1, 2024
Merged
Changes from 9 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
0217ab5
Improved EffLog
EquipableMC May 8, 2024
a8572a1
Update EffLog.java
EquipableMC May 8, 2024
66ad0c1
Update EffLog.java
EquipableMC May 8, 2024
ad9c652
Update EffLog.java
EquipableMC May 8, 2024
6d5d7ba
Update EffLog.java
EquipableMC May 8, 2024
2a57b56
Merge branch 'dev/feature' into dev/feature
EquipableMC May 8, 2024
570d764
Update EffLog.java
EquipableMC May 8, 2024
413d717
Update EffLog.java
EquipableMC May 8, 2024
d12f89b
Update EffLog.java
EquipableMC May 8, 2024
ef9ab68
Update EffLog.java
EquipableMC May 8, 2024
c09984f
Merge branch 'SkriptLang:dev/feature' into dev/feature
EquipableMC May 10, 2024
806cbcc
Update EffLog.java
EquipableMC May 10, 2024
fc37d4f
Update EffLog.java
EquipableMC May 12, 2024
080515c
Merge branch 'dev/feature' into dev/feature
sovdeeth May 13, 2024
078265c
Update EffLog.java
EquipableMC May 13, 2024
244fa08
Update EffLog.java
EquipableMC May 13, 2024
3500a4b
Update EffLog.java
EquipableMC May 13, 2024
797573e
Update EffLog.java
EquipableMC May 13, 2024
b24be61
Update EffLog.java
EquipableMC May 16, 2024
e0faf82
Update EffLog.java
EquipableMC May 19, 2024
a2b117f
Update EffLog.java
EquipableMC May 27, 2024
694024b
Update EffLog.java
EquipableMC Jun 1, 2024
55e92ba
Update EffLog.java
EquipableMC Jun 1, 2024
5081bae
Update EffLog.java
EquipableMC Jun 5, 2024
b4675df
Merge branch 'dev/feature' into dev/feature
Moderocky Jun 11, 2024
b8b5b7d
Update EffLog.java
EquipableMC Jun 11, 2024
88ef745
Merge branch 'dev/feature' into dev/feature
Moderocky Jun 28, 2024
baefbfb
Merge branch 'dev/feature' into dev/feature
EquipableMC Jun 28, 2024
4009d45
Update EffLog.java
EquipableMC Jun 28, 2024
22ab0ec
Merge branch 'dev/feature' of https://github.com/EquipableMC/Skript i…
EquipableMC Jun 28, 2024
0cb99cc
Update EffLog.java
EquipableMC Jun 28, 2024
d8f4c4b
Update EffLog.java
EquipableMC Jun 28, 2024
6406a05
Merge branch 'dev/feature' into dev/feature
sovdeeth Jun 28, 2024
251419c
Update EffLog.java
EquipableMC Jul 1, 2024
cebf1c3
Update EffLog.java
EquipableMC Jul 1, 2024
bb279ee
Merge branch 'dev/feature' into dev/feature
sovdeeth Jul 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 73 additions & 34 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,28 @@
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({"log \"%player% has just joined the server!\"",
"log \"Someone just went to %event-world%!\" to file \"worldlog/worlds.log\"",
"log \"%player% just executed %full command%!\" to file \"server/commands.log\"\"with a severity of warning\""})


EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
@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())
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
pw.close();
}
});
Expand All @@ -77,42 +77,61 @@ public void close() {
private Expression<String> messages;
@Nullable
private Expression<String> files;


private Level logLevel = Level.INFO;
private static final int WARNING_LOG = 900;
private static final int SEVERE_LOG = 1000;

@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);
continue;
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);
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
}
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) {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
Skript.error("Cannot write to log file '" + logFile + "' (" + logFolder.getPath() + "): " + ExceptionUtils.toString(ex));
return;
}
}
w.println("[" + SkriptConfig.formatDate(System.currentTimeMillis()) + "] " + message);
w.flush();
String levelType;
switch (logLevel.intValue()) {
case WARNING_LOG:
levelType = "WARNING";
break;
case SEVERE_LOG:
levelType = "SEVERE";
break;
default:
levelType = "INFO";
}
logWriter.println("[" + levelType + "]" + "[" + SkriptConfig.formatDate(System.currentTimeMillis()) + "] " + message);
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
logWriter.flush();
}
} else {
Trigger t = getTrigger();
Expand All @@ -122,13 +141,33 @@ protected void execute(final Event e) {
if (script != null)
scriptName = script.getConfig().getFileName();
}
Skript.info("[" + scriptName + "] " + message);
switch (logLevel.intValue()) {
case WARNING_LOG:
Skript.warning("[" + scriptName + "] " + messages);
break;
case SEVERE_LOG:
Skript.error("[" + scriptName + "] " + messages);
break;
default:
Skript.info("[" + scriptName + "] " + messages);
}
}
}
}

@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 e, boolean debug) {
String levelType;
switch (logLevel.intValue()) {
case WARNING_LOG:
levelType = "warning ";
break;
case SEVERE_LOG:
levelType = "severe ";
break;
default:
levelType = "info ";
}
return "log " + levelType + messages.toString(e, debug) + (files != null ? " to " + files.toString(e, debug) : "");
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
}
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
}