Swift & Developer Tools

Autolog - Best logs you’ve never had

From manual log messages to execution traces: automatic logging, early-exit details, and better context for humans and AI.

6 min readAnciltech

Get AutoLog on GitHub

You tap “Reload profile” and the request fails. Manual logs often contain useful sentences. AutoLog adds a consistent execution trail, timing, results, and the calls leading up to the failure:

Before

Messages you write and maintain

Console messages

[UI] Reload profile tapped.
[Profile] Refresh requested for profile 42.
[Profile] Loading profile 42.
[Cache] Looking for a saved profile.
[Cache] No cached profile found.
[Network] Requesting profile from the server.
[Network] Request failed: no internet connection.
[Profile] Could not refresh the profile.
After

Button tap → execution → error backtrack

Execution trace

👆🏼-🧵A1B - 👆🏼 Reload profile [ProfileView:8]
👆🏼-🧵A1B │ main │         │ --> 🔵 ProfileViewModel.reload(id: 42)
👆🏼-🧵A1B │ main │         │ ---> 🔵 ProfileService.load(id: 42)
👆🏼-🧵A1B │ main │         │ ----> 🔵 ProfileCache.lookup(id: 42)
👆🏼-🧵A1B │ main │   1.6µs │ ----> 🟢 ProfileCache.lookup(id: 42) → nil
👆🏼-🧵A1B │ main │         │ ----> 🔵 ProfileAPI.fetch(id: 42)
👆🏼-🧵A1B │ main │  14.1µs │ ----> 🔴 ProfileAPI.fetch(id: 42) → offline [NetworkError]
👆🏼-🧵A1B │ main │   852µs │ ---> 🔴 ProfileService.load(id: 42) → offline [NetworkError]
👆🏼-🧵A1B │ main │   1.1ms │ --> 🔴 ProfileViewModel.reload(id: 42) → offline [NetworkError]

Backtrack attached to the first failure

˄˄˄˄˄˄˄˄˄˄˄˄˄ Actions leading up to Error 💥 ˄˄˄˄˄˄˄˄˄˄˄˄˄˄
👆🏼-🧵A1B │ main │         │ --> 🔵 ProfileViewModel.reload(id: 42)
👆🏼-🧵A1B │ main │         │ ---> 🔵 ProfileService.load(id: 42)
👆🏼-🧵A1B │ main │         │ ----> 🔵 ProfileCache.lookup(id: 42)
👆🏼-🧵A1B │ main │   1.6µs │ ----> 🟢 ProfileCache.lookup(id: 42) → nil
👆🏼-🧵A1B │ main │         │ ----> 🔵 ProfileAPI.fetch(id: 42)
💥 Error in ProfileAPI.fetch()
˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅ End Error Context ˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅˅

The first line identifies the button tap and its source location. The same 👆🏼-🧵A1B flow badge connects that tap to the view model, cache miss, API failure, and error propagation. The failure also carries its recent lead-up, so you can work backward from the error through the calls in that interaction.

These illustrative logs extend the captured public 0.11.0 example with a Button.log origin. The compact layout shows arguments, interaction origins, thread, timing, depth, and outcome. Execution events and the first error’s backtrack are separated here; repeated backtracks on the propagating errors are omitted. Badges, source locations, and timings vary between runs. The manual messages illustrate the same offline scenario.

Not just the result. The decision that led to it.

A function returning nil does not tell you why it stopped. Access denied? Missing input? Nothing in the cache? AutoLog can collect the decision point at an early exit and attach it to the result.

In another run, ProfileService has hasAccess: false. With @AutoLog on the type, its completion tells you why it returned early, without adding a message inside the guard:

main │   1.9µs │ -> 🟢 ProfileService.load(id: 42) → nil
╰─ ↘️ !hasAccess

→ nil is the outcome. !hasAccess is the decision: the access check failed, so the guard returned before the API request. You know which branch stopped the work, even though nothing threw an error. Green means a normal return; it does not mean access was granted.

This is especially useful in methods with several guards. A missing selection, an invalid URL, or a denied permission can all produce the same return value. The recorded condition distinguishes those paths without a hand-written sentence at every exit.

When the condition needs a domain explanation, you can add optional intent inside an @AutoLog method:

guard hasAccess else {
    AutoLog.earlyExit(.expected, "access denied by policy")
    return nil
}

The decision point is collected automatically. This extra explanation adds intent to that branch. AutoLog can also capture early returns from supported conditional branches; you do not need to turn an expected early return into a thrown error to make it visible.

Annotate the type. Keep your business logic.

Add @AutoLog to a service, repository, or view model. Its methods get consistent execution logs without repeating logging code inside each one. The access check and API call stay the same:

Before

Manual logging in each method

struct ProfileService {
    let hasAccess: Bool

    func load(id: Int) throws -> String? {
        print("Loading profile \(id).")
        guard hasAccess else {
            print("Skipping profile: access denied.")
            return nil
        }
        do {
            let profile = try ProfileAPI().fetch(id: id)
            print("Loaded profile \(id).")
            return profile
        } catch {
            print("Failed to load profile: \(error)")
            throw error
        }
    }
}
After

@AutoLog covers the type

@AutoLog
struct ProfileService {
    let hasAccess: Bool

    func load(id: Int) throws -> String? {
        guard hasAccess else {
            return nil
        }
        return try ProfileAPI().fetch(id: id)
    }
}

ProfileAPI is an app-defined dependency. This smaller source comparison leaves out the cache layer from the opening trace. For one individual function, @Log is also available. Both macros generate Swift code at compile time; you can inspect the expansion in Xcode.

Better evidence for your AI assistant

An AI coding assistant can investigate an offline message, but the opening trace gives it more: the exact failing function, its callers, arguments, and the cache miss that led to the request. It can map that execution path to source instead of first asking you to add diagnostics.

The early-exit decision is useful evidence too. Instead of asking why a profile is missing, give the assistant the result and the condition that stopped the work:

ProfileService.load(id: 42) returned nil.
AutoLog recorded an early exit at !hasAccess.
Find the hasAccess guard, then trace why hasAccess was false.
Separate what this trace proves from what still needs checking.

That can help an assistant connect runtime behavior to source, locate the relevant callers, and propose a focused fix or test. Consistent execution logs can also reduce the need to ask it to insert temporary diagnostics, rerun the app, and remove those diagnostics later.

The trace establishes that the guard failed. It does not establish why the permission value was false. More context or source inspection is still needed, and generated suggestions still need verification. Better logs improve the evidence available to AI; they do not make its conclusions automatically correct.

Carry the story across layers

The same approach applies across a view model, use case, repository, and data source. Instrument those boundaries and you can follow the work through the app. The runtime can show timing and attach recent log context to thrown-error events, giving failures some of their lead-up.

The optional SwiftUI helpers add the trigger. A logged button establishes an interaction source that downstream AutoLog calls can carry:

import AutoLogSwiftUI
import SwiftUI

struct ProfileView: View {
    let viewModel: ProfileViewModel

    var body: some View {
        Button.log("Reload profile") {
            do {
                try viewModel.reload(id: 42)
            } catch {
                // Present the error to the user.
            }
        }
    }
}

ProfileViewModel.reload(id:) is your app’s annotated method from the opening trace. The button supplies the source location and shared flow badge; you do not pass an ID through each method. That source helps distinguish overlapping operations started by different interactions. Coverage still depends on what you instrument, and detached work needs deliberate context handling.

Choose the logs you actually need

Start with meaningful boundaries. Use @Omit to skip a method, omission options to exclude captured values, and deduplication to quiet repeated events. Passwords, tokens, and document contents should not become payloads simply because a function is annotated.

Runtime output filtering and compile-time emission are different controls. An explicit policy selects the event paths the macro generates. A service that only needs thrown failures can say so:

import AutoLog
import Foundation

@AutoLog(emit: .failures, measure: false)
struct DocumentService {
    func read(_ url: URL) throws -> String {
        try String(contentsOf: url, encoding: .utf8)
    }
}

This excludes successful-call events and disables generated timing work. It also means you will not see the successful early-return trace above under that failures-only policy. Choose the policy for the question you are trying to answer; do not assume zero overhead.

Keep domain-specific messages that explain decisions. Automatic instrumentation can show where a function went; a message can explain why a retry policy changed or why a cache entry was rejected. OSLog, existing log routing, and crash reporting can keep serving their own purposes.

Try it on one operation

Add the open-source Swift AutoLog package, starting at 0.11.0. Add the AutoLog product and, for interaction helpers, AutoLogSwiftUI. Start the runtime once in your app’s startup path:

import AutoLog

AutoLog.start()

Annotate one service and follow one call, including a guard that returns early. The captured examples use the runtime’s error-context handling and a custom output sink with the compact formatter; the default runtime routes console events through OSLog. Formatting and emitted events depend on configuration.

Release 0.11.0 requires Xcode 26 / Swift 6.2 or newer and supports iOS 17+, macOS 15+, and watchOS 11+. Xcode asks you to trust the macro plugin; automated builds need their own macro trust setup.

See the release README, the macro declarations, and the macro trust and CI guide for installation and API details.