Enable compile-time elimination of log levels to achieve zero runtime
overhead for logs that will never be needed in production.
### Motivation:
In production deployments, applications often know in advance which log
levels will never be needed. For example, a production service typically
only needs warning and above, while trace and debug levels are
exclusively useful during development. Currently, even with the log
level set to `.warning` at runtime, the code for trace and debug
statements still exists in the binary and incurs overhead.
### Modifications:
The "SLG-0002: Compile-time log level elimination using traits" proposal
doc added.
### Result:
The proposal is ready for implementation.
Conform `Logger.Level` to `CustomStringConvertible` and
`LosslessStringConvertible`.
### Motivation:
There are libraries, such as Vapor, that add these conformances
privately and `@retroactive`ly. This would avoid having to implement
these conformances manually in each library and would unify and
standardise the implementation.
### Modifications:
Make `Logger.Level` conform to `CustomStringConvertible` and
`LosslessStringConvertible`, adding a public `description` computed
property that returns the `rawValue` and a public initializer that takes
a `description` string, lowercases it, and passes it to the `rawValue`
initializer.
### Result:
`Logger.Level` conforms to `CustomStringConvertible` and
`LosslessStringConvertible`.
Make InMemoryLogging available on all platforms
### Motivation:
Users might want to adopt the in memory logger on lower platform
versions.
### Modifications:
Remove availability annotations by using swift-logs own Lock instead of
Mutex
### Result:
In memory logger can be used on all platforms that swift-log supports
Move single pre-process proposal file to the new location.
### Motivation:
To have all the proposals in one place.
### Modifications:
One .md file moved.
### Result:
All proposals are in one place.
Add an InMemoryLogHandler
### Motivation:
Library maintainers should be able to test that their libraries log what
they expect
### Modifications:
Create a new InMemoryLogging product, which contains an
InMemoryLogHandler
This log handler can be used to make a logger, pass the logger into some
function, and then assert that logs were emitted.
See the tests for example usage
### Future:
For now i have left this very bare-bones. We could in future add
convenience functions. For example
- a function to assert that a particular log message was logged
- a static function to create a logger with this handler
---------
Co-authored-by: Franz Busch <privat@franz-busch.de>
Provide organization (aka curation) and abstracts for all of the public
types in the library.
### Motivation:
Updates to the documentation to fill in the missing abstracts for the
provided types, updating the language to follow Apple developer content
guidelines, and fixing smaller typos and grammar issues.
### Modifications:
Curation files added to provide organization for public types, ordering
the content to align with the narrative content provided earlier.
Updating API comments, adding abstracts and parameters where missing,
and moving some of the getting started content that was previously in
the main index into a separate article.
### Result:
starting point
```
--- Experimental coverage output enabled. ---
| Abstract | Curated | Code Listing
Types | 69% (11/16) | 38% (6/16) | 25% (4/16)
Members | 57% (54/94) | 3.2% (3/94) | 0.0% (0/94)
Globals | 7.1% (1/14) | 7.1% (1/14) | 7.1% (1/14)
```
Updated:
```
| Abstract | Curated | Code Listing
Types | 94% (15/16) | 94% (15/16) | 38% (6/16)
Members | 85% (80/94) | 97% (91/94) | 0.0% (0/94)
Globals | 14% (2/14) | 100% (14/14) | 7.1% (1/14)
```
---------
Co-authored-by: Honza Dvorsky <honza@apple.com>
Co-authored-by: Franz Busch <privat@franz-busch.de>
Make Swift 6.0 the minimum support version.
### Motivation:
`swift-log` supports 3 latest Swift releases, which is at this point
6.0, 6.1 and 6.2. Swift 5 is no longer supported.
### Modifications:
- Package.swift swift tools
- CI configs
- Imports checking if compiler is <6.0
### Result:
Swift 5 is no longer supported.
Co-authored-by: Honza Dvorsky <honza@apple.com>
### Motivation:
`StreamLogHandler` has at some point in time acquired an extra
whitespace:
```
2025-07-18T10:09:12+0100 notice label : hello
^
|
this one is superfluous
```
but we don't typically put spaces in front of `:`s. This PR changes it
to
```
2025-07-18T10:09:12+0100 notice label: hello
^
|
no extra space
```
Additionally, if the label is empty (`""`), then we shouldn't print any
spaces there. So with an empty label, we go from
```
2025-07-18T10:09:12+0100 notice : hello
^^
|
two superfluous spaces
```
to
```
2025-07-18T10:09:12+0100 notice: hello
^
|
no extra spaces
```
### Modifications:
- Remove superfluous space if label is not empty
- Remove two superfluous spaces if label is empty
### Result:
- Somewhat more compact output
- Nicer output
# Motivation
In my previous PR, I overhauled the README and the DocC catalog. While
doing this I removed the guidance around how to implement a log handler.
# Modifications
This PR adds a DocC based guide containing the contents of the previous
README guidance.
# Result
Guidance around log handler implementation in a modern DocC format.
# 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.
Remove an inaccurate comment from the code.
### Motivation:
The removed comment incorrectly described the `flush()` method as not
having an effect when the flush mode is set to `.always`. Not only is
this the opposite of the actual behavior, in fact the method itself is
not affected by the flush mode at all; that check is made elsewhere.
Further, at the point where the check is made, the behavior is clearly
spelled out by the code making the check, so moving the comment there
seems redundant. As such, it seemed best to simply remove it altogether.
### Modifications:
Removed a comment.
### Result:
The inaccurate comment can no longer cause confusion.
Co-authored-by: Franz Busch <f.busch@apple.com>
Motivation:
Latest swift introduces new warnings when deprecated default
implementations are used.
Modifications:
Add new log implementation to NoopLogHandler.
Mark test of old method as deprecated to silence warning.
Result:
Warning free builds on latest swift 6 compiler.
Motivation:
This becomes a critical feature in swift 6.
Modifications:
Add Sendability annotations where required.
Factor out code protected by locks.
Result:
Compiles without warnings with Sendability checking
Motivation:
Calling through protocols is more expensive than many other types of call.
Modifications:
Annotate each protocol usage with any or some keywords.
Result:
Protocol usage now obvious
Motivation:
Feels like a better fit for the problem when a dictionary of bools.
Modifications:
Convert storage to set.
Result:
Fewer lines of code, less waste.
* minimal DocC markdown to order Logger.Levels
Motivation:
When viewed in documentation generated by DocC, the enumeration levels
are ordered alphabetically, removintg the implied ordering in the
source. The written overview of the enum relies on that ordering to make
sense.
Modifications:
Adding a single file that provides structure to Logging/Logger/Levels
within DocC generated documentation.
Result:
When imported into a project that leverages Swift 5.6 (such as Vapor),
the generated documentation for the levels provided by
Logging/Logger/Levels are displayed in the order intended/expected by
the documentation.
* Adding a Swift 5.6 version of Package.swift
Motivation:
Per DocC team, an updated version is required for the DocC changes
to be picked up and reflected in generated dcumentation, even for
upstream projects already using swift 5.6 or later.
Modifications:
Adding Package.Swift@5.6 with the swift version updated.
Result:
Hopefully, documentation updates in DocC generated content will be
reflected.
* Updating linuxmain test generation for <swift5.6
Motivation:
To appease CI, added #if statements into generated LinuxMain
content to handle conditional compilation with Swift 5.6 and later.
Modifications:
Added #if statements to test generating script and re-ran the script
Result:
CI will hopefully pass.
* also guard the LinuxMain file
* Force using test discovery on 5.6+ even though tools version is latest
but LinuxMain exists
* cleanup
* simplify docc links
* formatting revert
* ignore Package-swift5.6.swift from soundness license check
Co-authored-by: Konrad `ktoso` Malawski <konrad_malawski@apple.com>
motivation: adjust to swift 5.6
changes:
* define sendable shims for protocols and structs that may be used in async context
* adjust tests
* add a test to make sure no warning are emitted
Windows recommends the safety extension usage which in this case
explicitly allow you to be informed of failures. This just avoids an
annoying warning when using logging in projects.
Motivation:
`LogHandler` facotories are given a label as input and return a
`LogHandler`. Since the no-op handler doesn't care about the label
callers must provide a thunk: `{ _ in SwiftLogNoOpLogHandler() }`.
That's fine but slightly more annoying to type than it needs to be.
Modifications:
- Add `SwiftLogNoOpLogHandler.init(_: String)`
Result:
Users can create a logger with the no-op handler with:
```swift
Logger(label: "foo", factory: SwiftLogNoOpLogHandler.init)
```
instead of:
```swift
Logger(label: "foo", factory: { _ in SwiftLogNoOpLogHandler() })
```
Motivation:
Right now, we use the full path (`#file`) for the logging as well as for
parsing out the module name (which goes into `source` by default).
This has two problems:
1. The file names contain the build server's full path, ie. aren't
portable across different compiles (and are very long).
2. The module name parsing fails if we log from a file that's not in a
directory named just like the module.
Modification:
Switch `#file` for `#fileID`. This changes a bunch of things:
- `source` is always the module name by default (great!)
- `file` changes from `/the/full/path/on/disk` to `ModuleName/FileBaseName.swift` (I think better, but it's a change)
Result:
`source` is more correct, `file` will be different.
Replaced `fputs` with `fwrite`, added test and made spelling corrections
Motivation:
If a 0 byte is logged, `fputs` would not output the content after the 0 byte making it harder to debug (see #180). `fwrite` uses a count argument so content after 0 byte can be logged.
Modifications:
Replaced `fputs` with the correct call to `fwrite`. Added helper internal func `contiguousUTF8(_ string: String) -> String.UTF8View`. Added `testStdioOutputStreamWrite()` to test ability to log content after 0 byte.
Result:
Improved debugging experience
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
Motivation:
When using the same Logger across an application in combination
with the default StreamLogHandler, it may be difficult to differentiate
between logs produced by different modules. ServiceLifecycle works around
this by prepending the log message with [Lifecycle], which is what I'd like
to do StreamLogHandler by default, leveraging the source log parameter.
Modifications:
StreamLogHandler will include the source in brackets right before
the log message when writing a log.
Result:
Logs from different modules are easily differentiable when produced
using the same Logger instance.
Motivation:
When using the recently re-introduced source-less version of trace, the function parameter that reaches the underlying LogHandler is wrong.
Modifications:
Pass the function parameter to trace with source parameter.
Result:
With these changes, the function parameter that reaches the underlying LogHandler has the correct value.