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
LogHandler
Declaration
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
EventLoopGroup
which is used to create theHTTPClient
Declaration
Swift
static var eventLoopGroup: EventLoopGroup?
-
Used to log background activity of the
LogstashLogHandler
andHTTPClient
This logger MUST be created BEFORE theLoggingSystem
is 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
ByteBuffer
must be at leastDeclaration
Swift
static var logStorageSize: Int?
-
Specifies how large the log storage
ByteBuffer
with all the current uploading buffers can be at the mostDeclaration
Swift
static var maximumTotalLogStorageSize: Int?
-
The
HTTPClient
which is used to create theHTTPClient.Request
Declaration
Swift
static var httpClient: HTTPClient?
-
The
HTTPClient.Request
which 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.Level
of theLogstashLogHandler
Logging entries below thisLogger.Level
won’t get logged at allDeclaration
Swift
public var logLevel: Logger.Level
-
Holds the
Logger.Metadata
of theLogstashLogHandler
Declaration
Swift
public var metadata: Logger.Metadata
-
Convenience subscript to get and set
Logger.Metadata
Declaration
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
LogstashLogHandler
Merges theLogger.Metadata
, encodes the log entry to a propertly formatted HTTP body which is then cached in the log storeByteBuffer
Declaration
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
JSONEncoder
used to encode theLogstashHTTPBody
to JSONDeclaration
Swift
private static let jsonEncoder: JSONEncoder
-
Creates the HTTP request which stays constant during the entire lifetime of the
LogstashLogHandler
Sets 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
ISO8601DateFormatter
to create the timstamp of the log entryDeclaration
Swift
private var timestamp: String { get }
-
An
ISO8601DateFormatter
used to format the timestamp of the log entry in an ISO8601 conformant fashionDeclaration
Swift
private static let dateFormatter: ISO8601DateFormatter
-
Merges the
Logger.Metadata
passed 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
uploadLogData
function with a certainTimeAmount
asinitialDelay
anddelay
(delay between repeating the task)Declaration
Swift
static func scheduleUploadTask(initialDelay: TimeAmount) -> RepeatedTask
-
Function which uploads the stored log data in the
ByteBuffer
to Logstash Never called directly, its only scheduled via thescheduleUploadTask
function This function is thread-safe and designed to only block the stored log dataByteBuffer
for a short amount of time (the time it takes to duplicate this bytebuffer). Then, the “original” stored log dataByteBuffer
is freed and the lock is liftedDeclaration
Swift
static func uploadLogData(_ task: RepeatedTask? = nil)