Contents

OSLogInterpolation

A container for the elements of a log message.

Declaration

@frozen struct OSLogInterpolation

Overview

An OSLogInterpolation structure contains a formatted string and its interpolated arguments. Don’t create OSLogInterpolation structures directly. The system creates them automatically when you log messages using the Logger object.

When you craft log messages, you may incorporate content from your code’s variables directly into the message strings. When you do, the logging system creates OSLogInterpolation structures to store those values. The structure stores the provided values as-is, and the logging system formats those values as strings only in the final log message. To change the final appearance of a value, include additional parameters along with the value. Consider the following example, which logs values of several different types:

logger.log("\(taskID) \(giftCardID) \(serverID) \(seconds)")

For values that are variable in length, formatting them makes the log message easier to read. In the preceding example, giftCardID and seconds contain variable-length values. The following example fixes the formatting issues by making all gift card IDs the same width and aligning them to the left edge of the available space. The example also formats each seconds value as a fixed-point number with only two digits to the right of the decimal point.

logger.log("\(taskID) \(giftCardID, align: .left(columns: GiftCard.maxIDLength)) \(serverID) \(seconds, format: .fixed(precision: 2))")

Because strings and custom objects may contain user-sensitive information, the logging system redacts those values by default, however, it doesn’t redact numerical values. The resulting log message displays a string like <private> instead of the actual value. If you know the value doesn’t include sensitive data, change the privacy setting using the privacy parameter, as the following example shows:

logger.log("Paid with bank account \(accountNumberString)")   // Redacted by default
logger.log("Ordered smoothie \(smoothieName, privacy: .public)")  // Visible!

Topics

Appending Strings

Appending Signed Integers

Appending Unsigned Integers

Appending Doubles

Appending Floats

Appending Boolean Values

Appending Generic Types

Appending Pointer Data

Appending Objects

Instance Methods

See Also

Value Interpolation