1

我正在使用 logstash-logback-encoder 以 json 格式打印日志。

我的logback.xml样子如下:-

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <includeContext>false</includeContext>
            <fieldNames>
                <timestamp>timestamp</timestamp>
                <version>[ignore]</version>
                <levelValue>[ignore]</levelValue>
            </fieldNames>
        </encoder>
    </appender>
    <appender name="stash"
        class="ch.qos.logback.core.rolling.RollingFileAppender">        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/tmp/SolrUpdater.%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <includeContext>false</includeContext>
            <fieldNames>
                <timestamp>timestamp</timestamp>
                <version>[ignore]</version>
                <levelValue>[ignore]</levelValue>
            </fieldNames>
        </encoder>
    </appender>
    <root level="error">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="stash" />
    </root>
</configuration>

有人可以让我知道如何level以小写形式显示所有内容error,而不是默认的ERRORWARNaswarning吗?

更新CustomLogLevelJsonProvider- 我按照 Zakhar 的建议创建了一个:-

package com.jabong.discovery.importer.solrUpdater.log;

public class CustomLogLevelJsonProvider extends LogLevelJsonProvider {
    final static String DEBUG = "debug";
    final static String ERROR = "error";
    final static String INFO = "info";
    final static String WARNING = "warning";

    @Override
    public void writeTo(JsonGenerator generator, ILoggingEvent event)
        throws IOException {
    JsonWritingUtils.writeStringField(generator, getFieldName(),
        getCustomLogLevel(event));
    }

    private String getCustomLogLevel(ILoggingEvent event) {
    if (event.getLevel() == Level.ALL) {
        return Level.ALL.toString();
    }
    if (event.getLevel() == Level.DEBUG) {
        return DEBUG;
    }
    if (event.getLevel() == Level.ERROR) {
        return ERROR;
    }
    if (event.getLevel() == Level.INFO) {
        return INFO;
    }
    if (event.getLevel() == Level.WARN) {
        return WARNING;
    }
    return "";
    }
}

更新logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
         <encoder class="com.jabong.discovery.importer.solrUpdater.log.CustomEncoder">
            <includeContext>false</includeContext>
            <fieldNames>
                <timestamp>timestamp</timestamp>
                <version>[ignore]</version>
                <levelValue>[ignore]</levelValue>
            </fieldNames>
        </encoder>
    </appender>
    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/tmp/SolrUpdater.%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <includeContext>false</includeContext>
            <fieldNames>
                <timestamp>timestamp</timestamp>
                <version>[ignore]</version>
                <levelValue>[ignore]</levelValue>
            </fieldNames>
        </encoder>
    </appender>
    <root level="error">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

现在我看到两个level:-

{"timestamp":"2015-12-26T23:41:08.818+05:30","message":"Partial Updater Initialization Failed ","logger_name":"com.jabong.discovery.importer.solrUpdater.partialupdate.PartialUpdater","thread_name":"main","level":"ERROR","stack_trace":"java.lang.Exception: Failed to Connect to SolrIndex Type:solr, Core:discovery, Host:localhost, Port:8888\n\tat com.jabong.discovery.importer.solrUpdater.partialupdate.PartialUpdater.initIndexUpdater(PartialUpdater.java:83) [classes/:na]\n\tat com.jabong.discovery.importer.solrUpdater.partialupdate.PartialUpdater.initialise(PartialUpdater.java:36) [classes/:na]\n\tat com.jabong.discovery.importer.solrUpdater.partialupdate.PartialUpdater.<init>(PartialUpdater.java:30) [classes/:na]\n\tat Updater.initialise(Updater.java:50) [classes/:na]\n\tat Updater.main(Updater.java:70) [classes/:na]\n","level":"error"}
4

2 回答 2

8

我不知道这是否可以通过 XML 以某种方式配置。但我只是试图覆盖你不喜欢的行为。您将需要实现两个新类。

1)

public class CustomEncoder extends LogstashEncoder {
    public CustomEncoder() {
        LoggingEventJsonProviders providers = getFormatter().getProviders();

        // Remove provider that is responsible for log level appending
        removeDefaultProvider(providers);

        // Register our implementation
        providers.addLogLevel(new CustomLogLevelJsonProvider());
    }

    private void removeDefaultProvider(LoggingEventJsonProviders providers) {
        JsonProvider<ILoggingEvent> providerToDelete = null;

        for (JsonProvider<ILoggingEvent> provider : providers.getProviders()) {
            if (provider instanceof LogLevelJsonProvider) {
                providerToDelete = provider;
                break;
            }
        }

        providers.removeProvider(providerToDelete);
    }
}

2)

public class CustomLogLevelJsonProvider extends LogLevelJsonProvider {
    @Override
    public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException {
        JsonWritingUtils.writeStringField(
                generator, getFieldName(), event.getLevel().toString().toLowerCase());
    }
}

3)在配置中启用新的编码器。

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="com.test.encoder.CustomEncoder">
        <includeContext>false</includeContext>
        <fieldNames>
            <timestamp>timestamp</timestamp>
            <version>[ignore]</version>
            <levelValue>[ignore]</levelValue>
        </fieldNames>
    </encoder>
</appender>

更新:您已更新您的问题。您将需要一些额外的逻辑来将 WARN 或警告转换为警告。由于这将发生在每条日志消息上,这将产生开销。您要更改的值在类中是硬编码的:ch.qos.logback.classic.Level

于 2015-12-26T14:18:34.083 回答
1

更好地使用 Logstash 并在“级别”字段上应用小写过滤器。

于 2020-01-12T11:36:45.580 回答