Swiftpack.co -  nativedevbr/swift-log as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
nativedevbr/swift-log
Simple logging for simples needs.
.package(url: "https://github.com/nativedevbr/swift-log.git", from: "0.1.1")

Logger

Test

Simple logging for simples needs.

Getting Started

Declare your dependency in your Package.swift file.

.package(name: "Logger", url: "https://github.com/nativedevbr/swift-log.git", from: "0.1.0"),

and add it to your application target.

.target(name: "BestExampleApp", dependencies: ["Logger"]),

Now, let's learn how to use it.

// Import the logger package.
import Logger

// Create a logger instance, the system is a string that identifies this logging instance.
// It's recommended to use an inverse domain like your bundle identifier.
// This logging implementation is based on destinations, each destination represent a place where
// the log messagens will be sent.
let logger = Logger(system: "br.dev.native.best-example-app", destinations: [.console(), .file(url: URL(...)])

// Then, just call the available methods on the logger instance, there is one method for each
// logging level. [verbose, debug, info, warning and error].
logger.info("Hello World!")

// There's support for adding additional context to a log message.
logger.error("something is not working", context: ["user_id": "deadbeef"])

If you prefer, there's possibility for defining a shared logger instance through Logger.main.

Logger.main = Logger(system: "br.dev.native.best-example-app", destinations: [.console(), .file(url: URL(...)])

If Logger.main is used before being initialized the app will crash.

Destinations

This package provides two destinations out of the box, the console and file.

The console destination sends the log messages to the console and it's debug only, so no logs are sent on release builds.

The file destination writes logs messages to a local file that the user must provide.

Boths destinations accepts a Formatter parameter to customize the formatting logic of the message, there's a default implementation.

Custom destinations

This destination approach makes very easy to add new ones, like this example of a destination that sends the log messages to Crashlytics.

import FirebaseCrashlytics

extension Logger.Destination {
  static let crashlytics = Logger.Destination { msg in
    // You can use the default formatter for generating a string, 
    // or implement your own formatting logic.
    let string = Logger.Formatter.default.format(msg)
    Crashlytics.crashlytics().log(string)
  }
}

// Then to use the new destination, simply init a logger passing the `crashlytics` destination.
Logger.main = Logger(system: "br.dev.native.best-example-app", destinations: [.console(), .crashlytics])

Contributing

Pull requests are welcome.

Please make sure to update tests as appropriate.

License

Apacha 2.0

GitHub

link
Stars: 0
Last commit: 1 week 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.

Release Notes

0.1.1
1 week ago

Changes

  • Adds support for watchOS and tvOS.

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