'Coloring logs in logback files

I'm struggling with a logback.xml file. I want to write the code that will color both logs on a console and logs in a registered file. I have no problem coloring console logs but when I add a color to the file code I see this error([ESC]):

[main] INFO [ESC][31mBaseTest.clas[ESC][0;39m - Driver closed properly.

Here is my code:

<configuration scan = "true">
<property name="DEV_HOME" value="${user.dir}/log"/>
<timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <withJansi>true</withJansi>
    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
        <layout class="chapters.layouts.MySampleLayout" />
    </encoder>
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} %boldBlue(%-5level) - %boldYellow(%msg%n)
        </Pattern>
    </layout>

</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <withJansi>true</withJansi>
    <file>${DEV_HOME}/timeBasedlogFile_${bySecond}.log</file>  
    <append>false</append>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${DEV_HOME}/timeBasedlogFile.%d{yyyy-MM-dd-HH-mm}.zip</fileNamePattern>
        <maxHistory>3</maxHistory>
        <totalSizeCap>30KB</totalSizeCap>
    </rollingPolicy>
    <encoder>
        <pattern>[%thread] %-5level %logger{35} - %blue(%msg%n)</pattern>
    </encoder>
</appender>

<root level="info">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="FILE"/>
</root>


Solution 1:[1]

In File appender remove <withJansi>true</withJansi> and in the pattern instead of %blue(%msg%n) use %msg%n

Those escape sequence are added by Jansi and it is used in the terminal/cmd to colorize the logs you don't need in in file appender.

So the file appender should be as below.

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${DEV_HOME}/timeBasedlogFile_${bySecond}.log</file>  
    <append>false</append>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${DEV_HOME}/timeBasedlogFile.%d{yyyy-MM-dd-HH-mm}.zip</fileNamePattern>
        <maxHistory>3</maxHistory>
        <totalSizeCap>30KB</totalSizeCap>
    </rollingPolicy>
    <encoder>
        <pattern>[%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karthikeyan