skip to content

With iOS unified logging, which Logger levels stay in memory only and which persist to disk, and how do you choose a level?

level: middleimportance: must knowfreq 62%

answer

  1. every message starts in memory
  2. severity decides the trip to disk
  3. notice is the default level
  4. info's footnote: the log tool
  5. trace, warning, critical are aliases

basics

~20 s

Debug and info normally stay in memory only; notice (the default), error and fault also go to the on-disk store until it fills and purges old entries. Pick the lowest level that survives until someone reads it.

solid answer

~40 s

All messages go to memory first. `debug` is never persisted by default; `info` stays in memory unless the `log` tool collects it; `notice` (the same as `log(_:)`), `error` and `fault` are compressed into the on-disk store, which purges old messages once it reaches its size limit. So the steps support needs after a failure belong at `notice` or above, handled failures at `error`, broken assumptions at `fault`, and chatty detail at `debug`. Severity also costs: debug is cheap because it stays in memory, while fault often captures extra information. `trace`, `warning` and `critical` are aliases for `debug`, `error` and `fault`. When reading, remember `log show` hides info and debug unless you pass `--info` or `--debug`.

code

swift · 18 lines
swift
import os

let checkInLog = Logger(subsystem: "com.example.boatrental", category: "dock-checkin")

enum LockState { case idle, unlocking, unlocked }

func reportUnlock(result: Result<Int, Error>, attempt: Int, state: LockState) {
    checkInLog.debug("Unlock attempt \(attempt) sent")
    switch result {
    case .success(let millis):
        checkInLog.notice("Dock unlocked in \(millis) ms")
    case .failure(let error):
        checkInLog.error("Dock unlock failed: \(error, privacy: .public)")
    }
    if state == .idle {
        checkInLog.fault("Unlock reply arrived while no unlock was pending")
    }
}

go deeper

for a junior

Recall the five levels in order, debug, info, notice, error, fault, and that notice is the default used by log(_:).

for a middle

Explain which levels persist by default, info's dependence on the log tool, the storage limit that purges old entries, and the cost of fault.

for a senior

Diagnose a support archive that lacks the lines you need: debug never persisted, info rolled out of memory, or log show hiding them without --info.

for a principal

Turn the persistence table into a team rule for what support must be able to read after a failure, and weigh it against store churn.

## Memory first, disk by severity Apple's guide to generating log messages states the rule plainly: the system stores **all** messages in memory first and writes the more severe levels to an on-disk data store. The level you choose therefore decides whether a line can still be read after the moment has passed. | Level | `Logger` methods | Persisted to disk by default | Use it for | |---|---|---|---| | Debug | `debug(_:)`, `trace(_:)` | No | verbose detail useful only while developing | | Info | `info(_:)` | Only when collected with the `log` tool | helpful but not essential context | | Notice (default) | `notice(_:)`, `log(_:)` | Yes, up to a storage limit | steps essential for troubleshooting | | Error | `error(_:)`, `warning(_:)` | Yes, up to a storage limit | errors your code met | | Fault | `fault(_:)`, `critical(_:)` | Yes, up to a storage limit | bugs and broken assumptions | Two details matter: - **Persisted is not forever.** The on-disk messages are compressed, and when the store exceeds a predefined size the system purges old messages to make room. - **Info has a footnote.** Apple's `info(_:)` page says it writes to the in-memory store only, and also to the on-disk store if you use the `log` command-line tool to collect your logs. Do not rely on info lines surviving until someone looks. ## What each level costs Debug messages have very low overhead because they live only in memory. Faults and other severe messages cost more: the system often captures additional information and writes it to disk. For error and fault, Apple notes that if an activity object exists, the system also captures information for the related process chain. Severity is not a volume knob; reserve the top levels for what is actually wrong. ## Choosing a level in practice For a boat-rental app checking renters in at a dock over Bluetooth: - **debug**: each characteristic write and the raw bytes the lock returns; - **info**: the lock's reported firmware, the signal strength at connect; - **notice**: "check-in started at dock 4", "unlock sent", "dock unlocked in 820 ms", the trail support reads after a failure; - **error**: the unlock timed out, or Bluetooth permission is denied; - **fault**: an unlock reply arrived while no unlock was pending, a state the code should never reach. A quick rule a reviewer can apply: 1. Would support need this line after the failure? Then `notice` or above. 2. Did an operation fail in a way the code noticed? `error`. 3. Did the code reach a state it assumes impossible? `fault`. 4. Otherwise, `debug` or `info`. ```swift import os let checkInLog = Logger(subsystem: "com.example.boatrental", category: "dock-checkin") enum LockState { case idle, unlocking, unlocked } func reportUnlock(result: Result<Int, Error>, attempt: Int, state: LockState) { checkInLog.debug("Unlock attempt \(attempt) sent") switch result { case .success(let millis): checkInLog.notice("Dock unlocked in \(millis) ms") case .failure(let error): checkInLog.error("Dock unlock failed: \(error, privacy: .public)") } if state == .idle { checkInLog.fault("Unlock reply arrived while no unlock was pending") } } ``` ## Reading what you logged - `log show` prints only default-level messages unless you pass `--info` and/or `--debug`, so "missing" info lines may simply be hidden. - `log stream --level debug` includes events at and below the given level while streaming on the Mac. - `isEnabled(type:)` reports whether a logger can emit a given `OSLogType`, which lets you skip expensive preparation work for a level that is off. ## Aliases and the level type `trace(_:)` is functionally equivalent to `debug(_:)`, `warning(_:)` to `error(_:)`, `critical(_:)` to `fault(_:)`, and `log(_:)` to `notice(_:)`. `log(level:_:)` takes an `OSLogType`, whose values are `.debug`, `.info`, `.default`, `.error` and `.fault`; notice is the `.default` type. ## Common mistakes - **Verbose dumps at notice.** Packet bytes at notice persist and push older, more useful lines out of the store. - **Routine progress at error.** A level filter for errors then returns hundreds of lines that are not failures. - **Expecting debug lines in a field archive.** They were never written to disk, so an archive taken later cannot contain them. - **Treating fault as a crash.** `fault(_:)` records a message and execution continues; stopping the process is a separate decision. - **Formatting expensive values for a level that is off.** Check `isEnabled(type:)` first when building the value itself is costly. ## Changing the defaults while debugging Apple documents two overrides for debugging on macOS: `sudo log config --mode "level:debug" --subsystem <name>`, and a logging configuration profile in `/Library/Preferences/Logging/Subsystems/` whose `Level` dictionary sets `Enable` and `Persist` to `Inherit`, `Default`, `Info` or `Debug`. Treat these as debugging aids; code that ships should be correct with the defaults.

  • A support archive collected two hours after a failed check-in has no info lines at all; why?
    Info messages normally live only in memory, so by the time the archive is taken they may be gone, and `log show` hides info lines unless you pass `--info`. If a step matters after the fact, log it at `notice`, which goes to the on-disk store.
  • Does logging at fault stop or crash the app?
    No. `fault(_:)` records a message at the fault level, persisted and often with extra captured information, and execution continues. Stopping the process is a separate decision, such as an assertion or `fatalError`, and has nothing to do with the log level.
  • Why not log everything at error so it is sure to persist?
    Error is persisted and costs more to record, and the on-disk store has a size limit and purges old messages. Routine lines at error crowd out older useful ones and make real failures hard to find with a level filter.

A dashcam loops the last few minutes in memory and only saves a clip when something notable happens; debug and info are the loop, notice and above are the saved clips, and the card still overwrites its oldest clips when full.

  • Debug messages are written to disk, so verbose dumps at debug help support
  • notice(_:) and log(_:) write at different levels
  • Info lines persist exactly like notice lines
  • Logging at fault terminates the app like fatalError
  • Persisted messages stay on the device forever