Broadcast
Simple and composable logging for Swift apps, servers, and coding agents.
Broadcast is a structured logging library for Swift apps and servers that feels as lightweight as print, but gives every log enough structure to help you solve real production issues.
Broadcast turns simple logs like log.info("Started app") into a trail of actions and state that can be sent to your console, saved to support logs, or optimized for coding agents.
Broadcast is already running in production in Plinky, where it's helped Codex track down bugs and race conditions I'd been chasing for over a year. By handing an agent thousands of structured logs you take the guesswork out of debugging. Instead your coding agent can trace through and debug problems on its own, with far less help from you.
By integrating Broadcast from day one you'll give coding agents the feedback loop they need while they work, leading to fewer bugs and less time reviewing slop.
Table of Contents
Getting Started
Broadcast starts with one type: Log. A Log owns one or more destinations, so every call to log.debug, log.info, log.error, etc. is sent to each destination.
import Broadcast
// Instantiate a ConsoleLogger that logs to Xcode's console
let consoleLogger = ConsoleLogger(subsystem: "com.example.app", category: "logs")
// Instantiate a SessionLogger that logs to an in-memory buffer that you can later export from
let sessionLogger = SessionLogger()
let log = Log(
destinations: [
consoleLogger,
sessionLogger
]
)
log.info("Started app")
log.debug("Synced", 10, "links")
That's it. You now have one API that writes to the console and keeps an in-memory support log you can export later.
ConsoleLogger uses Apple's unified logging system. On Linux, enable the
SwiftLogging package trait and use SwiftLogDestination for process output.
let supportLogs = sessionLogger.logs()
[!TIP]
I recommend creating one global let log for your app or package so you can call log.info(...) everywhere and make your call-sites clean and simple.
Broadcast is intentionally not prescriptive, so you can still pass Log through dependency injection, expose the same global logger through the SwiftUI environment, or use whatever access pattern fits your app. The important part is that your app has one easy logging API, and destinations decide where those logs go.
Structured Logs
Logging plain strings is useful for quick local debugging, but structured logs provide the context you need to fix complicated problems. We can do so by using typed values like Log.Signal, Log.Category, and Log.Payload to add context for future debugging sessions.
log.info(
.state,
"Synced links",
category: .sync,
payload: [
.result("Success"),
.linkCount(links.count)
]
)
Broadcast's structured log is composed of a few primitives:
Log.Level: debug, info, warn, error, or fault.
Log.Signal: Describes what change occurred, such as an .action, .state, .event, .metric, or .diagnostic.
Log.Category: Describes what part or subsystem of your app this log is tied to, such as "Sync", "Account", or "Payments".
Log.Payload: Typed key-value diagnostics like identifiers, counts, dates, durations, and errors.
You can keep call-sites readable by adding properties and functions that represent signals, categories, and payloads unique to your app:
extension Log.Category {
static let sync: Self = "Sync"
}
extension Log.Payload {
static func result(_ result: String) -> Self {
Self.string("result", result)
}
static func accountID(_ id: UUID) -> Self {
Self.uuid("accountID", id)
}
static func linkCount(_ count: Int) -> Self {
Self.int("linkCount", count)
}
}
log.info(
.action,
"Finished account sync",
category: .sync,
payload: [
.accountID(account.id),
.linkCount(links.count),
.duration(seconds: syncDuration)
]
)
Destinations
Broadcast includes a few destinations out of the box:
ConsoleLogger writes to Apple's unified logging system on Apple platforms.
SessionLogger buffers logs in memory for the current launch.
MultiSessionLogger buffers logs across launches when the MultiSessionLogging package trait is enabled. It currently uses a Boutique Store on Apple platforms.
Destinations are composable, so one call-site can power multiple debugging workflows.
let supportLogger = SessionLogger(
timestampFormatStyle: .timestamp
)