Is there a way to make interpolated strings public by default when using Swift Logger?

445 Views Asked by At

In Swift, I use code like this:

let logger = Logger()
let someInfo = "information"
logger.warning("blah: \(someInfo)")

but the console output shows: blah: \<private>

unless I change the final line to:

logger.warning("blah: \(someInfo, privacy: .public)")

I'm wondering if there is a way to make the default to be .public -- while I am developing my app, I want to see all of the messages -- without having to include the privacy: .public everywhere.

Edit. I am using a workaround for now, but I don't love the solution, and hoping there is some official support, the workaround I'm using is a method like this:

static func logwarn(_ message: String) {
    let logger = Logger()
    logger.warning("\(message, privacy: .public)")
}

then I call logwarn, and can #ifdef it so that it's implemented differently (ie, not public) in release code.

1

There are 1 best solutions below

1
horseshoe7 On

This isn't an answer but an extension on your idea which I thought to be good.

So I wrote this:

import Foundation
import OSLog

/**
 An extension that makes it easier to export data into the application log that can be viewed in the app (like in a text view).  By default, OSLogMessage will redact the arguments in interpolated strings, but it's a pain to have to specify privacy level, especially on teams where developers might not be hard-core Swift devs; it requires more cognitive load out of any collaborator.  Also this default behavior given by Apple  is often not useful when trying to diagnose issues in development that aren't reproducible by the developer.
 
 As such we introduce these flavours below that are designed to be friendly for this purpose.
 
 As we didn't want to deal with potential performance impacts, such as doing tracelogging during callbacks, etc. we only support info, warning, and error here.  The other log types are assumed to be developer only type stuff anyway.
 
 */

var _logsFileAndLineNumber = true

public extension Logger {

    static var logsFileAndLineNumber: Bool {
        get { _logsFileAndLineNumber }
        set { _logsFileAndLineNumber = newValue }
    }

    /// a wrapper to allow log message content to be public by default.
    func warning_(_ message: String, file: String = #file, lineNumber: Int = #line) {
        if Self.logsFileAndLineNumber {
            self.warning("\((file as NSString).lastPathComponent, privacy: .public)(\(lineNumber, privacy: .public)): \(message, privacy: .public)")
        } else {
            self.warning("\(message, privacy: .public)")
        }
    }

    /// a wrapper to allow log message content to be public by default.
    func info_(_ message: String, file: String = #file, lineNumber: Int = #line) {
        if Self.logsFileAndLineNumber {
            self.info("\((file as NSString).lastPathComponent, privacy: .public)(\(lineNumber, privacy: .public)): \(message, privacy: .public)")
        } else {
            self.info("\(message, privacy: .public)")
        }
    }

    /// a wrapper to allow log message content to be public by default.
    func error_(_ message: String, file: String = #file, lineNumber: Int = #line) {
        if Self.logsFileAndLineNumber {
            self.error("\((file as NSString).lastPathComponent, privacy: .public)(\(lineNumber, privacy: .public)): \(message, privacy: .public)")
        } else {
            self.error("\(message, privacy: .public)")
        }
    }
}