删除文件追加器后,Log4j2不会登录到服务器 [英] Log4j2 does not log to server after removing file appender

查看:131
本文介绍了删除文件追加器后,Log4j2不会登录到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题,我不明白:

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

当我删除我的文件附加程序时,它阻止我登录到我的服务器,即使文件appender不应该负责将任何内容记录到服务器;该任务应仅适用于我的GELF appender。

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>

然而,当我删除此

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

<AppenderRef ref="everything" />

它不再向我的服务器记录任何内容。

it no longer logs anything to my server.

File appender和logger不应该只记录到文件而不是服务器吗?如果是这样,为什么删除文件记录器和appender阻止我登录到服务器?

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?

推荐答案

你试过使用Log4j中的GelfLayout?

Have you tried to use the GelfLayout in Log4j?

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

这篇关于删除文件追加器后,Log4j2不会登录到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆