LogstashLogHandler
public struct LogstashLogHandler : LogHandler
LogstashLogHandler is a simple implementation of LogHandler for directing
Logger output to Logstash via HTTP requests
-
The label of the
LogHandlerDeclaration
Swift
let label: String -
The host where a Logstash instance is running
Declaration
Swift
static var hostname: String? -
The port of the host where a Logstash instance is running
Declaration
Swift
static var port: Int? -
Specifies if the HTTP connection to Logstash should be encrypted via TLS (so HTTPS instead of HTTP)
Declaration
Swift
static var useHTTPS: Bool? -
The
EventLoopGroupwhich is used to create theHTTPClientDeclaration
Swift
static var eventLoopGroup: EventLoopGroup? -
Used to log background activity of the
LogstashLogHandlerandHTTPClientThis logger MUST be created BEFORE theLoggingSystemis bootstrapped, else it results in an infinte recusion!Declaration
Swift
static var backgroundActivityLogger: Logger? -
Represents a certain amount of time which serves as a delay between the triggering of the uploading to Logstash
Declaration
Swift
static var uploadInterval: TimeAmount? -
Specifies how large the log storage
ByteBuffermust be at leastDeclaration
Swift
static var logStorageSize: Int? -
Specifies how large the log storage
ByteBufferwith all the current uploading buffers can be at the mostDeclaration
Swift
static var maximumTotalLogStorageSize: Int? -
The
HTTPClientwhich is used to create theHTTPClient.RequestDeclaration
Swift
static var httpClient: HTTPClient? -
The
HTTPClient.Requestwhich stays consistent (except the body) over all uploadings to LogstashDeclaration
Swift
@Boxed static var httpRequest: HTTPClient.Request? { get set } -
The log storage byte buffer which serves as a cache of the log data entires
Declaration
Swift
@Boxed static var byteBuffer: ByteBuffer? { get set } -
Provides thread-safe access to the log storage byte buffer
Declaration
Swift
static let byteBufferLock: ConditionLock<Bool> -
Semaphore to adhere to the maximum memory limit
Declaration
Swift
static let semaphore: DispatchSemaphore -
Manual counter of the semaphore (since no access to the internal one of the semaphore)
Declaration
Swift
@Boxed static var semaphoreCounter: Int { get set } -
Keeps track of how much memory is allocated in total
Declaration
Swift
@Boxed static var totalByteBufferSize: Int? { get set } -
Created during scheduling of the upload function to Logstash, provides the ability to cancel the uploading task
Declaration
Swift
@Boxed private(set) static var uploadTask: RepeatedTask? { get set } -
The default
Logger.Levelof theLogstashLogHandlerLogging entries below thisLogger.Levelwon’t get logged at allDeclaration
Swift
public var logLevel: Logger.Level -
Holds the
Logger.Metadataof theLogstashLogHandlerDeclaration
Swift
public var metadata: Logger.Metadata -
Convenience subscript to get and set
Logger.MetadataDeclaration
Swift
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? { get set } -
Undocumented
Declaration
Swift
public init(label: String) -
setup(hostname:port: useHTTPS: eventLoopGroup: backgroundActivityLogger: uploadInterval: logStorageSize: maximumTotalLogStorageSize: ) Setup of the
LogstashLogHandler, need to be called once beforeLoggingSystem.bootstrap(...)is calledDeclaration
Swift
public static func setup(hostname: String, port: Int, useHTTPS: Bool = false, eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: (System.coreCount != 1) ? System.coreCount / 2 : 1), backgroundActivityLogger: Logger = Logger(label: "backgroundActivity-logstashHandler"), uploadInterval: TimeAmount = TimeAmount.seconds(3), logStorageSize: Int = 524_288, maximumTotalLogStorageSize: Int = 4_194_304) -
The main log function of the
LogstashLogHandlerMerges theLogger.Metadata, encodes the log entry to a propertly formatted HTTP body which is then cached in the log storeByteBufferDeclaration
Swift
public func log(level: Logger.Level, // swiftlint:disable:this function_parameter_count function_body_length message: Logger.Message, metadata: Logger.Metadata?, source: String, file: String, function: String, line: UInt) -
Undocumented
See moreDeclaration
Swift
enum Error : String -
A struct used to encode the
See moreLogger.Level,Logger.Message,Logger.Metadata, and a timestamp which is then sent to LogstashDeclaration
Swift
struct LogstashHTTPBody : Codable -
The
JSONEncoderused to encode theLogstashHTTPBodyto JSONDeclaration
Swift
private static let jsonEncoder: JSONEncoder -
Creates the HTTP request which stays constant during the entire lifetime of the
LogstashLogHandlerSets some default headers, eg. a dynamically adjusted “Keep-Alive” headerDeclaration
Swift
static func createHTTPRequest() -> HTTPClient.Request -
Encodes the
Logger.Level,Logger.Message,Logger.Metadata, and an automatically created timestamp to a HTTP body in the JSON formatDeclaration
Swift
func encodeLogData(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata) -> Data? -
Uses the
ISO8601DateFormatterto create the timstamp of the log entryDeclaration
Swift
private var timestamp: String { get } -
An
ISO8601DateFormatterused to format the timestamp of the log entry in an ISO8601 conformant fashionDeclaration
Swift
private static let dateFormatter: ISO8601DateFormatter -
Merges the
Logger.Metadatapassed via the.log()function call as well as the metadata set directly on the logger (eg. vialogger[metadatakey: "test"] = "test"Declaration
Swift
func mergeMetadata(passedMetadata: Logger.Metadata?, file: String, function: String, line: UInt) -> Logger.Metadata -
Splits the source code file path so that only the relevant path is logged
Declaration
Swift
private func conciseSourcePath(_ path: String) -> String -
Formats the code location properly
Declaration
Swift
private func formatLocation(file: String, function: String, line: UInt) -> String -
Schedules the
uploadLogDatafunction with a certainTimeAmountasinitialDelayanddelay(delay between repeating the task)Declaration
Swift
static func scheduleUploadTask(initialDelay: TimeAmount) -> RepeatedTask -
Function which uploads the stored log data in the
ByteBufferto Logstash Never called directly, its only scheduled via thescheduleUploadTaskfunction This function is thread-safe and designed to only block the stored log dataByteBufferfor a short amount of time (the time it takes to duplicate this bytebuffer). Then, the “original” stored log dataByteBufferis freed and the lock is liftedDeclaration
Swift
static func uploadLogData(_ task: RepeatedTask? = nil)
View on GitHub
LogstashLogHandler Structure Reference