Skip to content

Latest commit

 

History

History
76 lines (61 loc) · 1.82 KB

README.md

File metadata and controls

76 lines (61 loc) · 1.82 KB

LogMacro

Swift 5 SPM compatible MIT License

A macro that outputs logs with formatting anywhere across modules.

Installation

Swift Package Manager

.package(url: "https://github.com/to4iki/LogMacro", from: <#version#>)

Usage

debug

#logDebug("debug")

warn

#logWarn("warn", category: .tracking)

fault

enum MyError: Error {
  case unknown
}

#logFault(MyError.unknown, category: .network)

Restrict logs to be posted per level

Set global LogLevel and configure which logs can be sent on a per-application basis.

LogProcess.shared.setEnabledLogLevel(.warn)

Replacing log message

Register a LogReplacingPlugin for rewrite outgoing log messages.

/// Replace the string "password" with an empty string.
struct PasswordLogReplacingPlugin: LogReplacingPlugin {
  func newMessage(from message: String, level: LogLevel) -> String {
    message.replacingOccurrences(of: "password", with: "")
  }
}

LogProcess.shared.setReplacingPlugin(PasswordLogReplacingPlugin())

Processing after log post

Register a LogPostActionPlugin for perform any processing after the log is post.

/// Print output only when message is `MyError`.
struct MyErrorLogPostActionPlugin: LogPostActionPlugin {
  func execute(message: Any, level: LogLevel, file: String, function: String, line: Int) {
    guard level >= .fault else {
      return
    }
    guard message is MyError else {
      return
    }
    print("MyErrorLogPostActionPlugin")
  }
}

LogProcess.shared.setPostActionPlugins(MyErrorLogPostActionPlugin())

License

LogMacro is released under the MIT license.