How to colorize Log4j2 output on console in intelliJ?
Asked Answered
A

5

26

I've tried to change the config file to like below but still, the output is plain white. How can I change it to any color? Like different color for each level.

Code:

import org.apache.log4j.*;

public class StartUp {

    private static final Logger LOGGER = Logger.getLogger(Class.class.getName());

    public static void main(String[] args) throws Exception {

        LOGGER.trace("Trace Message!");
        LOGGER.debug("Debug Message!");
        LOGGER.info("Info Message!");
        LOGGER.warn("Warn Message!");
        LOGGER.error("Error Message!");
        LOGGER.fatal("Fatal Message!");

Config file (log4j2.xml):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%highlight{[%d] - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="ALL">
            <AppenderRef ref="CONSOLE"/>
        </Root>
    </Loggers>
</Configuration>
Azotobacter answered 27/1, 2018 at 2:38 Comment(0)
P
64

It seems like some default is broken in 2.10.0. By adding disableAnsi options, I could get the colors back with the last release.

<PatternLayout pattern="%highlight{...}" disableAnsi="false"/>

In the docs, it is said to default to false but it doesn't seem the case.

Purchasable answered 15/2, 2018 at 23:1 Comment(5)
The default was changed in Log4j 2.10 because the native code in jansi was causing issues for some users. The documentation should have been updated but perhaps some places were missed. Could you raise a ticket on the Log4j2 issue tracker to fix the docs? Pull request would be even more awesome.Letishaletitia
This is the correct answer. After changing disableAnsi to "false" my colors start appearing again. Maybe the issues other people were having was due to the fact that they tried to write the pattern of colors into log files? This coloring works only in the console/terminal. You should just configure regular logs without coloring into the logs files themselves and use tools like Splunk when working in Production.Lacunar
why does it print disableAnsi=false in the console?Parlour
You probably have left it in the pattern, between quotes. It's an xml parameter.Purchasable
In fact, the default value is dependent on platform (true for Windows, false otherwise) and default can be also overridden for Windows by specifying log4j.skipJansi (defaults to true now). It can be seen in the source code of PatternLayout.java, but Log4j authors seem to completely forget to update documentation accordingly.Battledore
R
9

For IntelliJ I can highly recommend the Grep Console Plugin.

It can parse console output for logs and much more without changing the source code.

enter image description here

Reason answered 27/10, 2020 at 13:34 Comment(0)
H
6

I took both the question and the answer and I have created a colored output similar to the default logback.

Config file (log4j2.xml):

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN" monitorInterval="30">
        <Properties>
            <Property name="CLR">{FATAL=bright red, ERROR=red, WARN=bright yellow, INFO=Normal, DEBUG=white, TRACE=black}</Property>
            <Property name="LOG_PATTERN">
                %highlight{%5p- %d{yy-MM-dd HH:mm:ss.SSS}}${CLR} %clr{${sys:PID}}{magenta}%clr{-}{faint}%clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan}  %highlight{: %m%n%xwEx}${CLR}
            </Property>
        </Properties>
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT" follow="true">
                <PatternLayout pattern="${LOG_PATTERN}" disableAnsi="false"/>
            </Console>
        </Appenders>
    
        <Loggers>
            <logger name="org.springframework.boot.autoconfigure.logging" level="info"/>
            <Root level="debug">
                <AppenderRef ref="Console"/>
            </Root>
        </Loggers>
    </Configuration>

enter image description here

Hypertonic answered 20/2, 2022 at 20:17 Comment(0)
H
4

Use log4j2's stable version 2.9.1 and replace LOGGER initialization with

private static final Logger LOGGER = LogManager.getLogger(Class.class.getName());

Additional documentation about highlighting your console appender: https://logging.apache.org/log4j/2.x/manual/layouts.html

Halfway answered 27/1, 2018 at 2:45 Comment(5)
did you read my config file ? its literally copy-paste of you the question you mentioned, but with different the format.Azotobacter
Yeah.Let me verify your configuration on my intellij instance.Halfway
Your log4j2 configuration is correct. It works with log4j2 2.9.1 version and doesnt working on log4j2 2.10.1 version.Halfway
oh, im actually using 2.10.1, ill downgrade to 12.9 thank you :)Azotobacter
see the more upvoted answer, better than downgradingRainie
B
1

I'll try to accumulate solutions for this issue, I apologize if I repeated. What should be done to enable the color of the logs in the console (valid only for versions lo4j2 from 2.10, since jansi has been disabled by default)

1) Add jansi dependency:

<dependency>
  <groupId>org.fusesource.jansi</groupId>
  <artifactId>jansi</artifactId>
  <version>1.16</version>
</dependency>

2) Add VM option: -Dlog4j.skipJansi=false

3) And don't forget add %highlight inside pattern (yaml example below):

Configuration:
  Appenders:
    Console:
      PatternLayout:
        pattern: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight{%-5level}{STYLE=Logback} %logger.%M - %msg%n'
Blodget answered 21/3, 2019 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.