Skip to content

A macro that outputs logs with formatting anywhere across modules.

License

Notifications You must be signed in to change notification settings

to4iki/LogMacro

Repository files navigation

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.

About

A macro that outputs logs with formatting anywhere across modules.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages