Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- // MARK: - Logger event type
- enum LoggerEventType: String {
- case error = "[ERROR]"
- case info = "[INFO]"
- case debug = "[DEBUG]"
- case verbose = "[VERBOSE]"
- case warning = "[WARNING]"
- case severe = "[SEVERE]"
- }
- final class Logger {
- // MARK: - Properties
- private static var isLoggingEnabled: Bool {
- #if DEVELOP
- return true
- #else
- return false
- #endif
- }
- private static var loggerInfo: String {
- let formatter = DateFormatter()
- formatter.dateFormat = "yyyy-MM-dd hh:mm:ssSSS"
- formatter.locale = .current
- formatter.timeZone = .current
- return "[LOGGER] \(formatter.string(from: Date()))"
- }
- // MARK: - Methods
- /// Extract the file name from the file path.
- ///
- /// - Parameter filePath: Full file path in bundle.
- /// - Returns: File Name with extension.
- private class func sourceFileName(filePath: String) -> String {
- let components = filePath.components(separatedBy: "/")
- return components.isEmpty ? "" : components.last!
- }
- /// Logs error messages on console with prefix.
- ///
- /// - Parameters:
- /// - object: Object or message to be logged.
- /// - filename: File name from where log to be done.
- /// - line: Line number in file from where the logging is done.
- /// - column: Column number of the log message.
- /// - funcName: Name of the function from where the logging is done.
- class func error( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
- if isLoggingEnabled {
- print("\(loggerInfo) \(LoggerEventType.error.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
- }
- }
- /// Logs info messages on console with prefix.
- ///
- /// - Parameters:
- /// - object: Object or message to be logged.
- /// - filename: File name from where log to be done.
- /// - line: Line number in file from where the logging is done.
- /// - column: Column number of the log message.
- /// - funcName: Name of the function from where the logging is done.
- class func info( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
- if isLoggingEnabled {
- print("\(loggerInfo) \(LoggerEventType.info.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
- }
- }
- /// Logs debug messages on console with prefix.
- ///
- /// - Parameters:
- /// - object: Object or message to be logged.
- /// - filename: File name from where log to be done.
- /// - line: Line number in file from where the logging is done.
- /// - column: Column number of the log message.
- /// - funcName: Name of the function from where the logging is done.
- class func debug( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
- if isLoggingEnabled {
- print("\(loggerInfo) \(LoggerEventType.debug.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
- }
- }
- /// Logs messages verbosely on console with prefix.
- ///
- /// - Parameters:
- /// - object: Object or message to be logged.
- /// - filename: File name from where log to be done.
- /// - line: Line number in file from where the logging is done.
- /// - column: Column number of the log message.
- /// - funcName: Name of the function from where the logging is done.
- class func verbose( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
- if isLoggingEnabled {
- print("\(loggerInfo) \(LoggerEventType.verbose.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
- }
- }
- /// Logs warnings verbosely on console with prefix.
- ///
- /// - Parameters:
- /// - object: Object or message to be logged.
- /// - filename: File name from where log to be done.
- /// - line: Line number in file from where the logging is done.
- /// - column: Column number of the log message.
- /// - funcName: Name of the function from where the logging is done.
- class func warning( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
- if isLoggingEnabled {
- print("\(loggerInfo) \(LoggerEventType.warning.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
- }
- }
- /// Logs severe events on console with prefix.
- ///
- /// - Parameters:
- /// - object: Object or message to be logged.
- /// - filename: File name from where log to be done.
- /// - line: Line number in file from where the logging is done.
- /// - column: Column number of the log message.
- /// - funcName: Name of the function from where the logging is done.
- class func severe( _ object: Any, filename: String = #file, line: Int = #line, column: Int = #column, funcName: String = #function) {
- if isLoggingEnabled {
- print("\(loggerInfo) \(LoggerEventType.severe.rawValue) [\(sourceFileName(filePath: filename))]: {\(line), \(column)} \(funcName) -> \(object)")
- }
- }
- }
Add Comment
Please, Sign In to add comment