Created
February 9, 2017 09:22
-
-
Save qdequele/db8b15645720e99b5dec217d511364e7 to your computer and use it in GitHub Desktop.
Simple logger for swift
This file contains 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 | |
// Crisp | |
// | |
// Created by Quentin de Quelen on 08/02/2017. | |
// Copyright Β© 2017 qdequele. All rights reserved. | |
// | |
import Foundation | |
struct logEnable { | |
var description: Bool = true | |
var verbose: Bool = true | |
var debug: Bool = true | |
var success: Bool = true | |
var `continue`: Bool = true | |
var warning: Bool = true | |
var error: Bool = true | |
} | |
class log { | |
static var isEnable: Bool = true | |
static var logLevel: logEnable = logEnable() | |
class func description( | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.description { | |
basic("", function: function, file: file, line: line) | |
} | |
} | |
class func verbose(_ items: Any..., | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.verbose { | |
basic("[Verbose] π€ \(items)", function: function, file: file, line: line) | |
} | |
} | |
class func verbose( | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.verbose { | |
basic("[Verbose] π€", function: function, file: file, line: line) | |
} | |
} | |
class func debug(_ items: Any..., | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.debug { | |
basic("[Debug] π \(items)", function: function, file: file, line: line) | |
} | |
} | |
class func debug( | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.debug { | |
basic("[Debug] π", function: function, file: file, line: line) | |
} | |
} | |
class func success(_ items: Any..., | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.success { | |
basic("[Success] π \(items)", function: function, file: file, line: line) | |
} | |
} | |
class func success( | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.success { | |
basic("[Success] π", function: function, file: file, line: line) | |
} | |
} | |
class func `continue`(_ items: Any..., | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.continue { | |
basic("[Success] π \(items)", function: function, file: file, line: line) | |
} | |
} | |
class func `continue`( | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.continue { | |
basic("[Success] π", function: function, file: file, line: line) | |
} | |
} | |
class func warning(_ items: Any..., | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.warning { | |
basic("[Warning] π \(items)", function: function, file: file, line: line) | |
} | |
} | |
class func warning( | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.warning { | |
basic("[Warning] π", function: function, file: file, line: line) | |
} | |
} | |
class func error(_ items: Any..., | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.error { | |
basic("[Error] β€οΈ \(items)", function: function, file: file, line: line) | |
} | |
} | |
class func error( | |
function: String = #function, | |
file: String = #file, | |
line: Int = #line) | |
{ | |
if logLevel.error { | |
basic("[Error] β€οΈ", function: function, file: file, line: line) | |
} | |
} | |
static func basic( | |
_ items: Any..., | |
function: String, | |
file: String, | |
line: Int) | |
{ | |
if isEnable { | |
print("(\(fileNameWithoutSuffix(file)):\(function) : \(line)) - \(items)") | |
} | |
} | |
static func fileNameOfFile(_ file: String) -> String { | |
let fileParts = file.components(separatedBy: "/") | |
if let lastPart = fileParts.last { | |
return lastPart | |
} | |
return "" | |
} | |
static func fileNameWithoutSuffix(_ file: String) -> String { | |
let fileName = fileNameOfFile(file) | |
if !fileName.isEmpty { | |
let fileNameParts = fileName.components(separatedBy: ".") | |
if let firstPart = fileNameParts.first { | |
return firstPart | |
} | |
} | |
return "" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment