Convert Figma logo to code with AI

apple logoswift-log

A Logging API for Swift

3,653
307
3,653
30

Top Related Projects

7,468

Simple .NET logging with fully-structured events

6,420

NLog - Advanced and Structured Logging for Various .NET Platforms

22,959

Blazing fast, structured, leveled logging in Go.

25,195

Structured, pluggable logging for Go.

11,307

Zero Allocation JSON Logger

Quick Overview

Swift-log is a logging API package for Swift, designed to provide a unified logging system across Apple platforms and server-side Swift applications. It offers a simple, extensible, and performant logging interface that can be easily integrated into various Swift projects.

Pros

  • Platform-agnostic design, suitable for both Apple platforms and server-side Swift
  • Extensible architecture allowing custom log handlers and formatters
  • Lightweight and performant with minimal overhead
  • Strong community support and active development

Cons

  • Limited built-in log handlers compared to some other logging frameworks
  • Lack of advanced features like log rotation or compression out of the box
  • May require additional setup for more complex logging scenarios
  • Learning curve for developers used to other logging systems

Code Examples

  1. Basic logging:
import Logging

let logger = Logger(label: "com.example.myapp")
logger.info("Application started")
logger.warning("Low memory warning")
logger.error("Failed to load data", metadata: ["error": "Network timeout"])
  1. Creating a custom log handler:
import Logging

struct CustomLogHandler: LogHandler {
    var metadata: Logger.Metadata = [:]
    var logLevel: Logger.Level = .info

    func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, source: String, file: String, function: String, line: UInt) {
        print("[\(level)] \(message)")
    }
}

LoggingSystem.bootstrap { label in
    CustomLogHandler()
}
  1. Using metadata in logs:
import Logging

var logger = Logger(label: "com.example.myapp")
logger[metadataKey: "user"] = "John Doe"
logger.info("User logged in", metadata: ["session": "abc123"])

Getting Started

To use swift-log in your Swift project:

  1. Add the package to your Package.swift:
dependencies: [
    .package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
]
  1. Import the module in your Swift file:
import Logging

let logger = Logger(label: "com.example.myapp")
logger.info("Hello, swift-log!")

Competitor Comparisons

7,468

Simple .NET logging with fully-structured events

Pros of Serilog

  • More mature and feature-rich logging framework with extensive configuration options
  • Supports structured logging with rich data types and custom event enrichers
  • Offers a wide range of sinks for various output destinations (e.g., console, file, database)

Cons of Serilog

  • Primarily designed for .NET ecosystem, limiting cross-platform compatibility
  • May have a steeper learning curve due to its extensive feature set
  • Potentially higher performance overhead compared to Swift-log's lightweight design

Code Comparison

Swift-log:

import Logging

let logger = Logger(label: "com.example.MyApp")
logger.info("Application started")
logger.error("An error occurred", metadata: ["error": "File not found"])

Serilog:

using Serilog;

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateLogger();

Log.Information("Application started");
Log.Error("An error occurred {ErrorMessage}", "File not found");

Both libraries provide similar basic logging functionality, but Serilog offers more advanced features and configuration options out of the box. Swift-log focuses on simplicity and performance, while Serilog emphasizes flexibility and rich logging capabilities.

6,420

NLog - Advanced and Structured Logging for Various .NET Platforms

Pros of NLog

  • More mature and feature-rich logging framework with extensive configuration options
  • Supports a wide range of .NET platforms, including .NET Framework, .NET Core, and .NET Standard
  • Offers built-in support for various log targets (file, database, email, etc.) out of the box

Cons of NLog

  • Can be more complex to set up and configure compared to Swift-log's simpler API
  • May have a slightly higher performance overhead due to its extensive feature set

Code Comparison

Swift-log:

import Logging

let logger = Logger(label: "com.example.MyApp")
logger.info("Application started")

NLog:

using NLog;

private static Logger logger = LogManager.GetCurrentClassLogger();
logger.Info("Application started");

Summary

NLog is a comprehensive logging framework for .NET platforms, offering extensive features and configuration options. Swift-log, on the other hand, provides a simpler and more lightweight logging solution for Swift applications. While NLog excels in flexibility and target support, Swift-log focuses on ease of use and performance. The choice between the two depends on the specific requirements of your project and the platform you're developing for.

22,959

Blazing fast, structured, leveled logging in Go.

Pros of zap

  • Higher performance and lower allocation overhead
  • More flexible configuration options and log levels
  • Built-in support for structured logging

Cons of zap

  • More complex API, steeper learning curve
  • Primarily designed for Go, less suitable for cross-platform projects
  • Requires more setup and configuration for basic use cases

Code Comparison

swift-log:

let logger = Logger(label: "com.example.MyApp")
logger.info("Application started")
logger.error("An error occurred", metadata: ["error": "File not found"])

zap:

logger, _ := zap.NewProduction()
defer logger.Sync()
logger.Info("Application started")
logger.Error("An error occurred", zap.String("error", "File not found"))

Summary

swift-log is a lightweight, cross-platform logging solution designed for Swift applications, offering simplicity and ease of use. zap, on the other hand, is a high-performance, feature-rich logging library for Go, providing advanced capabilities at the cost of increased complexity. While swift-log is more suitable for Swift-based projects and simpler logging needs, zap excels in Go environments where performance and flexibility are crucial.

25,195

Structured, pluggable logging for Go.

Pros of Logrus

  • More mature and widely adopted in the Go ecosystem
  • Offers a rich set of features including structured logging and hooks
  • Supports multiple output formats (JSON, text) out of the box

Cons of Logrus

  • Larger and more complex API compared to Swift-log's minimalist approach
  • Not as actively maintained as Swift-log (last major release was in 2020)
  • Performance can be slower due to its feature-rich nature

Code Comparison

Swift-log:

import Logging

let logger = Logger(label: "com.example.MyApp")
logger.info("Application started")
logger.error("An error occurred", metadata: ["error": "File not found"])

Logrus:

import "github.com/sirupsen/logrus"

log := logrus.New()
log.Info("Application started")
log.WithFields(logrus.Fields{"error": "File not found"}).Error("An error occurred")

Both libraries provide similar basic logging functionality, but Logrus offers more built-in features for structured logging and customization. Swift-log focuses on providing a simple, performant logging API that can be extended through separate packages.

11,307

Zero Allocation JSON Logger

Pros of zerolog

  • High-performance, zero-allocation JSON logging
  • Structured logging with a fluent API for easy field addition
  • Supports multiple output formats (JSON, CBOR, MessagePack)

Cons of zerolog

  • Primarily designed for Go, limiting cross-platform usage
  • Steeper learning curve for developers new to structured logging
  • Less integration with Apple ecosystem tools and frameworks

Code Comparison

zerolog:

log := zerolog.New(os.Stdout).With().Timestamp().Logger()
log.Info().Str("foo", "bar").Msg("Hello World")

swift-log:

let logger = Logger(label: "com.example.MyApp")
logger.info("Hello World", metadata: ["foo": "bar"])

Key Differences

  • zerolog focuses on high-performance JSON logging, while swift-log provides a more general-purpose logging framework
  • swift-log is designed specifically for Swift and Apple platforms, offering better integration with Apple ecosystem
  • zerolog offers more advanced features for structured logging and performance optimization
  • swift-log has a simpler API, making it easier for beginners to adopt

Both libraries serve their respective ecosystems well, with zerolog excelling in performance-critical Go applications and swift-log providing a solid foundation for Swift-based projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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:

RepositoryHandler Description
Kitura/HeliumLoggera logging backend widely used in the Kitura ecosystem
ianpartridge/swift-log-sysloga syslog backend
Adorkable/swift-log-format-and-pipea backend that allows customization of the output format and the resulting destination
chrisaljoudi/swift-log-oslogan 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/StackdriverLogginga structured JSON logging backend for use on Google Cloud Platform with the Stackdriver logging agent
DnV1eX/GoogleCloudLogginga client-side library for logging application events in Google Cloud via REST API v2.
vapor/console-kita logger to the current terminal or stdout with stylized (ANSI) output. The default logger for all Vapor applications
neallester/swift-log-testingprovides access to log messages for use in assertions (within test targets)
wlisac/swift-log-slacka logging backend that sends critical log messages to Slack
NSHipster/swift-log-github-actionsa logging backend that translates logging messages into workflow commands for GitHub Actions.
stevapple/swift-log-telegrama logging backend that sends log messages to any Telegram chat (Inspired by and forked from wlisac/swift-log-slack)
jagreenwood/swift-log-datadoga logging backend which sends log messages to the Datadog log management service
google/SwiftLogFireClouda logging backend for time series logging which pushes logs as flat files to Firebase Cloud Storage.
crspybits/swift-log-filea simple local file logger (using Foundation FileManager)
sushichop/Puppya logging backend that supports multiple transports(console, file, syslog, etc.) and has the feature with formatting and file log rotation
ShivaHuang/swift-log-SwiftyBeavera logging backend for printing colored logging to Xcode console / file, or sending encrypted logging to SwiftyBeaver platform.
Apodini/swift-log-elka logging backend that formats, caches and sends log data to elastic/logstash
binaryscraping/swift-log-supabasea logging backend that sends log entries to Supabase.
kiliankoe/swift-log-matrixa logging backend for sending logs directly to a Matrix room
DiscordBM/DiscordLoggera 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.
CocoaLumberjacka fast & simple, yet powerful & flexible logging framework for macOS, iOS, tvOS and watchOS, which includes a logging backend for swift-log.
rwbutler/swift-log-ecsa logging backend for logging in ECS Log format. Compatible with Vapor and allows chaining of multiple LogHandlers.
ShipBook/swift-log-shipbooka 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.
kasianov-mikhail/scoutCloudKit as a log storage
PADL/AndroidLogginga logging backend for the Android in-kernel log buffer
xtremekforever/swift-systemda logging backend for the systemd journal
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.