Strings do not get redacted in OSLogMessages despite OSLogPrivacy options

I have read several times

From what I understand code like this:

import SwiftUI
import OSLog

struct ContentView: View {
  private static let logger = Logger(subsystem: "HCP", category: "ContentView")
  var myprivateData: Date { Date() }
  var myprivateData2: String { "bank-account-111-222-333" }
  let myprivateData3: String = "bank-account-111-222-333"
  
  var body: some View {
    VStack {
      Image(systemName: "globe")
        .imageScale(.large)
        .foregroundStyle(.tint)
      Text("Hello, world!")
    }
    .padding()
    .onAppear(perform: {
      Self.logger.info("test info")
      Self.logger.info("test info \(myprivateData)")
      Self.logger.info("test info \(myprivateData2)")
      Self.logger.info("test info \(myprivateData3)")
      
      Self.logger.info("test info")
      Self.logger.info("test info \(myprivateData, privacy: .private)")
      Self.logger.info("test info \(myprivateData2, privacy: .private)")
      Self.logger.info("test info \(myprivateData3, privacy: .private)")
    })
  }
}

Should result in somewhat redacted log messages, so my expectation would be that dynamic strings

Self.logger.info("test info1")
Self.logger.info("test info2 \(myprivateData)")
Self.logger.info("test info3 \(myprivateData2)")
Self.logger.info("test info4 \(myprivateData3)")

would result in logs like:

info	21:29:07.877698+0200	TestOsLogger	test info1
info	21:29:07.877757+0200	TestOsLogger	test info2 <private>
info	21:29:07.877800+0200	TestOsLogger	test info3 <private>
info	21:29:07.877835+0200	TestOsLogger	test info4 <private>

instead I get

info	21:29:07.874356+0200	TestOsLogger	test info1
info	21:29:07.877531+0200	TestOsLogger	test info2 <private>
info	21:29:07.877615+0200	TestOsLogger	test info3 bank-account-111-222-333
info	21:29:07.877656+0200	TestOsLogger	test info4 bank-account-111-222-333

where clearly date object got redacted, but string not really.

Adding privacy: .private helps here, but it is still a different behavior from what I expected after reading docs.

Is that a change or rather my misunderstanding? Eskimo for the rescue?

Replies

Are you sure that Xcode isn’t getting in the way here?

Consider this program:

import os.log

func main() {
    // subsystem:com.example.apple-samplecode.Test750285
    let log = Logger(subsystem: "com.example.apple-samplecode.Test750285", category: "test")
    let defaultDescription = ""
    let publicDescription = "Fabulous "
    let privateDescription = "Cruel "
    log.log("Hello \(defaultDescription)World!")
    log.log("Hello \(publicDescription, privacy: .public)World!")
    log.log("Hello \(privateDescription, privacy: .private)World!")
}

main()

I put this into a command-line tool project (using Xcode 15.3 on macOS 14.4) and ran it. In Xcode I see this:

Hello World!
Hello Fabulous World!
Hello Cruel World!

This is what I’d expect. Xcode sets up the logging environment so that you can see you log.

In Console I see this:

type: default
time: 08:38:57.730190+0100
process: Test750285
subsystem: com.example.apple-samplecode.Test750285
category: test
message: Hello World!

type: default
time: 08:38:57.730231+0100
process: Test750285
subsystem: com.example.apple-samplecode.Test750285
category: test
message: Hello Fabulous World!

type: default
time: 08:38:57.730247+0100
process: Test750285
subsystem: com.example.apple-samplecode.Test750285
category: test
message: Hello Cruel World!

This is less expected. Xcode’s magic has changed the Console behaviour as well.

Now I ran the tool from Terminal:

% ./Test750285

This time the private stuff is private:

type: default
time: 08:39:26.223567+0100
process: Test750285
subsystem: com.example.apple-samplecode.Test750285
category: test
message: Hello <private>World!

type: default
time: 08:39:26.223598+0100
process: Test750285
subsystem: com.example.apple-samplecode.Test750285
category: test
message: Hello Fabulous World!

type: default
time: 08:39:26.223613+0100
process: Test750285
subsystem: com.example.apple-samplecode.Test750285
category: test
message: Hello <private>World!

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"