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 3 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
64 changes: 43 additions & 21 deletions src/main/java/ch/njol/skript/effects/EffLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@
@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")
" log \"%player% placed TNT in %world% at %location of block%\" to file \"tnt/placement.log\"",
" log \"%player% placed TNT in %world% at %location of block%\" to file \"tnt/placement.log\"\"with severity of info\"",
" log \"%player% placed TNT in %world% at %location of block%\" to file \"tnt/placement.log\"\"with severity of warning\"",
" log \"%player% placed TNT in %world% at %location of block%\" to file \"tnt/placement.log\"\"with severity of error\""})
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved


@Since("2.0, 2.9 (Adds severity to logs)")
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
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 severity (of) (1:warning|2:severe)]");
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
}

private final static File logsFolder = new File(Skript.getInstance().getDataFolder(), "logs");
Expand All @@ -77,12 +82,15 @@ public void close() {
private Expression<String> messages;
@Nullable
private Expression<String> files;


private int loglevel;
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
messages = (Expression<String>) exprs[0];
files = (Expression<String>) exprs[1];
loglevel = parser.mark;
return true;
}

Expand All @@ -91,28 +99,35 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
protected void execute(final Event e) {
for (final String message : messages.getArray(e)) {
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
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(e)) {
logFile = logFile.toLowerCase(Locale.ENGLISH);
if (!logFile.endsWith(".log"))
logFile += ".log";
if (logFile.equals("server.log")) {
switch (loglevel) {
case 1: SkriptLogger.LOGGER.log(Level.WARNING, message);
break;
case 2: SkriptLogger.LOGGER.log(Level.SEVERE, message);
break;
default:
SkriptLogger.LOGGER.log(Level.INFO, 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) {
final 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);
logFolder.getParentFile().mkdirs();
logWriter = new PrintWriter(new BufferedWriter(new FileWriter(logFolder, true)));
writers.put(logFile, logWriter);
} catch (final IOException ex) {
Skript.error("Cannot write to log file '" + s + "' (" + f.getPath() + "): " + ExceptionUtils.toString(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("[" + SkriptConfig.formatDate(System.currentTimeMillis()) + "] " + message);
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
logWriter.flush();
}
} else {
Trigger t = getTrigger();
Expand All @@ -122,7 +137,14 @@ protected void execute(final Event e) {
if (script != null)
scriptName = script.getConfig().getFileName();
}
Skript.info("[" + scriptName + "] " + message);
switch (loglevel) {
case 1: Skript.warning("[" + scriptName + "] " + message);
break;
case 2: Skript.error("[" + scriptName + "] " + message);
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
Skript.info("[" + scriptName + "] " + message);
}
}
}
}
Expand Down