ECS Logging backend for SwiftLog. Outputs log messages in ECS Log format.
- 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
- 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.
- 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)
}
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
.
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)
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
.
See Issues.
swift-log-ecs is available under the MIT license. See the LICENSE file for more info.