我使用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
,而不是将默认ERROR
和WARN
显示为warning
?< / p>
更新 - 我按照Zakhar的建议创建了CustomLogLevelJsonProvider
: -
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"}
答案 0 :(得分:3)
我不知道这是否可以通过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
答案 1 :(得分:1)
最好使用Logstash并将小写过滤器应用于“级别”字段。