In my Logback configuration I have the following lines:
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%highlight(...) %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
</appender>
This makes warnings and errors show up in terminal, colored, while main log file can contain much more information, e.g. INFO and DEBUG levels.
Generally, this works fine. But, when I run it from Emacs or any other "not really a terminal" program, coloring commands show out as ASCII escape sequences, e.g. ^[[31m for warning highlighting. Is it somehow possible to make Logback only use ANSI coloring when connected to a real terminal?
You have two problems here:
how to detect if you should use colors or not
This is not trivial. As this answer suggests you could use JNI call to isatty to detect if you're connected to a terminal, but it's a lot of work for fairly low-priority feature.
how to conditionally use colors in logback
That is actually quite easy (official docs), remember that you need janino for this to work: