Is there a way to color-code Log4J2 in Windows Powershell?

31 Views Asked by At

I want to color code my Log4J logs. I tried everything I could, used every answer on Stack Overflow, Google and GPT, and still it doesn't work.

This is my log4j2.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <File name="File" fileName="logs/info.log">
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
        
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss} [%t] %highlight{%-5level}{FATAL=magenta, ERROR=red, WARN=yellow, INFO=white, DEBUG=blue, TRACE=blue} %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    
    <Loggers>
        <Root level="info">
            <AppenderRef ref="File"/>
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>me.eduardogottert</groupId>
    <artifactId>hyperbot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <mainClass>me.eduardogottert.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.17.1</version>
        </dependency>


        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.1</version>
        </dependency>


        <dependency>
            <groupId>org.javacord</groupId>
            <artifactId>javacord</artifactId>
            <version>3.8.0</version>
            <type>pom</type>
        </dependency>

    </dependencies>
</project>

This is my Main class, that I'm using to test the logger:

package me.eduardogottert;

import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.intent.Intent;
import org.javacord.api.entity.permission.Permissions;
import org.javacord.api.entity.server.Server;
import me.eduardogottert.commands.utils.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Collection;

public class Main {

    private static Logger logger = LogManager.getLogger(Main.class);

    public final static char prefix = '!';
    public static void main(String[] args) {
        final String BOT_TOKEN = "*hidden*";
        
        DiscordApi api = new DiscordApiBuilder().setToken(BOT_TOKEN).addIntents(Intent.MESSAGE_CONTENT).login().join();
        
        Collection<Server> guilds = api.getServers();
        
        logger.info("Logged in as " + api.getYourself().getName());
        logger.info("Client ID (" + api.getClientId() + ")");
        logger.info(api.createBotInvite(Permissions.fromBitmask(8)));
        logger.info("Currently in " + guilds.size() + " guild(s): " + guilds);

        logger.trace("test");
        logger.debug("test");
        logger.info("test");
        logger.warn("test");
        logger.error("test");
        logger.fatal("test");  
    }
}

This is the result I got: My console log after running the above code

I was expecting that the word "FATAL" was magenta, "ERROR" was red, "WARN" was yellow and "INFO" was white.

0

There are 0 best solutions below