Skip to content

Instantly share code, notes, and snippets.

@shial4
Last active August 27, 2018 02:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shial4/abd0be7b79ea20a2bc7008a8b1e92f23 to your computer and use it in GitHub Desktop.
Save shial4/abd0be7b79ea20a2bc7008a8b1e92f23 to your computer and use it in GitHub Desktop.
Swift Logger
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)
}
}
@shial4
Copy link
Author

shial4 commented Aug 24, 2018

Simple yet elegant swift logger.

Usage

Log.v(123)
Log.i("ABC")
Log.d("@$#!^%")
Log.w(Date())
Log.e([0.1,1,"A",Date()])

Example Log:

1990-02-19T22:45:36.250Z [VERBOSE] MyFile.swift:19 - 123
1991-03-20T20:33:44.777Z [INFO] MyFile.swift:20 - ABC
1992-04-21T09:53:51.021Z [DEBUG] MyFile.swift:21 - @$#!^%
1993-05-22T11:05:02.000Z [WARNING] MyFile.swift:22 - 2017-10-04 22:45:36 +0000
1994-06-23T15:13:00.146Z [ERROR] MyFile.swift:23 - [0.10000000000000001, 1, "A", 2017-10-04 09:55:36 +0000]

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment