Reputation: 2865
Has anyone figured out how to get color output working within Intellij Idea for a Spring Boot application?
Upvotes: 89
Views: 55136
Reputation: 2865
Using a Mac, Intellij Idea 14 and Spring Boot v1.2.2.RELEASE, all you have to do is set in the application.properties
file:
spring.output.ansi.enabled=ALWAYS
I have added this as a VM option (-Dspring.output.ansi.enabled=ALWAYS
). Works great!
Upvotes: 162
Reputation: 171
Here is my source of a problem and a solution.
I had two implementation of logging frameworks, which of I was notified immediately after the app was started. You can have even more - all of them are coming from various dependencies. So the message was:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:.../slf4j-simple-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:.../logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
As you can see there are two found bindings point to two logger frameworks. Each of them is using their own color schema. So which one will be chosen by Spring boot ? According to this tutorial it will get the last one from the list. Which means that your color schema depends now on some pleasantly random choice (have no time to investigate how the list assembles).
After gathering that information about found logging framework bindings I sequentially removed redundant ones from my dependencies.
You can build a maven tree to find the dependency which pulls into your project unwanted SLF4j binding and then exclude it in a maven or gradle configuration file.
Since I am using gradle I just added this configuration to my build.gradle
configurations {
all {
exclude group: 'org.slf4j', module: 'slf4j-simple' // no ansi colors in terminal (:
}
}
This exclusion removes unwanted bindings and returned back my ansi colors to intellij idea terminal (since the only one logging framework left over in my project - and this one has colored output).
Have to notice: if I start my app via mac os terminal by
java -jar fileName.jar
command I do not have colored output.
Upvotes: 2
Reputation: 4371
With newer versions of IntelliJ (2019) and Spring Boot (2.0) when running the a Spring Boot application inside IntelliJ colour logging is correctly output, however when running unit tests no console is detected and so colour logging isn't used. To force Spring Boot to always consider there to be a console even when it can't find one set the following property:
spring.output.ansi.console-available=true
Unlike spring.output.ansi.enabled=ALWAYS
this leaves the detection code running (so no colour if you're on windows) but causes colour logging to happen in tests (both in IntelliJ and when running with Maven).
Upvotes: 0
Reputation: 553
Simply by adding those properties to application.properties for IntelliJ IDEA:
spring.main.banner-mode=off
spring.output.ansi.enabled=ALWAYS
Upvotes: 20
Reputation: 6036
In application.properties use (for example) the following line:
logging.pattern.console= %d{yyyy-MMM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{15}) - %msg %n
If you would like almost similar to Spring Boot you can use pattern like this:
%date %highlight(%-5level) [%12.12thread] %cyan(%-40.40logger{40}) : %msg %n
Upvotes: 25
Reputation: 48813
Generic way to enable logging color support on any condition with Gradle:
bootRun {
def console = System.console() != null
if (! console) { console = System.getenv()["TERM"].startsWith("xterm") }
if (console) systemProperties 'spring.output.ansi.enabled': 'always'
}
Upvotes: 2
Reputation: 71
Upvotes: 7