Skip to content

Instantly share code, notes, and snippets.

@kyungpyoda
Created May 28, 2021 05:03
Show Gist options
  • Save kyungpyoda/145de60a98848a76ba5833547ed51fc6 to your computer and use it in GitHub Desktop.
Save kyungpyoda/145de60a98848a76ba5833547ed51fc6 to your computer and use it in GitHub Desktop.
[Swift] iOS custom Logger for debugging
//
// Logger.swift
//
// Created by 홍경표 on 2021/05/28.
//
import Foundation
final class Logger {
enum LEVEL: String {
case DEBUG = "DEBUG"
case INFO = "INFO"
case ERROR = "ERROR"
case FATAL = "FATAL"
}
private init() {}
fileprivate func logEvent(_ identifier: String, dateFormat: DateFormatter, content: String) {
print("\(dateFormat.string(from: Date())): \(identifier) -> \(content)")
}
class func log(level: LEVEL, message: String, file: String = #file, function: String = #function, line: Int = #line) {
let dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
let dfmt = DateFormatter()
dfmt.dateFormat = dateFormat
let filename = file.split(separator: "/").last!
print("[\(level.rawValue) \(dfmt.string(from: Date())) \(filename)(line:\(line))] \(message)")
}
class func debug(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(level: .DEBUG, message: message, file: file, function: function, line: line)
}
class func info(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(level: .INFO, message: message, file: file, function: function, line: line)
}
class func error(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(level: .ERROR, message: message, file: file, function: function, line: line)
}
class func fatal(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(level: .FATAL, message: message, file: file, function: function, line: line)
}
class func debug(_ format: String, _ arguments: CVarArg..., file: String = #file, function: String = #function, line: Int = #line) {
log(level: .DEBUG, message: String(format: format, arguments), file: file, function: function, line: line)
}
class func info(_ format: String, _ arguments: CVarArg..., file: String = #file, function: String = #function, line: Int = #line) {
log(level: .INFO, message: String(format: format, arguments), file: file, function: function, line: line)
}
class func error(_ format: String, _ arguments: CVarArg..., file: String = #file, function: String = #function, line: Int = #line) {
log(level: .ERROR, message: String(format: format, arguments), file: file, function: function, line: line)
}
class func fatal(_ format: String, _ arguments: CVarArg..., file: String = #file, function: String = #function, line: Int = #line) {
log(level: .FATAL, message: String(format: format, arguments), file: file, function: function, line: line)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment