ansi colors - Logback: use colored output only when logging to a real terminal -


in logback configuration have 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 , errors show in terminal, colored, while main log file can contain more information, e.g. info , debug levels.

generally, works fine. but, when run emacs or other "not terminal" program, coloring commands show out ascii escape sequences, e.g. ^[[31m warning highlighting. somehow possible make logback use ansi coloring when connected real terminal?

you have 2 problems here:

how detect if should use colors or not

this not trivial. this answer suggests use jni call isatty detect if you're connected terminal, it's lot of work low-priority feature.

how conditionally use colors in logback

that quite easy (official docs), remember need janino work:

<configuration>     <appender name="color" class="ch.qos.logback.core.consoleappender">         <encoder>             <pattern>[%date] %highlight([%level]) [%logger{10} %file:%line] %msg%n</pattern>             <!--             ^^^^^^^^^^ -->         </encoder>     </appender>     <appender name="nocolor" class="ch.qos.logback.core.consoleappender">         <encoder>             <pattern>[%date] [%level] [%logger{10} %file:%line] %msg%n</pattern>         </encoder>     </appender>     <root level="debug">         <!-- use enable mode pass -dcolor jvm -->         <if condition='isdefined("color")'>             <then>                     <appender-ref ref="color"/>             </then>             <else>                     <appender-ref ref="nocolor"/>             </else>         </if>     </root> </configuration> 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -