Last active
February 9, 2026 20:37
-
-
Save fxm90/08a187c5d6b365ce2305c194905e61c2 to your computer and use it in GitHub Desktop.
A simple log extension on `String` using literal expressions
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
| // | |
| // String+Log.swift | |
| // | |
| // Created by Felix Mau on 16.09.18. | |
| // Copyright Β© 2018 Felix Mau. All rights reserved. | |
| // | |
| import Foundation | |
| extension String { | |
| // MARK: - Types | |
| enum LogLevel { | |
| case debug | |
| case info | |
| case warning | |
| case error | |
| var emoji: Character { | |
| switch self { | |
| case .debug: "π" | |
| case .info: "βΉοΈ" | |
| case .warning: "β οΈ" | |
| case .error: "π¨" | |
| } | |
| } | |
| } | |
| // MARK: - Private Properties | |
| /// The formatter we use to prefix the log output with the current date and time. | |
| private static let logDateFormatter: DateFormatter = { | |
| let dateFormatter = DateFormatter() | |
| dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" | |
| return dateFormatter | |
| }() | |
| // MARK: - Public Methods | |
| func log(level: LogLevel, file: String = #file, function: String = #function, line: UInt = #line) { | |
| #if DEBUG | |
| let timestamp = Self.logDateFormatter.string(from: Date()) | |
| let filename = URL(fileURLWithPath: file).lastPathComponent | |
| print("\(level.emoji) \(timestamp) [\(filename):\(line)] \(function) - \(self)") | |
| #endif | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Output