<?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>
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 "";
}
}
<?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>
现在我看到了两个级别
:-
{"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"}
我不知道这是否可以通过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());
}
}
<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>
我有Spring Boot应用程序,并在应用程序中设置。下一个变量的属性: 应用程序启动后正确创建日志文件。但是当我更改日志级别时,logback会创建名为“appName _ IS _ UNDEFINED-appVersion _ IS _ UNDEFINED”的新日志文件。我使用下一个回退配置: 无法理解如何解决它。
logback-test 16:47:03.689[http-bio-8080-exec-5]调试org.jboss.Logging-日志提供程序:org.jboss.Logging.slf4JLoggerProvider logback-test 16:47:04.244[http-bio-8080-exec-5]DEBUG o.h.cfg.annotations.entityBinder lo
我有一个logback配置,它有一个带有阈值筛选器的附加器: 这确保只有信息和更高级别(警告、错误)被记录到syslog。但是,我们使用的第三方库之一是在调试时记录特定事件,我希望将此事件记录到syslog。我想到的第一个方法是尝试重新映射日志记录程序中的日志级别,但不确定这是否可能?类似于: 谢谢,
我尝试使用Logstash tcp套接字追加器将日志从java应用程序发送到Logstash。java应用程序。已经可以使用logback 1.1.9(slf4j)和其他追加器。 现在,我将以下行添加到logback-test.xml中:
我的logback.xml如下: 当我查看wireshark的输出时,我只看到记录的JSON(没有给出PRI头字段) 如果切换到标准logback Syslog appender(非JSON输出)
问题内容: 除了将日志记录级别设置为,我想使用默认的SLF4J + Logback配置。 我该如何使用Java代码呢? 我没有使用XML,而是在运行时做出此决定。 问题答案: 以下内容对我有用,但通常这不是一个好主意。您的代码将取决于Logback(您不能在SLF4J之后选择另一个日志框架)。