WSLogger 4.0.0

WSLogger 4.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2023
SPMSupports SPM

Maintained by Ricardo Pereira.



WSLogger 4.0.0

WSLogger

Carthage Compatible CocoaPods Compatible Swift 5.0 Platforms iOS Build Status License MIT

An iOS logger where it's possible to extend the log functionality.

Usage

For example, create a Logger.swift and add your implementation of WSLoggable:

import WSLogger

extension WSLoggable {
    func log(_ message: String, level: WSLogLevel = .debug, customAttributes: [String : Any]? = nil, className: String = "", fileName: NSString = #file, line: Int = #line, function: String = #function) {
        // Log internally
        let text = WSLogger.shared.log(message, level: level, customAttributes: customAttributes, className: String(describing: type(of: self)), fileName: fileName, line: line, function: function)
        // Log remotely using `text`.
        // Fabric, LogEntries, etc.
    }
}

You can add a typealias to avoid importing the WSLogger on every file:

typealias Loggable = WSLoggable
typealias LoggerOptions = WSLoggerOptions
typealias LogLevel = WSLogLevel

Then use the protocol Loggable where you want. The function log will be accessible:

struct WSTableViewCell: Loggable {
    func configure(viewModel: ViewModel) {
        log("Bind model data with views")
        ...
    }
}

It's possible to change the log level with LoggerOptions.defaultLevel property. For example, if LoggerOptions.defaultLevel is debug then all the verbose entries will be ignored.

You can add LoggerOptions.defaultLevel = .none to discard any log events on your test suite. It's also possible ignoring classes with Logger.shared.ignoreClass(WSTableViewCell).

You can execute those operations in debug mode as well. Just write in the console expr -- Logger.shared.ignoreClass(WSTableViewCell).

Extend the log mechanism: example using LogEntries

You can extend the log mechanism as you want. For example, if you want to access the log entries online:

LogEntries dashboard

import Foundation
import WSLogger
import lelib //LogEntries iOS lib

func loggerSetup() {
    LoggerOptions.defaultLevel = .Debug
    WSLogger.shared.traceFile = true
    WSLogger.shared.traceMethod = true
    // LogEntries
    LELog.sharedInstance().token = "XXXX-XXX-XXX-XXXX"
}

extension WSLoggable {
    func log(_ message: String, level: WSLogLevel = .debug, customAttributes: [String : Any]? = nil, className: String = "", fileName: NSString = #file, line: Int = #line, function: String = #function) {
        // Log internally
        let text = WSLogger.shared.log(message, level: level, customAttributes: customAttributes, className: String(describing: type(of: self)), fileName: fileName, line: line, function: function)
        // Log remotely
        LELog.sharedInstance().log(text as NSObject)
    }
}

The complete example is available here.

Installation

Carthage

To install it, simply add the following line to your Cartfile:

github "whitesmith/WSLogger"

Then run carthage update.

Follow the current instructions in Carthage's README for up to date installation instructions.

CocoaPods

To install it, simply add the following line to your Podfile:

pod 'WSLogger'

You will also need to make sure you're opting into using frameworks:

use_frameworks!

Then run pod install with CocoaPods 1.0 or newer.

Manually

Download all the source files and drop them into your project.

Requirements

  • iOS 8.0+
  • Xcode 10 (Swift 4.0)

Contributing

The best way to contribute is by submitting a pull request. We'll do our best to respond to your patch as soon as possible. You can also submit a new GitHub issue if you find bugs or have questions. :octocat:

Credits

Whitesmith

Checkout the excelent topic on Logging in Swift from Krzysztof Zabłocki.