Skip to content

Instantly share code, notes, and snippets.

@qdequele
Created February 9, 2017 09:22
Show Gist options
  • Save qdequele/db8b15645720e99b5dec217d511364e7 to your computer and use it in GitHub Desktop.
Save qdequele/db8b15645720e99b5dec217d511364e7 to your computer and use it in GitHub Desktop.
Simple logger for swift
//
// 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