1

Today I have started using LogBack in combination with SLF4J for my project. I have been trying to redirect all console output (From my own project and others) to a custom appender.

The final goal is to redirect all the output to a Discord chat channel.

I have looked into the following approach: java runtime.getruntime() getting output from executing a command line program but it wasn't successful.

I am aiming for the following structure:

  1. Console message is sent by another library
  2. Catch the message and process it

I have tried the following implementation:

public class DiscordAppender extends AppenderBase<ILoggingEvent> {
    @Override
    protected void append(ILoggingEvent eventObject) {
        SomeDiscordClient.sendMessage(specificChannel, eventObject.getFormattedMessage());
    }
}

With the following logback.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="DISCORD" class="package.DiscordAppender"/>

    <root level="TRACE">
        <appender-ref ref="DISCORD"/>
    </root>
</configuration>

But this approach is not working, hopefully someone can help me out.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jordie
  • 134
  • 1
  • 2
  • 10
  • Simpler suggestion - avoid outputting to the console, output to the logger only. – Boris the Spider Apr 15 '18 at 18:24
  • @BoristheSpider how would i do that when i don't have control over the other output sources (In this case [Bukkit](https://bukkit.org/)) – Jordie Apr 15 '18 at 18:28
  • [`System.setOut`](https://docs.oracle.com/javase/10/docs/api/java/lang/System.html#setOut(java.io.PrintStream))? Or, even better, use [their recommendations](http://projects.lidalia.org.uk/sysout-over-slf4j/faq.html). – Boris the Spider Apr 15 '18 at 18:29
  • I have tried the implementation, it works but it doesn't catch everything that gets send, [this](https://github.com/Scarsz/DiscordSRV/blob/master/src/main/java/github/scarsz/discordsrv/objects/log4j/ConsoleAppender.java) is what i am trying to achieve but using LogBack – Jordie Apr 15 '18 at 18:57

0 Answers0