Swiftpack.co -  rimarsh/PrivacyAwareLogTransformer as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
A simple logging middleware to help keep user information out of logs.
.package(url: "https://github.com/rimarsh/PrivacyAwareLogTransformer.git", from: "1.0.0")


A simple logging middleware to help keep user information out of logs.

Build Status Swift 5.1 SPM GitHub tag (latest SemVer) License

Keeping your logs clear of personal user information and other private data is hard. PrivacyAwareLogTransformer helps you make intentional decisions about what to log.

Currently, developers need to be cautious with each log message they write to ensure that they are not leaking personal data. This is both tedious and error-prone, so why not ask the compiler to do protect from logging private data unintentionally.

PrivacyAwareLogTransformer ensures any variables emitted in logs have been sanitized. It then removes those variables and inserts a token while attaching the variable values in a dictionary.

As an added benefit, every log message will be emitted the same way regardless of the variables. This allows trend monitoring and alerting to work well since the log messages will not appear as different strings and therefore confuse the alerting system.

  • ☑ Natural logging syntax using string interpolation
  • ☑ Custom defined, per-type stringifications
  • ☑ Collatable log messages for easy metrics and alerting
  • ☑ Maintain control of how/where/when logs are emitted


logger.log("a message with an object: \(User(name: "Bob"))")


message: a message with an object: {{user_1}}, properties: ["user_1": "<safe value>"]


Swift Package Manager

Swift Package Manager is the recommended way of consuming PrivacyAwareLogTransformer. To integrate it, add a dependency in your Package.swift:

 dependencies: [
   .package(url: "https://github.com/rimarsh/PrivacyAwareLogTransformer.git", from: "1.0")


To integrate PrivacyAwareLogTransformer into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'PrivacyAwareLogTransformer', :git => 'https://github.com/rimarsh/PrivacyAwareLogTransformer.git'


To integrate PrivacyAwareLogTransformer into your Xcode project using Carthage, specify it in your Cartfile:

github "rimarsh/PrivacyAwareLogTransformer"


  1. Implement a PrivacyAwareLogReceiver to take transformed logs and send them to an existing logging infrastructure:
import PrivacyAwareLogTransformer

class LogReceiver: PrivacyAwareLogReceiver {
  func log(_ message: String, properties: [String : String]?, context: [AnyHashable : Any]?) {
      print("message: \(message), properties: \(properties ?? [:]), context: \(context ?? [:])")
  1. Initialize the log transformer in one place or many:
import PrivacyAwareLogTransformer

let receiver = LogReceiver()
var logger = PrivacyAwareLogTransformer()
logger.receiver = receiver
  1. Logging can be done using a natural syntax

Assume, for example, two example data structures (one implements PrivacyAwareLoggable and the other does not):

struct User: PrivacyAwareLoggable {
    let userId: UUID = UUID()
    let name: String

    var loggingValue: String {
        return userId.uuidString

struct Team {
    let team: UUID = UUID()
    let name: String

Logging a static string is just like any other log mechanism.

logger.log("a basic message")


message: a basic message, properties: nil, context: nil

When logging an object, the object must conform to PrivacyAwareLoggable.

logger.log("a message with an object: \(User(name: "Bob"))")


message: a message with an object: {{user_1}}, properties: ['user_1': <uuid>], context: nil

If the object doesn't conform to PrivacyAwareLoggable, it will fail to compile.

logger.log("a message with an object: \(Team(name: "My Team"))")




Feel free to dive in! Open an issue or submit a PR.


MIT © Riley Marsh. See the LICENSE file for more information.


Stars: 0
Last commit: 1 year ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

Initial Release
1 year ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics