简体   繁体   English

Log4j2没有正确登录到graylog服务器

[英]Log4j2 not logging to graylog server properly

Right now I am trying use log4j2 to log everything that has a level of I NFO or higher (WARN, ERROR, AND FATAL) to my server and anything that has a level of INFO to my console . 现在我正在尝试使用log4j2将具有I NFO或更高级别 (WARN,ERROR和FATAL)级别的所有内容记录到我的服务器以及任何具有INFO级别的内容到我的控制台 I am able to log things to my console, however, I am having an issue logging the correct levels to the server properly. 我可以将内容记录到我的控制台,但是,我遇到了正确记录正确级别到服务器的问题。

Here is what I have tried so far: 这是我到目前为止所尝试的:

Java Java的

import java.time.Instant;

import org.apache.log4j.PropertyConfigurator;
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) {
        try {
            String log4jConfPath = "src/main/resources/log4j2.xml";
            PropertyConfigurator.configure(log4jConfPath);
            log4j.info("this is a testmessage " + Instant.now().toString());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

XML XML

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" packages="org.graylog2.log4j2">

    <Properties>
        <Property name="default_pattern">%d{MM/dd/yyyy hh:mm:ss} %5p %c{1} - %m%n
        </Property>
    </Properties>

    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="${default_pattern}" />
        </Console>
        <GELF name="gelfAppender" server="graylog.x.something.com"
            hostName="some.host" port="12201">
            <PatternLayout pattern="%d{dd MMM yyyy HH:mm:ss,SSS} %5p %c{1} - %m%n" />
            <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="error">
            <AppenderRef ref="gelfAppender" />
            <AppenderRef ref="console" />
        </Root>
        <Root level="info">
            <AppenderRef ref="gelfAppender" />
            <AppenderRef ref="console" />

        </Root>
    </Loggers>
</Configuration>

The above code does not output what I want, which is to have INFO level output to the console and INFO Levels and up output to the server . 上面的代码没有输出我想要的内容,即将INFO级别输出到控制台 ,将INFO级别输出到服务器

Instead, I am able to output: EVERYTHING (Trace) to console and INFO to the server. 相反,我能够输出:EVERYTHING(Trace)到控制台和INFO到服务器。

I have messed around with the XML file a little bit and I noticed that when I change 我稍微弄乱了XML文件,当我改变时,我注意到了

status="trace"

to

status="off"

it logs only INFO to the console, but nothing to the server. 它只将INFO记录到控制台,但没有记录到服务器。

Lastly, and probably the most odd thing of all, if I remove 最后,如果我删除,可能是最奇怪的事情

Instant.now().toString()

from my print statement, then nothing will be logged to the server, regardless of the status (if it's TRACE or OFF), but it still logs to the console. 从我的print语句中,无论状态如何(如果它是TRACE或OFF),都不会将任何内容记录到服务器,但它仍会记录到控制台。 I thought it had something to do with the pattern layout of my GELF appender, so I changed 我认为它与我的GELF appender的模式布局有关,所以我改变了

<PatternLayout pattern="%d{dd MMM yyyy HH:mm:ss,SSS} %5p %c{1} - %m%n" />

to

<PatternLayout pattern="${default_pattern}" />

but that did not change the output... 但这没有改变输出......

Here are my current dependencies: 这是我当前的依赖项:

在此输入图像描述

In short, I just want to log levels of INFO to my console and levels of INFO or higher to my server. 简而言之,我只想将INFO级别记录到我的控制台,将INFO级别或更高级别记录到我的服务器。

As you seem to have figured out you should use the threshold filter to filter messages of level WARN and up from the console appender. 正如您似乎已经想到的那样,您应该使用阈值过滤器从控制台appender过滤级别WARN及更高级别的消息。

The correct way to specify different levels for different appenders is to specify it on the <Appender> element like this: 为不同的appender指定不同级别的正确方法是在<Appender>元素上指定它,如下所示:

<Loggers>
    <Root level="trace">
        <Appender ref="console"/>
        <Appender ref="file" level="info"/>
    </Root>
</Logger>

As to solving your problem, here's a simple example program and configuration that outputs INFO level messages to console, all log messages to everything.log and INFO and up to infoAndUp.log . 至于解决您的问题,这里有一个简单的示例程序和配置,它将INFO级别的消息输出到控制台,所有日志消息发送到everything.logINFO以及最多的infoAndUp.log

LogTest.java : LogTest.java

public class LogTest {

    public static void main(String[] args) {
        final Logger log = LogManager.getLogger();

        log.trace("This is a trace message");
        log.debug("This is a debug message");
        log.info("This is an info message");
        log.warn("This is a warning message");
        log.error("This is an error message");
        log.fatal("This is a fatal message");
    }
}

log4j2.xml : log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="console">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Filters>
                <!-- Exclude messages logged above INFO -->
                <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
        </Console>
        <File name="everything" fileName="everything.log">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
        <File name="infoAndUp" fileName="infoAndUp.log">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="console" level="info"/>
            <AppenderRef ref="everything" />
            <AppenderRef ref="infoAndUp" level="info"/>
        </Root>
    </Loggers>
</Configuration>

Results 结果

Console output : 控制台输出

09:29:35.331 [main] INFO  LogTest - This is an info message

everything.log : everything.log

09:29:35.330 [main] TRACE LogTest - This is a trace message
09:29:35.331 [main] DEBUG LogTest - This is a debug message
09:29:35.331 [main] INFO  LogTest - This is an info message
09:29:35.332 [main] WARN  LogTest - This is a warning message
09:29:35.332 [main] ERROR LogTest - This is an error message
09:29:35.332 [main] FATAL LogTest - This is a fatal message

infoAndUp.log : infoAndUp.log

09:29:35.331 [main] INFO  LogTest - This is an info message
09:29:35.332 [main] WARN  LogTest - This is a warning message
09:29:35.332 [main] ERROR LogTest - This is an error message
09:29:35.332 [main] FATAL LogTest - This is a fatal message

You should be able to fix your own config using this as a base. 您应该能够以此为基础修复自己的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM