Skip to content

rwbutler/swift-log-ecs

Repository files navigation

swift-log-ecs

License Twitter

ECS Logging backend for SwiftLog. Outputs log messages in ECS Log format.

Features

  • Compatible with SwiftLog
  • Compatible with Vapor
  • Support for custom events not defined as part of the ECS Log fields
  • Supports additional key-value pairs which can be attached to and sent with every log event i.e. SwiftLog metadata
  • Use ECS format logging in production falling back to regular logging format for development

Installation

Swift Package Manager

  • Include SwiftLog as a dependency as part of your projects’s Package.swift file. If using Vapor, you may skip this step as Vapor already includes SwiftLog as a dependency.
  • Include swift-log-ecs as a dependency as part of your project’s Package.swift file.

Usage

  • Import ECSLogging i.e. import ECSLogging
  • Bootstrap the SwiftLog logging system as follows:
LoggingSystem.bootstrap { label in
    ECSLogHandler(label: label)
}

Note: The default log level is .info. You can set the log level as follows:

LoggingSystem.bootstrap { label in
    ECSLogHandler(label: label, logLevel: .notice)
}

Chaining LogHandlers

If you wish to send your ECS formatted log messages somewhere other than the console, it’s possible to chain log handlers by passing in another LogHandler as follows:

let consoleLogger = ConsoleLogger(label: label, console: console, level: level)
return ECSLogHandler(label: label, logHandler: consoleLogger, logLevel: level)

This will cause the ECSLogHandler to format log messages according to the ECS Logging standard before forwarding the log messages on to the next LogHandler.

Usage With Vapor

The default log handler included with SwiftLog is StreamLogHandler whilst the default LogHandler for Vapor is ConsoleLogger (from ConsoleKit) when you call the following bootstrapping code:

var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)

To use ECSLogHandler instead, add the following extension to your Vapor project:

import Foundation
import Logging
import ECSLogging
import Vapor

extension LoggingSystem {
    public static func bootstrapECSLogging(from environment: inout Environment) throws {
        try self.bootstrap(from: &environment) { level in
            return { (label: String) in
                return ECSLogHandler(label: label, logLevel: level)
            }
        }
    }
}

Or if you prefer to use a combination of both:

import Foundation
import Logging
import ECSLogging
import ConsoleKit
import Vapor

extension LoggingSystem {
    public static func bootstrapECSLogging(from environment: inout Environment) throws {
        try self.bootstrap(from: &environment) { level in
            let console = Terminal()
            return { (label: String) in
                let consoleLogger = ConsoleLogger(label: label, console: console, level: level)
                return ECSLogHandler(label: label, logHandler: consoleLogger, logLevel: level)
            }
        }
    }
}

Then instead of calling the following in your main.swift:

var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)

Instead call:

var env = try Environment.detect()
try LoggingSystem.bootstrapECSLogging(from: &env)

How It Works

The SwiftLog package provides the Logger which clients should use. A LogHandler is a specific logging implementation e.g. to log to a file, to log to the console, to log to a monitoring solution such as Datadog or Sentry. swift-log-ecs provides the ECSLogHandler for logging messages in ECS Log format.

The Logger will pass log messages to the appropriate LogHandler, in this case, ECSLogHandler, if:

  • The SwiftLog logging system has been bootstrapped to use the appropriate LogHandler i.e. ECSLogHandler (see Usage above).
  • The message was logged at a log level higher than (or equal to) the current logging level set on the Logger.

Known Issues

See Issues.

Author

Ross Butler

License

swift-log-ecs is available under the MIT license. See the LICENSE file for more info.

Additional Software

Controls

AnimatedGradientView
AnimatedGradientView

Frameworks

  • Cheats - Retro cheat codes for modern iOS apps.
  • Connectivity - Improves on Reachability for determining Internet connectivity in your iOS application.
  • FeatureFlags - Allows developers to configure feature flags, run multiple A/B or MVT tests using a bundled / remotely-hosted JSON configuration file.
  • FlexibleRowHeightGridLayout - A UICollectionView grid layout designed to support Dynamic Type by allowing the height of each row to size to fit content.
  • Hyperconnectivity - Modern replacement for Apple's Reachability written in Swift and made elegant using Combine. An offshoot of the Connectivity framework.
  • Skylark - Fully Swift BDD testing framework for writing Cucumber scenarios using Gherkin syntax.
  • TailorSwift - A collection of useful Swift Core Library / Foundation framework extensions.
  • TypographyKit - Consistent & accessible visual styling on iOS with Dynamic Type support.
  • Updates - Automatically detects app updates and gently prompts users to update.
Cheats Connectivity FeatureFlags Hyperconnectivity Skylark TypographyKit Updates
Cheats Connectivity FeatureFlags Hyperconnectivity Skylark TypographyKit Updates

Tools

  • Clear DerivedData - Utility to quickly clear your DerivedData directory simply by typing cdd from the Terminal.
  • Config Validator - Config Validator validates & uploads your configuration files and cache clears your CDN as part of your CI process.
  • IPA Uploader - Uploads your apps to TestFlight & App Store.
  • Palette - Makes your TypographyKit color palette available in Xcode Interface Builder.
Config Validator IPA Uploader Palette
Config Validator IPA Uploader Palette

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages