|
| 1 | +// |
| 2 | +// CoreRouter.swift |
| 3 | +// CoreModule |
| 4 | +// |
| 5 | +// Created by Maxim Ivanov on 16.12.2021. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +extension Logger { |
| 11 | + |
| 12 | + public func debug(_ message: String) { |
| 13 | + log(message, level: .debug) |
| 14 | + } |
| 15 | + |
| 16 | + public func logWithContext(_ message: String, |
| 17 | + level: LogLevel = .info, |
| 18 | + file: String = #file, |
| 19 | + function: String = #function, |
| 20 | + line: Int = #line) { |
| 21 | + guard isDevelopment() else { return } |
| 22 | + |
| 23 | + log(message, level: level, file: file, function: function, line: line) |
| 24 | + } |
| 25 | + |
| 26 | + public func errorWithContext(_ error: Error, |
| 27 | + file: String = #file, |
| 28 | + function: String = #function, |
| 29 | + line: Int = #line) { |
| 30 | + guard isDevelopment() else { return } |
| 31 | + |
| 32 | + log("\(error)", level: .error, file: file, function: function, line: line) |
| 33 | + } |
| 34 | + |
| 35 | + public func logState<State>(actionName: String, |
| 36 | + _ state: State, |
| 37 | + file: String = #file, |
| 38 | + function: String = #function, |
| 39 | + line: Int = #line) { |
| 40 | + guard isDevelopment() else { return } |
| 41 | + let text = "\(actionName) result:\n\(state)" |
| 42 | + |
| 43 | + log(text, level: .info, file: file, function: function, line: line) |
| 44 | + } |
| 45 | + |
| 46 | + public func logSending<T>(_ object: T, |
| 47 | + toModelStream modelStreamName: String, |
| 48 | + file: String = #file, |
| 49 | + function: String = #function, |
| 50 | + line: Int = #line) { |
| 51 | + guard isDevelopment() else { return } |
| 52 | + let text = "Sending \(type(of: object)) = \(object) to the \(modelStreamName) model stream." |
| 53 | + |
| 54 | + log(text, level: .info, file: file, function: function, line: line) |
| 55 | + } |
| 56 | + |
| 57 | + public func logReceiving<T>(_ object: T, |
| 58 | + fromModelStream modelStreamName: String, |
| 59 | + file: String = #file, |
| 60 | + function: String = #function, |
| 61 | + line: Int = #line) { |
| 62 | + guard isDevelopment() else { return } |
| 63 | + let text = "Received \(type(of: object)) = \(object) from the \(modelStreamName) model stream." |
| 64 | + |
| 65 | + log(text, level: .info, file: file, function: function, line: line) |
| 66 | + } |
| 67 | + |
| 68 | + public func log(installedFeatureName: String) { |
| 69 | + debug("The \(installedFeatureName) feature has been installed.") |
| 70 | + } |
| 71 | + |
| 72 | + public func log(dismissedFeatureName: String) { |
| 73 | + debug("The \(dismissedFeatureName) feature has been dismissed.") |
| 74 | + } |
| 75 | + |
| 76 | + private func log(_ text: String, level: LogLevel, file: String, function: String, line: Int) { |
| 77 | + let description = "\((file as NSString).lastPathComponent) line:\(line) \(function)" |
| 78 | + |
| 79 | + log("\(text) -> \(description)", level: level) |
| 80 | + } |
| 81 | +} |
0 commit comments