0
votes

I'm using Java 11, Spring Boot 2.4.1, Intellij Idea Community Edition 2019.2. I'm using Log4j2 and trying to change color of log level in console from log4j2.xml configuration file. And I have success if I run my application from public static void main(String[] args), but if I run my app from terminal with: 'mvn spring-boot:run', then it doesn't work.

My log4j2.xml:

<Configuration strict="true">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{DEFAULT} %highlight{%-5p}{DEBUG=blue,INFO=black,WARN=yellow} [%c{1}.%M():%L %t] [${bundle:application:spring.application.name}] %m%n"
                    disableAnsi="false" charset="UTF-8"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="Console"/>
        </Root>
        <Logger name="com.example" level="TRACE" includeLocation="true" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
        <Logger name="org.hibernate" level="ERROR" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
        <Logger name="org.hibernate.SQL" level="ERROR" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
        <Logger name="org.springframework" level="INFO" includeLocation="true" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
    </Loggers>
</Configuration>

If I run with public static void main(String[] args), I have e.g. DEBUG log and color of word DEBUG is blue:

2021-03-08 19:04:56,357 DEBUG [MyApplication.logStarting():56 main] [My-App-name] Running with Spring Boot v2.4.1, Spring v5.3.2

If I run in terminal with 'mvn spring-boot:run', there are no colors and log is wrong:

2021-03-08 19:15:16,450 ←[34mDEBUG←[m [MyApplication.logStarting():56 main] [My-App-name] Running with Spring Boot v2.4.1, Spring v5.3.2

How can I fix it? If you need any clarification please let me know, thank you in advance!

EDIT:

Project Structure (It's my sanbox app, where I train different things). Also you can see here how I connect log4j2 to the project in pom.xml: enter image description here


My build section in pom.xml: enter image description here


Logs, if run public static void main: enter image description here


Logs, if run 'mvn spring-boot:run' (2 pictures): enter image description here | enter image description here


I tried to use another approach with color of '%-5p':

  1. Pattern: <PatternLayout pattern="%d{DEFAULT} %clr{%-5p}{magenta} [%c{1}.%M():%L %t] [${bundle:application:spring.application.name}] %m%n" charset="UTF-8"/>
  2. Added to application.properties: spring.output.ansi.enabled=ALWAYS

But it still works only for run from 'main' and not for terminal.

Could you add your project structure with both full launch methods logs. - tmarwen
Are you using Windows? - Simon Martinelli
@tmarouane sure, added. - Georgy Lvov
@SimonMartinelli yes, windows 10 x64 - Georgy Lvov
Does your project include the jansi jar? - rgoers