Adding custom appender to Bukkit on runtime

Discussion in 'Plugin Development' started by Chloe-chan, Feb 2, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Chloe-chan

    Hi all,

    I wanted to add a custom appender to Bukkit's internal Log4J2 on runtime to capture the LogEvents. However, when I include the latest Log4J2 (2.8) and shaded it into my plugin's JAR, I received the following error.
    Code:
    ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
    This is the configuration in the POM I've used to shade the JAR.
    Code:
    <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
             <execution>
               <phase>package</phase>
               <goals>
                 <goal>shade</goal>
               </goals>
               <configuration>
                 <relocations>
                   <relocation>
                     <pattern>org.apache.logging</pattern>
                     <shadedPattern>com.gmail.surreal.cle.plugins.testlogging.library.org.apache.logging</shadedPattern>
                   </relocation>
                   <relocation>
                     <pattern>org.apache.commons</pattern>
                     <shadedPattern>com.gmail.surreal.cle.plugins.testlogging.library.org.apache.commons</shadedPattern>
                   </relocation>
                 </relocations>
               </configuration>
             </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
    The code I've tried to use is as shown here.
    Code:
            PatternLayout layout = PatternLayout.createDefaultLayout();
            Appender appender = new AbstractAppender("CustomAppender", null, layout)
            {
                @Override
                public void append(LogEvent logEvent)
                {
                    loggerMessages.add(logEvent);
                }
            };
            appender.start();
            LoggerContext context = (LoggerContext) LogManager.getContext(false);
            Configuration config = context.getConfiguration();
            config.addAppender(appender);
    
    This has also caused additional errors to be logged in the console.
    Code:
    [16:58:10 ERROR]: Error occurred while enabling TestLogging v1.0 (Is it up to date?)
    java.lang.ClassCastException: com.gmail.surreal.cle.plugins.testlogging.library.org.apache.logging.log4j.simple.SimpleLoggerContext cannot be cast to com.gmail.surreal.cle.plugins.testlogging.library.org.apache.logging.log4j.core.LoggerContext
            at com.gmail.surreal.cle.plugins.testlogging.MDMain.onEnable(MDMain.java:137) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.loadPlugin(CraftServer.java:362) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugins(CraftServer.java:322) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
            at net.minecraft.server.v1_10_R1.DedicatedServer.init(DedicatedServer.java:204) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:535) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    
    Any help to add custom appenders would be greatly appreciated.
     
Thread Status:
Not open for further replies.

Share This Page