Last active
September 14, 2018 06:09
-
-
Save poca-p0ca/016eb6eca80d83b002fc55fbbbeec4dd to your computer and use it in GitHub Desktop.
Light-Weight-Easy-to-Use Logger Class
This file contains hidden or 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
// | |
// Logger.swift | |
// | |
// Created by Cyan on 2018. 9. 14.. | |
// Copyright © 2018 Cyan. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
/** | |
Very simple easy-to-use Logger. | |
*/ | |
class Logger{ | |
/** | |
Represents log's type. | |
*/ | |
internal enum LogType { | |
case info | |
case debug | |
case warning | |
case error | |
} | |
/** | |
Leaves log of information. prints given message in console, and writes it to log file. | |
returns actual written log message. | |
*/ | |
@discardableResult | |
class func log(info message: String, line: Int = #line, function: String = #function)-> String{ | |
return log(.info, message, line, function) | |
} | |
/** | |
Leaves log of debug. prints given message in console, and writes it to log file. | |
returns actual written log message. | |
*/ | |
@discardableResult | |
class func log(debug message: String, line: Int = #line, function: String = #function)-> String{ | |
return log(.debug, message, line, function) | |
} | |
/** | |
Leaves log of warning. prints given message in console, and writes it to log file. | |
returns actual written log message. | |
*/ | |
@discardableResult | |
class func log(warning message: String, line: Int = #line, function: String = #function)-> String{ | |
return log(.warning, message, line, function) | |
} | |
/** | |
Leaves log of error. prints given message in console, and writes it to log file. | |
returns actual written log message. | |
*/ | |
@discardableResult | |
class func log(error message: String, line: Int = #line, function: String = #function)-> String{ | |
return log(.error, message, line, function) | |
} | |
internal class func log(_ type: LogType, _ message: String, _ line: Int, _ function: String)-> String{ | |
let now = NSDate() | |
let dateFormatter = DateFormatter() | |
dateFormatter.locale = NSLocale(localeIdentifier: "ko_KR") as Locale? | |
dateFormatter.timeStyle = .medium | |
dateFormatter.dateStyle = .medium | |
let nowdate = dateFormatter.string(from: now as Date) | |
var prefix: String | |
switch type { | |
case .info: | |
prefix = "[INFO]" | |
case .debug: | |
prefix = "[DEBUG]" | |
case .warning: | |
prefix = "[WARN]" | |
case .error: | |
prefix = "[ERROR]" | |
} | |
let log = "\(prefix) : [\(nowdate)] in [\(function)], (line: \(line)) : \(message)" | |
print(log) | |
let logData = "\(log)\n".data(using: .utf8)! | |
let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as URL | |
let fileDirectory = directory.appendingPathComponent("debugLogs", isDirectory: false) | |
DispatchQueue.global().sync { | |
if !FileManager.default.fileExists(atPath: fileDirectory.path){ | |
let createdFile = FileManager.default.createFile(atPath: fileDirectory.path, contents: logData, attributes: nil) | |
} | |
if let fileHandle = FileHandle(forWritingAtPath: fileDirectory.path){ | |
defer{ | |
fileHandle.closeFile() | |
} | |
fileHandle.seekToEndOfFile() | |
fileHandle.write(logData) | |
} | |
} | |
return String(data: logData, encoding: .utf8)! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment