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 the HTTPClient

    Declaration

    Swift

    static var eventLoopGroup: EventLoopGroup?
  • Used to log background activity of the LogstashLogHandler and HTTPClient This logger MUST be created BEFORE the LoggingSystem 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 least

    Declaration

    Swift

    static var logStorageSize: Int?
  • Specifies how large the log storage ByteBuffer with all the current uploading buffers can be at the most

    Declaration

    Swift

    static var maximumTotalLogStorageSize: Int?
  • The HTTPClient which is used to create the HTTPClient.Request

    Declaration

    Swift

    static var httpClient: HTTPClient?
  • The HTTPClient.Request which stays consistent (except the body) over all uploadings to Logstash

    Declaration

    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 the LogstashLogHandler Logging entries below this Logger.Level won’t get logged at all

    Declaration

    Swift

    public var logLevel: Logger.Level
  • Holds the Logger.Metadata of the LogstashLogHandler

    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 of the LogstashLogHandler, need to be called once before LoggingSystem.bootstrap(...) is called

    Declaration

    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 the Logger.Metadata, encodes the log entry to a propertly formatted HTTP body which is then cached in the log store ByteBuffer

    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 more

    Declaration

    Swift

    enum Error : String
  • A struct used to encode the Logger.Level, Logger.Message, Logger.Metadata, and a timestamp which is then sent to Logstash

    See more

    Declaration

    Swift

    struct LogstashHTTPBody : Codable
  • The JSONEncoder used to encode the LogstashHTTPBody to JSON

    Declaration

    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” header

    Declaration

    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 format

    Declaration

    Swift

    func encodeLogData(level: Logger.Level,
                       message: Logger.Message,
                       metadata: Logger.Metadata) -> Data?
  • Uses the ISO8601DateFormatter to create the timstamp of the log entry

    Declaration

    Swift

    private var timestamp: String { get }
  • An ISO8601DateFormatter used to format the timestamp of the log entry in an ISO8601 conformant fashion

    Declaration

    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. via logger[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 certain TimeAmount as initialDelay and delay (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 the scheduleUploadTask function This function is thread-safe and designed to only block the stored log data ByteBuffer for a short amount of time (the time it takes to duplicate this bytebuffer). Then, the “original” stored log data ByteBuffer is freed and the lock is lifted

    Declaration

    Swift

    static func uploadLogData(_ task: RepeatedTask? = nil)