Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom log action #665

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions Sources/OAuthLogProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum OAuthLogLevel: Int {
public protocol OAuthLogProtocol {

var level: OAuthLogLevel { get }

var logAction: ((_ message: String) -> Void)? { get set }

/// basic level of print messages
func trace<T>(_ message: @autoclosure () -> T, filename: String, line: Int, function: String)
Expand All @@ -39,28 +41,44 @@ extension OAuthLogProtocol {
let logLevel = OAuthLogLevel.trace
// deduce based on the current log level vs. globally set level, to print such log or not
if level.rawValue >= logLevel.rawValue {
print("[TRACE] \((filename as NSString).lastPathComponent) [\(line)]: \(message())")
let message = "[TRACE] \((filename as NSString).lastPathComponent) [\(line)]: \(message())"
if let action = logAction {
action(message)
} else {
print(message)
}
}
}

public func warn<T>(_ message: @autoclosure () -> T, filename: String = #file, line: Int = #line, function: String = #function) {
let logLevel = OAuthLogLevel.warn
if level.rawValue >= logLevel.rawValue {
print("[WARN] \(self) = \((filename as NSString).lastPathComponent) [\(line)]: \(message())")
let message = "[WARN] \(self) = \((filename as NSString).lastPathComponent) [\(line)]: \(message())"
if let action = logAction {
action(message)
} else {
print(message)
}
}
}

public func error<T>(_ message: @autoclosure () -> T, filename: String = #file, line: Int = #line, function: String = #function) {
let logLevel = OAuthLogLevel.error
if level.rawValue >= logLevel.rawValue {
print("[ERROR] \((filename as NSString).lastPathComponent) [\(line)]: \(message())")
let message = "[ERROR] \((filename as NSString).lastPathComponent) [\(line)]: \(message())"
if let action = logAction {
action(message)
} else {
print(message)
}
}

}
}

public struct OAuthDebugLogger: OAuthLogProtocol {
public let level: OAuthLogLevel
public var logAction: ((_ message: String) -> Void)? = nil
init(_ level: OAuthLogLevel) {
self.level = level
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/OAuthSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,14 @@ extension OAuthSwift {
Self.log = OAuthDebugLogger(level)
OAuthSwift.log?.trace("Logging enabled with level: \(level)")
}

/// Set a custom log action to preform instead of printing to the console
/// Used to integrate external logging systems
public static func setCustomLogAction(_ action: ((_ message: String) -> Void)?) {
if Self.log == nil {
self.setLogLevel(.error)
}
Self.log?.logAction = action
OAuthSwift.log?.trace("Custom log action enabled with level: \(Self.log!.level)")
}
}