Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to colorize Log4j2 output on console in intelliJ?

Tags:

colors

log4j2

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>
like image 675
user2962142 Avatar asked Jan 27 '18 02:01

user2962142


People also ask

How do I use console Appenders in Log4j2?

Log4j2 ConsoleAppender ConfigurationThe target poperty specifies the target of the logging messages i.e. SYSTEM_OUT or SYSTEM_ERR . The follow attribute tells whether the appender should honor the reassignments of System. out or System. err made after the logging configuration has been initialized.

Where should I set Log4j2 formatMsgNoLookups to true?

You can do this: %m{nolookups} in the layout. {nolookups} is how you set the property log4j2. formatMsgNoLookups=true within the configuration XML content.

What is configuration status in Log4j2?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.


1 Answers

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.

like image 124
namero999 Avatar answered Sep 17 '22 11:09

namero999