Add custom Log4j2 RollingRandomAccessFile

Discussion in 'Spigot Plugin Development' started by m10653, Jun 29, 2016.

  1. So I have been looking into how Logging works and I want to be able to programmatic add a RollingRandomAccessFile or FileAppender to log filtered messaged. I know how to Add them though a config.
    Code (Text):
     <RollingRandomAccessFile name="File" fileName="logs/latest.log" filePattern="logs/%d{yyyy-MM-dd}-%i.log.gz">
                <PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level]: %msg%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy />
                    <OnStartupTriggeringPolicy />
                </Policies>
            </RollingRandomAccessFile>
     
    This is the default Minecraft config.
    I also was able to programmaticly add a Appender, but if I try to write anything to a file I get a Error. (ERROR Recursive call to appender)
    Code (Text):
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.apache.logging.log4j.core.LogEvent;
    import org.apache.logging.log4j.core.appender.AbstractAppender;
    import org.apache.logging.log4j.core.config.plugins.Plugin;
    import org.apache.logging.log4j.core.layout.PatternLayout;

    @Plugin(name = "Foo", category = "Core", elementType = "appender", printObject = true)
    public class Foo  extends AbstractAppender {
        static Logger log = LogManager.getLogger(Foo.class.getName());

        public Foo() {
            super("Foo", null, PatternLayout.createLayout("[%d{HH:mm:ss} %level]: %msg", null, null, null, null), false);
         
        }

        @Override
        public boolean isStarted() {
            return true;
        }

        @Override
        public void append(LogEvent e) {
            if (e.getMessage().getFormattedMessage().contains("Foo")) {
                //Somthing Happened
            }
        }
    }
    I
     
  2. Bump, Can anyone help we with how Spigot and mine craft does logging? I have looked a the documentation and programmatily adding stuff seems to modify the config.