Files
swift-log/Sources/Logging/Docs.docc/LoggingBestPractices.md
T
Franz Busch c05d67286c Overhaul README and documentation catalog to introduce best practices (#363)
# Motivation

`SwiftLog` was created before DocC existed which meant that most of the
documentation was part of the `README` and we had a pretty bare bones
documentation catalog. We also have a second source of guidelines from
the SSWG. This results in a currently unsatisfying developer experience
where everyone has to go around looking for the content in mostly
undocumented places.

# Modifications

This PR overhauls the `README` and moves most of the content into the
documentation catalog. It also introduces a new section called "Best
practices" that are intended to replace the logging guides on swift.org
so that the content moves closer to where it belongs. To showcase such a
best practice this PR brings over the log level recommendation of
swift.org. The recommendation is mostly the same except that after
recent discussions we wanted to allow libraries to log at `info` level
when they encounter problems which they can't communicate through other
channels e.g. by throwing from a method.

# Result

With this PR we provide a nicer experience to our developers making it
easier for them to get started and understand how to properly log. We
also set ourselves up for expanding on the best practices around
logging.
2025-07-09 16:48:56 +02:00

1.5 KiB

Logging best practices

Best practices for effective logging with SwiftLog.

Overview

This collection of best practices helps library authors and application developers create effective, maintainable logging that works well across diverse environments. Each practice is designed to ensure your logs are useful for debugging while being respectful of system resources and operational requirements.

Who Should Use These Practices

  • Library Authors: Creating reusable components that log appropriately.
  • Application Developers: Implementing logging strategies in applications.

Philosophy

Good logging strikes a balance between providing useful information and avoiding system overhead. These practices are based on real-world experience with production systems and emphasize:

  • Predictable behavior across different environments.
  • Performance consciousness to avoid impacting application speed.
  • Operational awareness to support production debugging and monitoring.
  • Developer experience to make debugging efficient and pleasant.

Contributing to these practices

These best practices evolve based on community experience and are maintained by the Swift Server Working Group (SSWG). Each practice includes:

  • Clear motivation explaining why the practice matters
  • Concrete examples showing good and bad patterns
  • Alternatives considered documenting trade-offs and rejected approaches

Topics