Log4j2 does not log to server after removing file

2019-06-05 23:04发布

问题:

I have come across an odd problem, which I do not understand:

when I remove my File appender it prevents me logging to my server, even though the File appender should not be responsible for logging anything to the server; that task should only be for my GELF appender.

The following code is able to log both to my console and server just fine

Java

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

public class App {
    private static final Logger log4j = LogManager.getLogger(App.class.getName());

    public static void main(String[] args) {
        log4j.info("This is my info message");
        log4j.warn("This is my warning message");
        log4j.error("This is my error message");
        log4j.fatal("This is my fatal message");
    }
}

XML

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">

    <Properties>
        <Property name="default_pattern">%d{MM/dd/yyyy hh:mm:ss} %5p %c{1} - %m%n </Property>
    </Properties>
    <Appenders>
        <Console name="console">
            <PatternLayout pattern="${default_pattern}" />
            <Filters>
                <!-- Exclude messages logged above INFO -->
                <ThresholdFilter level="WARN" onMatch="DENY"
                    onMismatch="NEUTRAL" />
            </Filters>
        </Console>

        <File name="everything" fileName="everything.log"> <!--trying to remove this-->
            <PatternLayout pattern="${default_pattern}" />
        </File>

        <GELF name="gelfAppender" server="graylog.x.something.com"
            hostName="some.host" port="12201">
            <PatternLayout pattern="${default_pattern}" />
            <KeyValuePair key="extractStacktrace" value="true" />
            <KeyValuePair key="addExtendedInformation" value="true" />
            <KeyValuePair key="facility" value="gelf-java" />
            <KeyValuePair key="environment" value="Test" />
            <KeyValuePair key="application" value="MyApp" />
            <KeyValuePair key="additionalFields" value="{'environment': 'Test', 'application': 'MyAPP'}" />
        </GELF>

    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="console" level="info" />
            <AppenderRef ref="everything" /><!--and trying to remove this-->
            <AppenderRef ref="gelfAppender" level="info" />
        </Root>
    </Loggers>
</Configuration>

However, when I remove this

<File name="everything" fileName="everything.log">
    <PatternLayout pattern="${default_pattern}" />
</File>

and

<AppenderRef ref="everything" />

it no longer logs anything to my server.

Shouldn't the File appender and logger only log to a file and not the server? If so, why would removing the File logger and appender prevent me from logging to the server?

回答1:

Have you tried to use the GelfLayout in Log4j?

http://logging.apache.org/log4j/2.x/manual/layouts.html#GELFLayout



回答2:

You may have found a bug. Please raise a ticket for this on the Log4j 2 issue tracker.