受这种Java class方法的启发,我正在尝试一种基于XML的方法,用于直接通过IsObserver
将日志发送到ELK。这将失败,并且日志不会到达ELK。可能是什么原因?任何帮助将不胜感激。
@配置方法
import Foundation
protocol IsObserver: class {
associatedtype DataType
func dataDidUpdate(_ data: [DataType])
}
struct Observation<T: IsObserver> {
weak var observer: T?
}
protocol HasObservers {
associatedtype DataType : IsObserver where DataType.DataType == DataType
static var observations: [ObjectIdentifier : Observation<IsObserver>] { get set } // ERROR: "Value of protocol type 'IsObserver' cannot conform to 'IsObserver'; only struct/enum/class types can conform to protocols"
static func tellObserversDataDidUpdate(_ data: [DataType])
}
extension HasObservers {
static func tellObserversDataDidUpdate(_ data: [DataType]) {
for (id, observation) in observations {
guard let observer = observation.observer else {
observations.removeValue(forKey: id)
continue
}
observer.dataDidUpdate(data)
}
}
static func addObserver<T: IsObserver>(_ observer: T) {
let id = ObjectIdentifier(observer)
let ob = Observation.init(observer: observer)
observations[id] = ob
}
static func removeObserver<T: IsObserver>(_ observer: T) {
let id = ObjectIdentifier(observer)
observations.removeValue(forKey: id)
}
}
logback-spring.xml方法(不将日志发送到ELK)
logback-spring.xml
删除 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);
}
后,我以调试模式运行了该应用程序,日志如下。什么都没发现。
<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"/>