Swiftpack.co - steamclock/steamclog-swift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by steamclock.
steamclock/steamclog-swift 1.0.0
A logging library that provides consistent semantics and results on iOS and Android.
⭐️ 1
🕓 1 week ago
iOS
.package(url: "https://github.com/steamclock/steamclog-swift.git", from: "1.0.0")

SteamcLog

Technical Documentation Android Repo

An open source library that consolidates/formalizes the logging setup and usage across all of Steamclock's projects.

Installation

Add the following to your podfile then run pod install

pod 'SteamcLog', :git => "git@github.com:steamclock/steamclog.git"

Note: If your project is using Sentry or XCGLogger, you can remove those from the podfile, as they'll be imported as dependencies for SteamcLog.

In your AppDelegate (or a logging manager), set-up a global instance of SteamcLog:

import SteamcLog

// defined globally
#if DEBUG
private let config = Config(logLevel: .firehose) // this will be used in debug builds.
#else
private let config = Config(logLevel: .release) // this will be used for release builds
#endif

let clog = SteamcLog(config: config)

class AppDelegate: UIApplicationDelegate {
    // ...
}

See configuration documentation for details on logLevel here.

Firebase Crashlytics is no longer a supported destination for crash reporting

Configuration

SteamcLog has a number of configuration options

logLevel: LogLevelPreset

Destination logging levels; it is recommended to use the defaults set by Steamclog instead of initializing these manually. In special cases where more data is desired, update this property. See technical documentation for more details on the available presets.

requireRedacted: Bool

Default value is false. Require that all logged objects conform to Redacted or are all redacted by default.

autoRotateConfig: AutoRotateConfig

By default, logs will rotate every 10 minutes, and store 10 archived log files. AutoRotateConfig allows customization for the auto-rotating behaviour.

AutoRotateConfig has the following fields: fileRotationTime: TimeInterval: The number of seconds before the log file is rotated and archived.

sentryFilter: SentryFilter

By default, all error objects will be sent to Sentry when submitted via the error call. SentryFilter allows you to change this behaviour at the SteamcLog Config-level.

Config(
    // other fields
    sentryFilter: { error in
        if let error = error as? CustomError {
            return true // CustomError errors will no longer be submitted to Sentry
        }
        return false
    }
)

Usage

From there, you can use clog anywhere in your application with the following levels. Note that availability of these logs will depend on your Configuration's logLevel.

  • clog.verbose - Log all of the things! Probably only output to the console by developers, never to devices.
  • clog.debug - Info that is interesting to developers, any information that may be helpful when debugging. Should be stored to system logs for debug builds but never stored in production.
  • clog.info - Routine app operations, used to document changes in state within the application. Minimum level of log stored in device logs in production.
  • clog.warn - Developer concerns or incorrect state etc. Something’s definitely gone wrong, but there’s a path to recover
  • clog.error - Something has gone wrong, report to a remote service (like Sentry)
  • clog.fatal - Something has gone wrong and we cannot recover, so force the app to close.

Each of these functions has the following 3 available signatures clog.<level>(_ message: String) clog.<level>(_ message: String, object: Encodable) clog.<level>(_ message: String, object: Redacted)

Exporting Logs

The log file URL is available via logFileURL() -> URL, or you can get the log file contents using clog.getLogFileContents() -> String?

Variable Redaction

Redacted is a protocol that can be conformed to by a struct or class for marking particular fields as safe for logging. By default, a class/struct conforming to Redacted will have all fields marked as redacted, and you can define logging-safe field using the safeProperties field.

Example:

import SteamcLog
struct User: Codable, Redacted {
    static var safeProperties = Set<String>(["name", "email"])
    let name: String
    let uuid: UUID
    let email: String
    let created: Date
}

In this case, when a User object is logged by Steamclog, it will log something like the following:

let sampleUser = User(name: "Name", uuid: UUID(), email: "hi@steamclock.com", created: Date())
clog.info("Here's a simple model", sampleUser)

And the log will output:

User(name: "Name", uuid: <redacted>, email: "hi@steamclock.com", created: <redacted>)

Using SteamcLog with Netable

If you're also using Netable, you can pipe your logs directly from Netable into SteamcLog.

First, in your Podfile, change

pod 'SteamcLog', :git => "git@github.com:steamclock/steamclog.git"

to

pod 'SteamcLog/Netable', :git => "git@github.com:steamclock/steamclog.git"

Then, when you create your Netable instance, set the log destination to RedactedLogDestination and pass in a reference to your Steamclog instance, like so:

let netable = Netable(baseURL: URL(string: "https://api.thecatapi.com/v1/")!, logDestination: RedactedLogDestination(clog: clog)

GitHub

link
Stars: 1
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

1.0.0 Release
2 weeks ago
  • Fixes issue with fatalError text not being included in Sentry reports (#65)
  • Adds tagging based on environment (#72)
  • Updated the README to point to our Coda docs (#92)
  • Adds Sentry filtering (#94)

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