受这种Java 类方法的启发,我正在尝试一种基于 XML 的方法,用于直接通过logback-spring.xml
. 这失败了,日志没有到达 ELK。可能是什么原因?任何帮助将不胜感激。
@Configuration 方法
private void addLogstashAppender(LoggerContext context) {
LogstashTcpSocketAppender logstashAppender = new LogstashTcpSocketAppender();
logstashAppender.setName(LOGSTASH_APPENDER_NAME);
logstashAppender.setContext(context);
String customFields = "{\"app_name\":\"" + appName + "\",\"app_port\":\"" + serverPort + "\"}";
LogstashEncoder logstashEncoder=new LogstashEncoder();
// Set the Logstash appender config from JHipster properties
logstashEncoder.setCustomFields(customFields);
// Set the Logstash appender config from JHipster properties
logstashAppender.addDestinations(new InetSocketAddress(jHipsterProperties.getLogging().getLogstash().getHost(),jHipsterProperties.getLogging().getLogstash().getPort()));
ShortenedThrowableConverter throwableConverter = new ShortenedThrowableConverter();
throwableConverter.setRootCauseFirst(true);
logstashEncoder.setThrowableConverter(throwableConverter);
logstashEncoder.setCustomFields(customFields);
logstashAppender.setEncoder(logstashEncoder);
logstashAppender.start();
// Wrap the appender in an Async appender for performance
AsyncAppender asyncLogstashAppender = new AsyncAppender();
asyncLogstashAppender.setContext(context);
asyncLogstashAppender.setName(ASYNC_LOGSTASH_APPENDER_NAME);
asyncLogstashAppender.setQueueSize(jHipsterProperties.getLogging().getLogstash().getQueueSize());
asyncLogstashAppender.addAppender(logstashAppender);
asyncLogstashAppender.start();
context.getLogger("ROOT").addAppender(asyncLogstashAppender);
}
logback-spring.xml 方法(不向 ELK 发送日志)
<appender name="LOGSTASH"
class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>${LOGSTASH_HOST}:${LOGSTASH_PORT}</destination>
<encoder charset="UTF-8"
class="net.logstash.logback.encoder.LogstashEncoder">
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE-ROLLING"/>
<appender-ref ref="LOGSTASH"/>
</root>
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>
删除后,我以调试模式运行应用程序,AsyncAppender
日志如下。没有注意到任何不愉快的事情。
22:33:10,313 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [net.logstash.logback.appender.LogstashTcpSocketAppender]
22:33:10,379 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [LOGSTASH]
22:33:11,090 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.sampleapp] to INFO
22:33:11,093 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating INFO level on Logger[com.sampleapp] onto the JUL framework
22:33:11,101 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.sampleapp] to false
22:33:11,102 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[com.sampleapp]
22:33:11,105 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate] to INFO
22:33:11,105 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating INFO level on Logger[org.hibernate] onto the JUL framework
22:33:11,106 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.hibernate] to false
22:33:11,106 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[org.hibernate]
22:33:11,106 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.SQL] to DEBUG
22:33:11,107 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating DEBUG level on Logger[org.hibernate.SQL] onto the JUL framework
22:33:11,108 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.hibernate.SQL] to false
22:33:11,109 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[org.hibernate.SQL]
22:33:11,111 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.type.descriptor.sql] to DEBUG
22:33:11,112 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating DEBUG level on Logger[org.hibernate.type.descriptor.sql] onto the JUL framework
22:33:11,112 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.hibernate.type.descriptor.sql] to false
22:33:11,113 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[org.hibernate.type.descriptor.sql]
22:33:11,114 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.springframework] to INFO
22:33:11,114 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating INFO level on Logger[org.springframework] onto the JUL framework
22:33:11,114 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.springframework] to false
22:33:11,114 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[org.springframework]
22:33:11,115 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
22:33:11,116 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating INFO level on Logger[ROOT] onto the JUL framework
22:33:11,116 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[ROOT]
22:33:11,116 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [LOGSTASH] to Logger[ROOT]
22:33:11,116 |-INFO in ch.qos.logback.core.joran.action.ShutdownHookAction - About to instantiate shutdown hook of type [ch.qos.logback.core.hook.DelayingShutdownHook]
22:33:11,122 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
22:33:11,123 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@448c8166 - Registering current configuration as safe fallback point
22:33:11,179 |-INFO in net.logstash.logback.appender.LogstashTcpSocketAppender[LOGSTASH] - Log destination localhost:4560: connection established.
22:33:11,192 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating DEBUG level on Logger[com.sampleapp] onto the JUL framework