Skip to content

Instantly share code, notes, and snippets.

@tanakeiQ
Created February 3, 2018 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanakeiQ/3ed765ebcfe0946fd330e54cc2479434 to your computer and use it in GitHub Desktop.
Save tanakeiQ/3ed765ebcfe0946fd330e54cc2479434 to your computer and use it in GitHub Desktop.
swift4-logger-service.swift
public class Logger {
fileprivate enum Prefix: String {
case Info = "🍀"
case Warn = "🍋"
case Error = "🍎"
case Happy = "🎀"
}
public static func info(_ items: String...) {
print(serialize(items, prefix: .Info))
}
public static func warn(_ items: String...) {
print(serialize(items, prefix: .Warn))
}
public static func error(_ items: String...) {
print(serialize(items, prefix: .Error))
}
public static func happy(_ items: String...) {
print(serialize(items, prefix: .Happy))
}
private static func serialize(_ items: [String], prefix: Prefix) -> String {
return items.map { $0
.replacingOccurrences(of: "\n", with: "\n\(prefix.rawValue) ", options: .regularExpression, range: nil)
.replacingOccurrences(of: "^", with: "\(prefix.rawValue) ", options: .regularExpression, range: nil)
.replacingOccurrences(of: "$", with: "\n", options: .regularExpression, range: nil)
}.joined()
}
}
@tanakeiQ
Copy link
Author

tanakeiQ commented Feb 3, 2018

example

Logger.info("Info")
Logger.warn("Warning")
Logger.error("Error")
Logger.happy("Happy!")

output

2018-02-04 2 12 36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment