Swiftpack.co - Package - steamclock/steamclog-swift

SteamcLog

Current Proposal Spec

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 Crashlytics, Fabric, 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.

For Crashlytics support, follow the instructions from the official Crashlytics docs, skipping the podfile changes.

Configuration

SteamcLog has a number of configuration options

logLevel: LogLevelPreset

Default value is: develop. There are four log level presets available, each of which has different logging outputs.

| LogLevelPreset | Disk Level | System Level | Remote Level | |-------------------|------------|--------------|--------------| | firehose | verbose | verbose | none | | develop | none | debug | none | | release | none | none | warn | | releaseAdvanced | verbose | none | warn |

In most cases, you'll be able to get by using firehose or develop on debug builds, and release or releaseAdvanced for production builds. Note that if you're using releaseAdvanced you must build in a way for the client to email you the disk logs.

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.

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 Crashlytics) 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

Dependencies

Used By

Total: 0