5

Can't quite find an answer to this, can anyone state a fix.

I have a library jar i'm building and using logback (logabck 1.2.3, jansi 1.16) and groovy, and i'm trying to colour code the log display - as spring boot manages to do - but i didn't want spring boot as dependency for this library i'm building.

I have included the jansi (1.16 as stated in logabck docs) library as dependency to the gradle build.

the relevant section of my logback.groovy looks like this

appender('STDOUT', ConsoleAppender) {
    withJansi = true
    encoder(PatternLayoutEncoder) {
        charset = Charset.forName('UTF-8')
        pattern = consolePatternFormat
    }
} 

when i run a test i get and error (with jansi enabled) like this

16:17:42,344 |-WARN in com.softwood.logging.logback.AnsiConsoleAppender[STDOUT] - Failed to create WindowsAnsiOutputStream. Falling back on the default stream. ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.fusesource.jansi.WindowsAnsiOutputStream
    at ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type org.fusesource.jansi.WindowsAnsiOutputStream
    at  at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:69)
    at  at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:40)
    at  at ch.qos.logback.core.ConsoleAppender.getTargetStreamForWindows(ConsoleAppender.java:88)
    at  at ch.qos.logback.core.ConsoleAppender.start(ConsoleAppender.java:79)
...
Caused by: java.lang.reflect.InvocationTargetException
    at  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at  at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:64)

there was a suggestion here https://jira.qos.ch/browse/LOGBACK-762 that all one needed to do was to wrap the output stream, so i created a custom console appender in my project like this

import java.io.OutputStream;
import org.fusesource.jansi.AnsiConsole;
import ch.qos.logback.core.ConsoleAppender;

public class AnsiConsoleAppender<E> extends ConsoleAppender<E> {

    @Override
    public void setOutputStream(OutputStream outputStream) {
        super.setOutputStream(AnsiConsole.wrapOutputStream(outputStream));
    }
}

and modified the logback.groovy appender to to use the AnsiConsoleAppender like this

 appender('STDOUT', AnsiConsoleAppender) {
        withJansi = true
        encoder(PatternLayoutEncoder) {
            charset = Charset.forName('UTF-8')
            pattern = consolePatternFormat
        }
    }

however this fails the same way as before. If i disable the jansi support - it all works as you'd expect. When I enable with jansi and run tests then these fail as shown

Colour logging works with springboot and you get the coloured console, so someone knows how to do this. But i don't know what the logback problem is

has any one got the basic jansi colour coding for logback (1.2.3) under windows to work ? If so whats the secret.

For now i've had to disable, but would like to get this to work. Bit frustrating

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
WILLIAM WOODMAN
  • 1,185
  • 5
  • 19
  • 36

2 Answers2

6

I fixed this same problem by REMOVING withJansi

<!-- <withJansi>true</withJansi> -->
Ian
  • 1,507
  • 3
  • 21
  • 36
1

GOTO> TOMCAT installation folder

You will find this setting in logback.xml file which is inside TOMCAT_INSTALL_DIR/conf/logback.xml

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <!--  <withJansi>${exo.logs.jansi.console:-false}</withJansi>-->
    <encoder>
      <pattern>${exo.logs.console.appender.pattern:-${exo.logs.default.pattern}}</pattern>
    </encoder>
  </appender>

clean tomcat and run again will resolve your problem definetely.

Dheeraj Upadhyay
  • 336
  • 2
  • 12