Latest1.0.0
Homepagehttps://github.com/mothule/SquidLogger
LicenseMIT
Platformsios 9.0, requires ARC
Authors

SquidLogger is a multicast logger written in Swift that send to multiple logs.

NetworkLogger.debug("hello world.")
PaymentLogger.info("hello world.")

Output

📋 debug [Network] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - hello world.
💡 info [Payment] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - hello world.

Why did I make this?

My project contains multiple logs. (e.g.: Crashlytics logging, Xcode console, our log server)
I don’t want to write the same code. Like the next.

let text = "Fail to fetching tha User data."
Crashlytics.log(text)
print(text)
OurLogServer.error(text)

I want to control logging per modules. Like the next.

  • log to Networking is warning or more.
  • log to Payment is debug or more.

Therefore I needed this logger.

How to use

Mostly simple

Initialize and default logger.
Log stream is only default console log stream if SquidLoggerManager.configure() arguments empty when initialize.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

      // Initialize code
      SquidLoggerManager.configure()

      // Each logs with log level
      IkaLogger.debug("hello world.")
      IkaLogger.info("hello world.")
      IkaLogger.warn("hello world.")
      IkaLogger.error("hello world.")
      IkaLogger.fatal("hello world.")

      return true
    }
}

Output

📋 info [Default] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - hello world.
💡 info [Default] 2018-03-24 03:38:02.494 AppDelegate.swift(19) application(_:didFinishLaunchingWithOptions:) - hello world.
⚠️ warn [Default] 2018-03-24 03:38:02.496 AppDelegate.swift(20) application(_:didFinishLaunchingWithOptions:) - hello world.
🚫 error [Default] 2018-03-24 03:38:02.496 AppDelegate.swift(21) application(_:didFinishLaunchingWithOptions:) - hello world.
💔 fatal [Default] 2018-03-24 03:38:02.496 AppDelegate.swift(22) application(_:didFinishLaunchingWithOptions:) - hello world.

Attention
This is sample so I don’t recommend you. Because this library is a multicast logger and not a logger.
So confirm below.

How to use detail

Change a log level

SquidLoggerManager.defaultLogLevel will apply to all log streams.

SquidLoggerManager.configureLogLevel(to: .info)
SquidLoggerManager.defaultLogLevel = .warn

Add new log streams

At first, implement a log stream.

struct ConsoleLogger: SquidLogStreaming {
    public func print<T>(_ object: T) {
        Swift.print(object)
    }
}

Next, add stream to SquidLoggerManager.

SquidLoggerManager.configure(streams: [ConsoleLogger()])

Attention
Will remove default log stream from managed log streams if added any new log stream.

Change a log level of a log stream

A log stream(ConsoleLogger) has already been implemented.

SquidLoggerManager.configureLogLevel(to: .error, at: ConsoleLogger.self)

Add new log modules

You just implement below if you want to use for networking area.

struct NetworkLogger: SquidLogger {
    static var category: String = "Network"
    static var logLevel: SquidLogLevel?
}

You will just call a NetworkLogger.

NetworkLogger.info("User data fetch completed")

Attention
SquidLogger isn’t thread safe.
Because this library primary purpose is multicast.
logging is not important purpose.

Change a log level of a log module

struct NetworkLogger: SquidLogger {
    static var category: String = "Network"
    static var logLevel: SquidLogLevel? = .debug
}

NetworkLogger.logLevel = .error

Change a symbol of log level

At first, Implement symboler.

struct MyLogLevelSymbol: SquidLogLevelSymbol {
    func toSymbol(from level: SquidLogLevel) -> String {
        switch level {
        case .info:  return "I"
        case .debug: return "D"
        case .warn:  return "W"
        case .error: return "E"
        case .fatal: return "F"
        case .none:  return ""
        }
    }
}

configure your symboler instance.

SquidLoggerManager.logLevelSymboler = MyLogLevelSymbol()

Change a log message format

struct MyTextFormatter: SquidLogTextFormatter {
    func format<T>(_ params: SquidLogParameter<T>) -> String {
        return "(params.object)"
    }
}
SquidLoggerManager.textFormatter = MyTextFormatter()

Change a log message format of log stream

implement SquidLogStreaming.makeLogText method.

struct ConsoleLogger: SquidLogStreaming {
    func makeLogText<T>(_ params: SquidLogParameter<T>) -> String? {
        return "(params.level):(params.object)"
    }
}

Filter a log message of log stream

Implement SquidLogStreaming.filterLogText method.

struct ConsoleLogger: SquidLogStreaming {
  func filterLogText(_ text: String) -> String {
      guard let regex = try? NSRegularExpression(pattern: "hello", options: []) else { return text }
      let range = NSRange(location: 0, length: text.count)
      return regex.stringByReplacingMatches(in: text, options: [], range: range, withTemplate: "<FILTERED>")
  }
}

IkaLogger.warn("hello world.")
📋 warn [Default] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - <FILTERED> world.

About filtering of log level

Final LevelSquidLogManager.
defaultLogLevel
SquidLogger.
logLevel
SquidLogStreaming.
logLevel
debugdebugnilnil
infodebuginfonil
warndebugnilwarn
warndebuginfowarn
errorerrornilnil
infoerrorinfonil
warnerrornilwarn
warnerrorinfowarn

Runtime Requirements

  • Swift 3.2 or later
  • iOS 9 or later
  • Xcode 9.2 or later

Library implementation policy

  • Don’t forget primary purpose.
  • Lightweight

Latest podspec

{
    "name": "SquidLogger",
    "version": "1.0.0",
    "summary": "SquidLogger is a multicast logger written in Swift that send to multiple logs.",
    "description": "SquidLogger is a multicast logger written in Swift that send to multiple logs.n- iOSn- Swiftn- Logn- Multicast",
    "homepage": "https://github.com/mothule/SquidLogger",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "mothule": "[email protected]"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/mothule/SquidLogger.git",
        "tag": "1.0.0"
    },
    "source_files": "SquidLogger/**/*.swift",
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This