|
1 | 1 | # PrettyLogger |
2 | 2 |
|
3 | | - |
| 3 | + |
4 | 4 |  |
5 | 5 |  |
6 | 6 |
|
7 | 7 | ## Introduction |
8 | | -A pretty set of log functions to print message in console using levels (Debug, Info, Trace, Warning & Error) and emojis to improve visibility 💪 |
| 8 | +A modern Swift logging library that integrates with Apple's unified logging system (OSLog) while maintaining a simple and familiar API. Provides structured logging with categories, privacy controls, and seamless integration with system debugging tools 💪 |
9 | 9 |
|
10 | | -## Platforms |
11 | | -Support for iOS, tvOS and macOS |
| 10 | +## Platforms |
| 11 | +Support for iOS 14.0+, tvOS 14.0+, watchOS 7.0+, and macOS 11.0+ |
12 | 12 |
|
13 | 13 | ## Support |
14 | 14 | For Swift 4 please use v1 |
15 | 15 |
|
16 | | -For Swift 5 please use v2+ |
| 16 | +For Swift 5 please use v2-v3 |
| 17 | + |
| 18 | +For Swift 5.5+ with OSLog please use v4+ |
17 | 19 |
|
18 | 20 | ## Installation |
19 | | -PrettyLogger is available through [Swift Package Manager](https://swift.org/package-manager/). |
| 21 | +PrettyLogger is available through [Swift Package Manager](https://swift.org/package-manager/). |
20 | 22 |
|
21 | 23 | 1. Follow Apple's [Adding Package Dependencies to Your App]( |
22 | 24 | https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app |
23 | 25 | ) guide on how to add a Swift Package dependency. |
24 | 26 | 2. Use `https://github.com/hyperdevs-team/PrettyLogger` as the repository URL. |
25 | | -3. Specify the version to be at least `3.0.0`. |
| 27 | +3. Specify the version to be at least `4.0.0`. |
26 | 28 |
|
27 | 29 | ## Usage |
28 | | -### Print messages |
29 | | -To print a message in the console you simply use any of the global functions: |
30 | | -```swift |
31 | | - logWarning("This a warning!!") |
32 | | - logError("This is showed as error") |
33 | | - logFatal("This is showed as fatal message") |
34 | | - logInfo("This is an info message") |
35 | | - logDebug("This is a debug message") |
36 | | - logTrace("This is a trace info") |
37 | | -``` |
38 | | -The previous example will print: |
39 | | -```ogdl |
40 | | -13:31:59.632 ◉ ⚠️⚠️⚠️ This a warning!! [File.swift:L109] |
41 | | -13:31:59.639 ◉ ❌❌❌ This is showed as error [File.swift:L110] |
42 | | -13:31:59.639 ◉ ☠️☠️☠️ This is showed as fatal message [File.swift:L111] |
43 | | -13:31:59.639 ◉ 🔍 This is an info message [File.swift:L112] |
44 | | -13:31:59.639 ◉ 🐛 This is a debug message [File.swift:L113] |
45 | | -13:31:59.640 ◉ ✏️ This is a trace info [File.swift:L114] |
46 | | -``` |
47 | | -### Level |
48 | | -You can silent all logs (or some, depending on level) by setting the property `level` on the shared instance: |
49 | | -```swift |
50 | | -PrettyLogger.shared.level = .all //To show all messages |
51 | | -PrettyLogger.shared.level = .disable //To silent logger |
52 | | -PrettyLogger.shared.level = .info //To show all message except debug & trace |
53 | | -``` |
54 | | -The available levels, in order, are: disable, fatal, error, warn, info, debug, trace & all |
55 | | -### Global framework |
56 | | -If you want to import all functions in your project without import PrettyLogger in every file you could use this directive in your AppDelegate: |
| 30 | + |
| 31 | +### Basic Logging |
| 32 | +To log messages, simply use any of the global functions: |
| 33 | +```swift |
| 34 | +logFatal("Critical error occurred") |
| 35 | +logError("Something went wrong") |
| 36 | +logWarning("This is a warning") |
| 37 | +logInfo("User logged in successfully") |
| 38 | +logDebug("Processing user data") |
| 39 | +logTrace("Entering function") |
| 40 | +``` |
| 41 | + |
| 42 | +### Logging with Categories |
| 43 | +Organize your logs with categories for better filtering and debugging: |
| 44 | +```swift |
| 45 | +logInfo("API request started", category: "Network") |
| 46 | +logError("Database connection failed", category: "Database") |
| 47 | +logDebug("Cache hit for user data", category: "Cache") |
| 48 | +logWarning("Low memory warning", category: "System") |
| 49 | +``` |
| 50 | + |
| 51 | +### Privacy Controls |
| 52 | +Control the visibility of sensitive information in your logs: |
| 53 | +```swift |
| 54 | +let userEmail = "user@example.com" |
| 55 | +let authToken = "abc123xyz" |
| 56 | + |
| 57 | +// Public information (visible in all log viewers) |
| 58 | +logInfo("App version 1.2.3", privacy: .public) |
| 59 | + |
| 60 | +// Private information (hidden in release builds) |
| 61 | +logDebug("User email: \(userEmail)", category: "Auth", privacy: .private) |
| 62 | +logTrace("Auth token: \(authToken)", category: "Auth", privacy: .private) |
| 63 | + |
| 64 | +// Auto privacy (system decides based on context) - Default behavior |
| 65 | +logInfo("User logged in successfully", privacy: .auto) |
| 66 | +``` |
| 67 | + |
| 68 | +### Complete Examples |
| 69 | +```swift |
| 70 | +// Basic usage |
| 71 | +logInfo("User authentication started") |
| 72 | + |
| 73 | +// With category |
| 74 | +logError("Network timeout occurred", category: "Network") |
| 75 | + |
| 76 | +// With privacy |
| 77 | +logDebug("Processing sensitive data", privacy: .private) |
| 78 | + |
| 79 | +// With both category and privacy |
| 80 | +logWarning("Failed login attempt", category: "Security", privacy: .private) |
| 81 | +``` |
| 82 | + |
| 83 | +> **Note**: Starting with version 4.0.0, PrettyLogger uses Apple's unified logging system (OSLog). This means logs are now visible not only in Xcode's console but also in system tools like Console.app, Instruments, and the command-line `log` tool, providing better integration with Apple's debugging ecosystem. |
| 84 | +
|
| 85 | +### Log Levels |
| 86 | +You can control which logs are shown by setting the level on the shared instance: |
| 87 | +```swift |
| 88 | +PrettyLogger.shared.level = .all // Show all messages |
| 89 | +PrettyLogger.shared.level = .disable // Disable all logging |
| 90 | +PrettyLogger.shared.level = .info // Show info and above (hide debug & trace) |
| 91 | +PrettyLogger.shared.level = .error // Show only error and fatal messages |
| 92 | +``` |
| 93 | + |
| 94 | +The available levels, in order from most restrictive to least restrictive, are: |
| 95 | +`disable`, `fatal`, `error`, `warn`, `info`, `debug`, `trace`, `all` |
| 96 | + |
| 97 | +### Real-time Log Monitoring |
| 98 | +You can subscribe to log outputs for real-time monitoring or custom handling: |
| 99 | +```swift |
| 100 | +import Combine |
| 101 | + |
| 102 | +var cancellables = Set<AnyCancellable>() |
| 103 | + |
| 104 | +PrettyLogger.shared.output |
| 105 | + .sink { logOutput in |
| 106 | + print("Received log: \(logOutput.level) - \(logOutput.message)") |
| 107 | + // Send to analytics, crash reporting, etc. |
| 108 | + } |
| 109 | + .store(in: &cancellables) |
| 110 | +``` |
| 111 | + |
| 112 | +### Viewing Logs in System Tools |
| 113 | + |
| 114 | +With version 4.0.0, you can view your app's logs in various Apple debugging tools: |
| 115 | + |
| 116 | +- **Xcode Console**: Shows logs during development and debugging |
| 117 | +- **Console.app**: System-wide log viewer with advanced filtering capabilities |
| 118 | +- **Instruments**: Correlate logs with performance data |
| 119 | +- **Command Line**: Use `log show --predicate 'subsystem == "com.yourapp.bundleid"'` |
| 120 | + |
| 121 | +### Global Framework Import |
| 122 | +If you want to use logging functions throughout your project without importing PrettyLogger in every file, add this to your AppDelegate: |
57 | 123 | ```swift |
58 | 124 | @_exported import PrettyLogger |
59 | 125 | ``` |
| 126 | + |
| 127 | +## Migration from v3 to v4 |
| 128 | + |
| 129 | +Version 4.0.0 introduces OSLog integration while maintaining the same familiar function names. The main changes are: |
| 130 | + |
| 131 | +- **Enhanced API**: New optional `category` and `privacy` parameters |
| 132 | +- **Better Performance**: Uses Apple's optimized logging system |
| 133 | +- **System Integration**: Logs appear in Console.app, Instruments, and other system tools |
| 134 | +- **Privacy Controls**: Built-in support for sensitive data handling |
| 135 | + |
| 136 | +Your existing code will continue to work with deprecation warnings guiding you to the new API: |
| 137 | + |
| 138 | +```swift |
| 139 | +// v3 style (still works, but deprecated) |
| 140 | +logInfo("User:", username, "logged in") |
| 141 | + |
| 142 | +// v4 style (recommended) |
| 143 | +logInfo("User: \(username) logged in", category: "Authentication") |
| 144 | +``` |
| 145 | + |
| 146 | +## Requirements |
| 147 | + |
| 148 | +- iOS 14.0+ / tvOS 14.0+ / watchOS 7.0+ / macOS 11.0+ |
| 149 | +- Swift 5.5+ |
| 150 | +- Xcode 13.0+ |
0 commit comments