我正在使用 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
aswarning
吗?
更新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"}