Search code examples
xmlspringspring-bootmavenlogback

Configure different colors for different log levels with logback


I have a spring boot project with maven and i have set up a logback.xml to write logs for example for my login to a file, and logs to the console which is working fine

now i am trying to get colors for different levels but if i use %highlight it seems like it automatically uses some coloring that is set somewhere because it does not take mine, ive tried different ways of specifying it

when i removed my highlight tags and content (which is supposed to give me an error?) it still built and used the same colors which are e.g. DEBUG white, INFO cyan

would be really helpful if anyone could help me set custom colors, ty in advance

i removed the code for my file appender, here the part for the console:

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>
            %d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{15}) - %msg %n
        </pattern>
        <highlight>
        <color>TRACE=WHITE</color>
        <color>DEBUG=GREEN</color>
        <color>INFO=GREEN</color>
        <color>WARN=YELLOW</color>
        <color>ERROR=RED</color>
    </highlight>
    </encoder>
</appender>

<logger name="at.qe.backend.api.services.AuthService" additivity="false" level="info">
    <appender-ref ref="FILE1" />
</logger>

<root level="DEBUG">
    <appender-ref ref="CONSOLE" />
</root>

The console looks like this

the console looks like this

Edit: i found this https://github.com/shuwada/logback-custom-color but it only works for the console logs if i use it for my logs to file aswell i looks like this for some reason

no idea why


Solution

  • It may sound dumb but i think the solution is that it is not possible, since the .log file where i write my logs to is just a plain text file so it is not possible to set the colors of the text, correct me if im wrong