MyGit

apple/swift-log

Fork: 298 Star: 3554 (更新于 2024-11-16 07:31:10)

license: Apache-2.0

Language: Swift .

A Logging API for Swift

最后发布版本: 1.6.1 ( 2024-06-25 01:37:08)

官方网址 GitHub网址

SwiftLog

First things first: This is the beginning of a community-driven open-source project actively seeking contributions, be it code, documentation, or ideas. Apart from contributing to SwiftLog itself, there's another huge gap at the moment: SwiftLog is an API package which tries to establish a common API the ecosystem can use. To make logging really work for real-world workloads, we need SwiftLog-compatible logging backends which then either persist the log messages in files, render them in nicer colors on the terminal, or send them over to Splunk or ELK.

What SwiftLog provides today can be found in the API docs.

Getting started

If you have a server-side Swift application, or maybe a cross-platform (for example Linux & macOS) app/library, and you would like to log, we think targeting this logging API package is a great idea. Below you'll find all you need to know to get started.

Adding the dependency

SwiftLog is designed for Swift 5.8 and later. To depend on the logging API package, you need to declare your dependency in your Package.swift:

.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),

and to your application/library target, add "Logging" to your dependencies, e.g. like this:

.target(name: "BestExampleApp", dependencies: [
    .product(name: "Logging", package: "swift-log")
],

Let's log

// 1) let's import the logging API package
import Logging

// 2) we need to create a logger, the label works similarly to a DispatchQueue label
let logger = Logger(label: "com.example.BestExampleApp.main")

// 3) we're now ready to use it
logger.info("Hello World!")

Output

2019-03-13T15:46:38+0000 info: Hello World!

Default Logger behavior

SwiftLog provides for very basic console logging out-of-the-box by way of StreamLogHandler. It is possible to switch the default output to stderr like so:

LoggingSystem.bootstrap(StreamLogHandler.standardError)

StreamLogHandler is primarily a convenience only and does not provide any substantial customization. Library maintainers who aim to build their own logging backends for integration and consumption should implement the LogHandler protocol directly as laid out in the "On the implementation of a logging backend" section.

For further information, please check the API documentation.

Available Logging Backends For Applications

You can choose from one of the following backends to consume your logs. If you are interested in implementing one see the "Implementation considerations" section below explaining how to do so. List of existing SwiftLog API compatible libraries:

Repository Handler Description
Kitura/HeliumLogger a logging backend widely used in the Kitura ecosystem
ianpartridge/swift-log-syslog a syslog backend
Adorkable/swift-log-format-and-pipe a backend that allows customization of the output format and the resulting destination
chrisaljoudi/swift-log-oslog an OSLog Unified Logging backend for use on Apple platforms. Important Note: we recommend using os_log directly as described here. Using os_log through swift-log using this backend will be less efficient and will also prevent specifying the privacy of the message. The backend always uses %{public}@ as the format string and eagerly converts all string interpolations to strings. This has two drawbacks: 1. the static components of the string interpolation would be eagerly copied by the unified logging system, which will result in loss of performance. 2. It makes all messages public, which changes the default privacy policy of os_log, and doesn't allow specifying fine-grained privacy of sections of the message. In a separate on-going work, Swift APIs for os_log are being improved and made to align closely with swift-log APIs. References: Unifying Logging Levels, Making os_log accept string interpolations using compile-time interpretation.
Brainfinance/StackdriverLogging a structured JSON logging backend for use on Google Cloud Platform with the Stackdriver logging agent
DnV1eX/GoogleCloudLogging a client-side library for logging application events in Google Cloud via REST API v2.
vapor/console-kit a logger to the current terminal or stdout with stylized (ANSI) output. The default logger for all Vapor applications
neallester/swift-log-testing provides access to log messages for use in assertions (within test targets)
wlisac/swift-log-slack a logging backend that sends critical log messages to Slack
NSHipster/swift-log-github-actions a logging backend that translates logging messages into workflow commands for GitHub Actions.
stevapple/swift-log-telegram a logging backend that sends log messages to any Telegram chat (Inspired by and forked from wlisac/swift-log-slack)
jagreenwood/swift-log-datadog a logging backend which sends log messages to the Datadog log management service
google/SwiftLogFireCloud a logging backend for time series logging which pushes logs as flat files to Firebase Cloud Storage.
crspybits/swift-log-file a simple local file logger (using Foundation FileManager)
sushichop/Puppy a logging backend that supports multiple transports(console, file, syslog, etc.) and has the feature with formatting and file log rotation
ShivaHuang/swift-log-SwiftyBeaver a logging backend for printing colored logging to Xcode console / file, or sending encrypted logging to SwiftyBeaver platform.
Apodini/swift-log-elk a logging backend that formats, caches and sends log data to elastic/logstash
binaryscraping/swift-log-supabase a logging backend that sends log entries to Supabase.
kiliankoe/swift-log-matrix a logging backend for sending logs directly to a Matrix room
DiscordBM/DiscordLogger a Discord logging implementation to send your logs over to a Discord channel in a good-looking manner and with a lot of configuration options including the ability to send only a few important log-levels such as warning/error/critical.
CocoaLumberjack a fast & simple, yet powerful & flexible logging framework for macOS, iOS, tvOS and watchOS, which includes a logging backend for swift-log.
rwbutler/swift-log-ecs a logging backend for logging in ECS Log format. Compatible with Vapor and allows chaining of multiple LogHandlers.
ShipBook/swift-log-shipbook a logging backend that sends log entries to Shipbook - Shipbook gives you the power to remotely gather, search and analyze your user logs and exceptions in the cloud, on a per-user & session basis.
Your library? Get in touch!

What is an API package?

Glad you asked. We believe that for the Swift on Server ecosystem, it's crucial to have a logging API that can be adopted by anybody so a multitude of libraries from different parties can all log to a shared destination. More concretely this means that we believe all the log messages from all libraries end up in the same file, database, Elastic Stack/Splunk instance, or whatever you may choose.

In the real-world however, there are so many opinions over how exactly a logging system should behave, what a log message should be formatted like, and where/how it should be persisted. We think it's not feasible to wait for one logging package to support everything that a specific deployment needs whilst still being easy enough to use and remain performant. That's why we decided to cut the problem in half:

  1. a logging API
  2. a logging backend implementation

This package only provides the logging API itself and therefore SwiftLog is a 'logging API package'. SwiftLog (using LoggingSystem.bootstrap) can be configured to choose any compatible logging backend implementation. This way packages can adopt the API and the application can choose any compatible logging backend implementation without requiring any changes from any of the libraries.

Just for completeness sake: This API package does actually include an overly simplistic and non-configurable logging backend implementation which simply writes all log messages to stdout. The reason to include this overly simplistic logging backend implementation is to improve the first-time usage experience. Let's assume you start a project and try out SwiftLog for the first time, it's just a whole lot better to see something you logged appear on stdout in a simplistic format rather than nothing happening at all. For any real-world application, we advise configuring another logging backend implementation that logs in the style you like.

The core concepts

Loggers

Loggers are used to emit log messages and therefore the most important type in SwiftLog, so their use should be as simple as possible. Most commonly, they are used to emit log messages in a certain log level. For example:

// logging an informational message
logger.info("Hello World!")

// ouch, something went wrong
logger.error("Houston, we have a problem: \(problem)")

Log levels

The following log levels are supported:

  • trace
  • debug
  • info
  • notice
  • warning
  • error
  • critical

The log level of a given logger can be changed, but the change will only affect the specific logger you changed it on. You could say the Logger is a value type regarding the log level.

Logging metadata

Logging metadata is metadata that can be attached to loggers to add information that is crucial when debugging a problem. In servers, the usual example is attaching a request UUID to a logger that will then be present on all log messages logged with that logger. Example:

var logger = logger
logger[metadataKey: "request-uuid"] = "\(UUID())"
logger.info("hello world")

will print

2019-03-13T18:30:02+0000 info: request-uuid=F8633013-3DD8-481C-9256-B296E43443ED hello world

with the default logging backend implementation that ships with SwiftLog. Needless to say, the format is fully defined by the logging backend you choose.

On the implementation of a logging backend (a LogHandler)

Note: If you don't want to implement a custom logging backend, everything in this section is probably not very relevant, so please feel free to skip.

To become a compatible logging backend that all SwiftLog consumers can use, you need to do two things: 1) Implement a type (usually a struct) that implements LogHandler, a protocol provided by SwiftLog and 2) instruct SwiftLog to use your logging backend implementation.

A LogHandler or logging backend implementation is anything that conforms to the following protocol

public protocol LogHandler {
    func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, source: String, file: String, function: String, line: UInt)

    subscript(metadataKey _: String) -> Logger.Metadata.Value? { get set }

    var metadata: Logger.Metadata { get set }

    var logLevel: Logger.Level { get set }
}

Instructing SwiftLog to use your logging backend as the one the whole application (including all libraries) should use is very simple:

LoggingSystem.bootstrap(MyLogHandler.init)

Implementation considerations

LogHandlers control most parts of the logging system:

Under control of a LogHandler

Configuration

LogHandlers control the two crucial pieces of Logger configuration, namely:

  • log level (logger.logLevel property)
  • logging metadata (logger[metadataKey:] and logger.metadata)

For the system to work, however, it is important that LogHandler treat the configuration as value types. This means that LogHandlers should be structs and a change in log level or logging metadata should only affect the very LogHandler it was changed on.

However, in special cases, it is acceptable that a LogHandler provides some global log level override that may affect all LogHandlers created.

Emitting
  • emitting the log message itself

Not under control of LogHandlers

LogHandlers do not control if a message should be logged or not. Logger will only invoke the log function of a LogHandler if Logger determines that a log message should be emitted given the configured log level.

Source vs Label

A Logger carries an (immutable) label and each log message carries a source parameter (since SwiftLog 1.3.0). The Logger's label identifies the creator of the Logger. If you are using structured logging by preserving metadata across multiple modules, the Logger's label is not a good way to identify where a log message originated from as it identifies the creator of a Logger which is often passed around between libraries to preserve metadata and the like.

If you want to filter all log messages originating from a certain subsystem, filter by source which defaults to the module that is emitting the log message.

Security

Please see SECURITY.md for SwiftLog's security process.

Design

This logging API was designed with the contributors to the Swift on Server community and approved by the SSWG (Swift Server Work Group) to the 'sandbox level' of the SSWG's incubation process.

最近版本更新:(数据更新于 2024-10-04 23:06:32)

2024-06-25 01:37:08 1.6.1

2024-06-24 16:27:07 1.6.0

2024-01-22 18:50:52 1.5.4

2023-08-11 14:52:51 1.5.3

2023-01-24 09:50:31 1.5.2

2023-01-19 18:42:16 1.5.1

2023-01-18 15:42:15 1.5.0

2022-08-15 15:44:25 1.4.4

2022-08-10 16:46:20 1.4.3

2021-03-04 19:12:07 1.4.2

主题(topics):

logging, swift-server

apple/swift-log同语言 Swift最近更新仓库

2024-11-19 05:05:44 whoeevee/EeveeSpotify

2024-11-08 09:08:36 Aidoku/Aidoku

2024-11-07 09:51:16 argmaxinc/WhisperKit

2024-11-01 03:30:19 wordpress-mobile/WordPress-iOS

2024-10-30 03:29:03 jordanbaird/Ice

2024-10-24 02:02:47 Artificial-Pancreas/iAPS