Swift Logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public class Log { | |
private enum LogType: Int, CustomStringConvertible { | |
public var description: String { | |
switch self { | |
case .verbose: | |
return "[VERBOSE]" | |
case .info: | |
return "[INFO]" | |
case .debug: | |
return "[DEBUG]" | |
case .warning: | |
return "[WARNING]" | |
case .error: | |
return "[ERROR]" | |
} | |
} | |
case verbose = 0 | |
case info = 1 | |
case debug = 2 | |
case warning = 3 | |
case error = 4 | |
} | |
private static var formatter: DateFormatter = { | |
let dateformatter = DateFormatter() | |
dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" | |
dateformatter.timeZone = TimeZone(abbreviation: "UTC") | |
dateformatter.locale = Locale(identifier: "en_US_POSIX") | |
return dateformatter | |
}() | |
private class func log(_ message: Any, type: LogType, file: String, line: UInt) { | |
let log: String = "\(formatter.string(from: Date())) \(type.description) \(file.split(separator: "/").last ?? ""):\(line) \(message)" | |
print(log) | |
} | |
//MARK: Log functions | |
public class func v(_ message: @autoclosure () -> Any, _ file: String = #file, _ line: UInt = #line) { | |
log(message(), type: .verbose, file: file, line: line) | |
} | |
public class func i(_ message: @autoclosure () -> Any, _ file: String = #file, _ line: UInt = #line) { | |
log(message(), type: .info, file: file, line: line) | |
} | |
public class func d(_ message: @autoclosure () -> Any, _ file: String = #file, _ line: UInt = #line) { | |
log(message(), type: .debug, file: file, line: line) | |
} | |
public class func w(_ message: @autoclosure () -> Any, _ file: String = #file, _ line: UInt = #line) { | |
log(message(), type: .warning, file: file, line: line) | |
} | |
public class func e(_ message: @autoclosure () -> Any, _ file: String = #file, _ line: UInt = #line) { | |
log(message(), type: .error, file: file, line: line) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple yet elegant swift logger.
Usage
Example Log:
To use it. Simple compy content of this file into your project. Replace all your swift
print()
with Log and that's it!You can modify this log class how you like. Change the print format, add condition for log hierarhy and more
For more advance Swift logger take a look in here:
SLLog