Fixed TownyAnnouncer

It took way too long to figure out how to add an appender that actually takes effect
Also changed Towny dep, it'll likely break Travis builds
This commit is contained in:
Norbi Peti 2019-10-20 02:29:54 +02:00
parent a91ee1b842
commit cc6c477add
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 27 additions and 10 deletions

View file

@ -33,8 +33,8 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: net.ess3:LegacyProvider:2.17.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.ess3:ReflectionProvider:2.17.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.ess3:FlattenedProvider:2.17.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.TownyAdvanced:Towny:0.94.0.9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.milkbowl:VaultAPI:master-af0353b1d7-1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.palmergames.bukkit.towny:Towny:0.95.0.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.milkbowl:VaultAPI:master-4c248aad62-1" level="project" />
<orderEntry type="library" name="Maven: org.bukkit:bukkit:1.13.1-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.projectlombok:lombok:1.18.10" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spigotmc:spigot:1.12.2-R0.1-SNAPSHOT" level="project" />

View file

@ -168,7 +168,7 @@
<id>projectlombok.org</id>
<url>http://projectlombok.org/mavenrepo</url>
</repository>
</repositories>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
@ -199,9 +199,9 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.TownyAdvanced</groupId>
<groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>Towny</artifactId>
<version>0.94.0.9</version>
<version>0.95.0.0</version>
<scope>provided</scope>
</dependency>
<!-- <dependency> <groupId>au.com.mineauz</groupId> <artifactId>Minigames</artifactId>

View file

@ -7,10 +7,10 @@ import buttondevteam.lib.chat.TBMCChatAPI;
import lombok.val;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.filter.LevelRangeFilter;
import org.apache.logging.log4j.core.layout.PatternLayout;
@ -19,13 +19,14 @@ import java.util.regex.Pattern;
public class TownyAnnouncer {
private static final Pattern LOG_TYPE_PATTERN = Pattern.compile("\\[(\\w+) (?:Msg|Message)](?: (\\w+):)?");
private static final Appender HANDLER = new AbstractAppender(TownyAnnouncer.class.getSimpleName(),
LevelRangeFilter.createFilter(Level.INFO, Level.INFO, Filter.Result.ACCEPT, Filter.Result.DENY),
private static final String APPENDER_NAME = "Chroma";
private static final AbstractAppender HANDLER = new AbstractAppender(APPENDER_NAME,
LevelRangeFilter.createFilter(Level.INFO, Level.INFO, Filter.Result.ACCEPT, Filter.Result.ACCEPT),
PatternLayout.createDefaultLayout()) {
@Override
public void append(LogEvent logRecord) {
if (logRecord.getMessage() == null) return;
String message = logRecord.getMessage().toString();
String message = logRecord.getMessage().getFormattedMessage();
val m = LOG_TYPE_PATTERN.matcher(message);
if (!m.find()) return;
String groupID = m.group(2); //The group ID is correctly cased
@ -59,7 +60,23 @@ public class TownyAnnouncer {
target = TBMCSystemChatEvent.BroadcastTarget.add("towny");
TownyAnnouncer.townChannel = townChannel;
TownyAnnouncer.nationChannel = nationChannel;
/*System.out.println(LogManager.getLogger("com.palmergames.bukkit.towny"));
((Logger) LogManager.getLogger("com.palmergames.bukkit.towny")).getContext().getConfiguration().addAppender(HANDLER);
System.out.println(((Logger) LogManager.getLogger("com.palmergames.bukkit.towny")).getAppenders());
((LoggerContext)LogManager.getContext(false)).updateLoggers();
System.out.println(((Logger) LogManager.getLogger("com.palmergames.bukkit.towny")).getAppenders());
((Logger) LogManager.getLogger("com.palmergames.bukkit.towny")).getContext().updateLoggers();
System.out.println(((Logger) LogManager.getLogger("com.palmergames.bukkit.towny")).getAppenders());*/
val lc = (LoggerContext) LogManager.getContext(false);
HANDLER.start();
lc.getConfiguration().addAppender(HANDLER);
Logger logger = lc.getLogger("com.palmergames.bukkit.towny");
//System.out.println(logger);
//System.out.println(lc.getConfiguration().<Appender>getAppender(HANDLER.getName())); //T defaults to String because of the context which results in a cast exception
logger.addAppender(lc.getConfiguration().getAppender(HANDLER.getName()));
logger.get().addAppender(HANDLER, Level.INFO, HANDLER.getFilter());
lc.updateLoggers();
//System.out.println(logger.getAppenders());
}
public static void setdown() {
@ -67,6 +84,6 @@ public class TownyAnnouncer {
target = null;
TownyAnnouncer.townChannel = null;
TownyAnnouncer.nationChannel = null;
((Logger) LogManager.getLogger("com.palmergames.bukkit.towny")).getContext().getConfiguration().getAppenders().remove(TownyAnnouncer.class.getSimpleName());
((Logger) LogManager.getLogger("com.palmergames.bukkit.towny")).getAppenders().remove(APPENDER_NAME);
}
}