javalogginglog4jlog4j2apache-chainsaw

How to open log4j2 logs with Apache Chainsaw


I'm having hard time opening my logs with Apache Chainsaw v2. I wasn't able to succesfully open my logs with zeroconf, neither with default xmllayout. It worked like a charm for me in .net. But in Java it's a nightmare.

Main class:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class Start {

    public static Logger logger = LogManager.getLogger(Start.class);

    public static void main(String[]args){
        logger.info("Example log info");
        logger.trace("Example log trace");
        logger.debug("Example log debug");
        logger.error("Example log error");
        logger.fatal("Example log fatal");
        logger.warn("Example log warn");
        return;
    }
}

Log4j2 configuration

    <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ALL" advertiser="multicastdns">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="custom layout %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <File name="log4j2Chainsaw" fileName="output.log" bufferedIO="false" advertiseURI="file:///C:/Users/gswiec/IdeaProjects/Log4j2ChainsawExample/output.log" advertise="true">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
        <File name="xmlLayoutForChainsaw" fileName="output.xml" bufferedIO="false" >
           <XmlLayout/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="log4j2Chainsaw"/>
            <AppenderRef ref="xmlLayoutForChainsaw"/>
        </Root>
    </Loggers>
</Configuration>

Here is pom, all needed dependencies seems to be there.

 <?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>swiec.eu</groupId>
    <artifactId>Log4j2ChainsawExample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--Log4j2 and apache chainsaw zeroconf dependencies-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>javax.jmdns</groupId>
            <artifactId>jmdns</artifactId>
            <version>3.4.1</version>
        </dependency>

        <!--XmlLayout dependencies-->
        <dependency>
            <groupId>com.fasterxml.woodstox</groupId>
            <artifactId>woodstox-core</artifactId>
            <version>5.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>

</project>

Whole example project is available at https://github.com/swiec/eu.swiec.log4j2apacheexample

I would appreciate your help, also if you suggest different approach to get logs more readable. Apache chainsaw doesn't seems to be live project, I'm not sure if it's good idea using it.


Solution

  • You should test with an app that doesn't start and immediately exit, or it won't have time to advertise & have Chainsaw receive events.

    I just added this to your Start.java file:

    import java.util.Scanner;
    ...
        public static void main(String[]args){
            Scanner in = new Scanner(System.in);
            int i = in.nextInt();
    

    I also updated your log4j config - you need to use %m not %msg for the PatternLayout used to generate output.log

            <File name="log4j2chainsawappender" fileName="output.log" bufferedIO="false" advertiseURI="file://Users/scott/eu.swiec.log4j2apacheexample/output.log" advertise="true">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m"/>
    

    I then started Chainsaw v2 (very latest version in git), and started your app.

    Once your app was started, I selected Chainsaw's 'connect to, log4j2chainsawappender', and a new tab appeared and correctly formatted your log events, parsing 'Start' as your logger, correct severity levels etc.