Guest User

Untitled

a guest
Jan 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. import Foundation
  2.  
  3. // MARK: - Logger event type
  4. enum LoggerEventType: String {
  5. case error = "[ERROR]"
  6. case info = "[INFO]"
  7. case debug = "[DEBUG]"
  8. case verbose = "[VERBOSE]"
  9. case warning = "[WARNING]"
  10. case severe = "[SEVERE]"
  11. }
  12.  
  13. final class Logger {
  14.  
  15. // MARK: - Properties
  16. private static var isLoggingEnabled: Bool {
  17. #if DEVELOP
  18. return true
  19. #else
  20. return false
  21. #endif
  22. }
  23.  
  24. private static var loggerInfo: String {
  25. let formatter = DateFormatter()
  26. formatter.dateFormat = "yyyy-MM-dd hh:mm:ssSSS"
  27. formatter.locale = .current
  28. formatter.timeZone = .current
  29. return "[LOGGER] \(formatter.string(from: Date()))"
  30. }
  31.  
  32. // MARK: - Methods
  33. /// Extract the file name from the file path.
  34. ///
  35. /// - Parameter filePath: Full file path in bundle.
  36. /// - Returns: File Name with extension.
  37. private class func sourceFileName(filePath: String) -> String {
  38. let components = filePath.components(separatedBy: "/")
  39. return components.isEmpty ? "" : components.last!
  40. }
  41.  
  42. /// Logs error messages on console with prefix.
  43. ///
  44. /// - Parameters:
  45. /// - object: Object or message to be logged.
  46. /// - filename: File name from where log to be done.
  47. /// - line: Line number in file from where the logging is done.
  48. /// - column: Column number of the log message.
  49. /// - funcName: Name of the function from where the logging is done.
  50. class func error( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
  51. if isLoggingEnabled {
  52. print("\(loggerInfo) \(LoggerEventType.error.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
  53. }
  54. }
  55.  
  56. /// Logs info messages on console with prefix.
  57. ///
  58. /// - Parameters:
  59. /// - object: Object or message to be logged.
  60. /// - filename: File name from where log to be done.
  61. /// - line: Line number in file from where the logging is done.
  62. /// - column: Column number of the log message.
  63. /// - funcName: Name of the function from where the logging is done.
  64. class func info( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
  65. if isLoggingEnabled {
  66. print("\(loggerInfo) \(LoggerEventType.info.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
  67. }
  68. }
  69.  
  70. /// Logs debug messages on console with prefix.
  71. ///
  72. /// - Parameters:
  73. /// - object: Object or message to be logged.
  74. /// - filename: File name from where log to be done.
  75. /// - line: Line number in file from where the logging is done.
  76. /// - column: Column number of the log message.
  77. /// - funcName: Name of the function from where the logging is done.
  78. class func debug( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
  79. if isLoggingEnabled {
  80. print("\(loggerInfo) \(LoggerEventType.debug.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
  81. }
  82. }
  83.  
  84. /// Logs messages verbosely on console with prefix.
  85. ///
  86. /// - Parameters:
  87. /// - object: Object or message to be logged.
  88. /// - filename: File name from where log to be done.
  89. /// - line: Line number in file from where the logging is done.
  90. /// - column: Column number of the log message.
  91. /// - funcName: Name of the function from where the logging is done.
  92. class func verbose( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
  93. if isLoggingEnabled {
  94. print("\(loggerInfo) \(LoggerEventType.verbose.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
  95. }
  96. }
  97.  
  98. /// Logs warnings verbosely on console with prefix.
  99. ///
  100. /// - Parameters:
  101. /// - object: Object or message to be logged.
  102. /// - filename: File name from where log to be done.
  103. /// - line: Line number in file from where the logging is done.
  104. /// - column: Column number of the log message.
  105. /// - funcName: Name of the function from where the logging is done.
  106. class func warning( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
  107. if isLoggingEnabled {
  108. print("\(loggerInfo) \(LoggerEventType.warning.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
  109. }
  110. }
  111.  
  112. /// Logs severe events on console with prefix.
  113. ///
  114. /// - Parameters:
  115. /// - object: Object or message to be logged.
  116. /// - filename: File name from where log to be done.
  117. /// - line: Line number in file from where the logging is done.
  118. /// - column: Column number of the log message.
  119. /// - funcName: Name of the function from where the logging is done.
  120. class func severe( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
  121. if isLoggingEnabled {
  122. print("\(loggerInfo) \(LoggerEventType.severe.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
  123. }
  124. }
  125. }
Add Comment
Please, Sign In to add comment