├── README.md └── ShareLogs ├── .DS_Store ├── ShareLogs.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ ├── IDEWorkspaceChecks.plist │ │ └── swiftpm │ │ │ └── Package.resolved │ └── xcuserdata │ │ └── nitinkumar.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── nitinkumar.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist └── ShareLogs ├── AppDelegate.swift ├── Assets.xcassets ├── AccentColor.colorset │ └── Contents.json ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── Logger └── Logger.swift └── ViewController.swift /README.md: -------------------------------------------------------------------------------- 1 | # Logging in Swift 2 | 3 | In this example, you can find how to print all the logs effciently in iOS application. Along with, you will find how to share logs for debuging purpose. 4 | 5 | By share logs, you can ask to your app user to share logs as a file so that you can debug any issue. 6 | 7 | ### Example: How to print logs 8 | 9 | You can print logs for different categories like error, info, warning, debug, verbose. 10 | 11 | ``` 12 | Log.p(#function, type: .info) 13 | Log.p("this is log to test log level: error", type: .error) 14 | Log.p("this is log to test log level: warn", type: .warn) 15 | Log.p("this is log to test log level: info", type: .info) 16 | Log.p("this is log to test log level: debug", type: .debug) 17 | Log.p("this is log to test log level: verbose", type: .verbose) 18 | ``` 19 | 20 | ### Example: How to share log file 21 | 22 | ``` 23 | if let logFilePath = Log.fileLogger.currentLogFileInfo?.filePath { 24 | DispatchQueue.main.async { 25 | let fileUrl = URL(fileURLWithPath: logFilePath) 26 | let activityViewController = UIActivityViewController(activityItems: [fileUrl], applicationActivities: nil) 27 | self.present(activityViewController, animated: true) { } 28 | } 29 | } 30 | ``` 31 | 32 | ### Example: How to log Networking request and response: 33 | While you deal with networking requests and responses in your application, you need to logs all the request and response. In Logger.swift file, you will find two seperate methods to do that. 34 | 35 | ``` 36 | Log.logStartRequest(request: URLRequest) // For request 37 | Log.logEndRequest(response: URLResponse>, data: Data?, error: Error?) // For response 38 | ``` 39 | 40 | 41 | 42 | I hope you find this repository useful. Do not forgot to follow me for more updates. 43 | 44 | ### Contact Me: 45 | 46 | Feel free to reach out if you have questions or if you want to contribute in any way: 47 | 48 | * E-mail: nitinagam17@gmail.com 49 | * Instagram: https://www.instagram.com/ios_geeks16/ 50 | * Medium: https://medium.com/@nitinagam17 51 | 52 | -------------------------------------------------------------------------------- /ShareLogs/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nitin-agam/swift_logger/cda3a6ce783f02ab00fd59896cf5f9c9e0877d2a/ShareLogs/.DS_Store -------------------------------------------------------------------------------- /ShareLogs/ShareLogs.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | C73EE6B72791C1C8001EC568 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C73EE6B62791C1C8001EC568 /* AppDelegate.swift */; }; 11 | C73EE6BB2791C1C8001EC568 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C73EE6BA2791C1C8001EC568 /* ViewController.swift */; }; 12 | C73EE6BE2791C1C8001EC568 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C73EE6BC2791C1C8001EC568 /* Main.storyboard */; }; 13 | C73EE6C02791C1CA001EC568 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C73EE6BF2791C1CA001EC568 /* Assets.xcassets */; }; 14 | C73EE6C32791C1CA001EC568 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C73EE6C12791C1CA001EC568 /* LaunchScreen.storyboard */; }; 15 | C73EE6CB2791C3D4001EC568 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = C73EE6CA2791C3D4001EC568 /* Logger.swift */; }; 16 | C73EE6CE2791C4AB001EC568 /* CocoaLumberjack in Frameworks */ = {isa = PBXBuildFile; productRef = C73EE6CD2791C4AB001EC568 /* CocoaLumberjack */; }; 17 | C73EE6D02791C4AB001EC568 /* CocoaLumberjackSwift in Frameworks */ = {isa = PBXBuildFile; productRef = C73EE6CF2791C4AB001EC568 /* CocoaLumberjackSwift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | C73EE6B32791C1C8001EC568 /* ShareLogs.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ShareLogs.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | C73EE6B62791C1C8001EC568 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 23 | C73EE6BA2791C1C8001EC568 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 24 | C73EE6BD2791C1C8001EC568 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 25 | C73EE6BF2791C1CA001EC568 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 26 | C73EE6C22791C1CA001EC568 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 27 | C73EE6C42791C1CA001EC568 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 28 | C73EE6CA2791C3D4001EC568 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | C73EE6B02791C1C8001EC568 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | C73EE6D02791C4AB001EC568 /* CocoaLumberjackSwift in Frameworks */, 37 | C73EE6CE2791C4AB001EC568 /* CocoaLumberjack in Frameworks */, 38 | ); 39 | runOnlyForDeploymentPostprocessing = 0; 40 | }; 41 | /* End PBXFrameworksBuildPhase section */ 42 | 43 | /* Begin PBXGroup section */ 44 | C73EE6AA2791C1C8001EC568 = { 45 | isa = PBXGroup; 46 | children = ( 47 | C73EE6B52791C1C8001EC568 /* ShareLogs */, 48 | C73EE6B42791C1C8001EC568 /* Products */, 49 | ); 50 | sourceTree = ""; 51 | }; 52 | C73EE6B42791C1C8001EC568 /* Products */ = { 53 | isa = PBXGroup; 54 | children = ( 55 | C73EE6B32791C1C8001EC568 /* ShareLogs.app */, 56 | ); 57 | name = Products; 58 | sourceTree = ""; 59 | }; 60 | C73EE6B52791C1C8001EC568 /* ShareLogs */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | C73EE6D12791C4BB001EC568 /* Logger */, 64 | C73EE6B62791C1C8001EC568 /* AppDelegate.swift */, 65 | C73EE6BA2791C1C8001EC568 /* ViewController.swift */, 66 | C73EE6BC2791C1C8001EC568 /* Main.storyboard */, 67 | C73EE6BF2791C1CA001EC568 /* Assets.xcassets */, 68 | C73EE6C12791C1CA001EC568 /* LaunchScreen.storyboard */, 69 | C73EE6C42791C1CA001EC568 /* Info.plist */, 70 | ); 71 | path = ShareLogs; 72 | sourceTree = ""; 73 | }; 74 | C73EE6D12791C4BB001EC568 /* Logger */ = { 75 | isa = PBXGroup; 76 | children = ( 77 | C73EE6CA2791C3D4001EC568 /* Logger.swift */, 78 | ); 79 | path = Logger; 80 | sourceTree = ""; 81 | }; 82 | /* End PBXGroup section */ 83 | 84 | /* Begin PBXNativeTarget section */ 85 | C73EE6B22791C1C8001EC568 /* ShareLogs */ = { 86 | isa = PBXNativeTarget; 87 | buildConfigurationList = C73EE6C72791C1CA001EC568 /* Build configuration list for PBXNativeTarget "ShareLogs" */; 88 | buildPhases = ( 89 | C73EE6AF2791C1C8001EC568 /* Sources */, 90 | C73EE6B02791C1C8001EC568 /* Frameworks */, 91 | C73EE6B12791C1C8001EC568 /* Resources */, 92 | ); 93 | buildRules = ( 94 | ); 95 | dependencies = ( 96 | ); 97 | name = ShareLogs; 98 | packageProductDependencies = ( 99 | C73EE6CD2791C4AB001EC568 /* CocoaLumberjack */, 100 | C73EE6CF2791C4AB001EC568 /* CocoaLumberjackSwift */, 101 | ); 102 | productName = ShareLogs; 103 | productReference = C73EE6B32791C1C8001EC568 /* ShareLogs.app */; 104 | productType = "com.apple.product-type.application"; 105 | }; 106 | /* End PBXNativeTarget section */ 107 | 108 | /* Begin PBXProject section */ 109 | C73EE6AB2791C1C8001EC568 /* Project object */ = { 110 | isa = PBXProject; 111 | attributes = { 112 | BuildIndependentTargetsInParallel = 1; 113 | LastSwiftUpdateCheck = 1320; 114 | LastUpgradeCheck = 1320; 115 | TargetAttributes = { 116 | C73EE6B22791C1C8001EC568 = { 117 | CreatedOnToolsVersion = 13.2.1; 118 | }; 119 | }; 120 | }; 121 | buildConfigurationList = C73EE6AE2791C1C8001EC568 /* Build configuration list for PBXProject "ShareLogs" */; 122 | compatibilityVersion = "Xcode 13.0"; 123 | developmentRegion = en; 124 | hasScannedForEncodings = 0; 125 | knownRegions = ( 126 | en, 127 | Base, 128 | ); 129 | mainGroup = C73EE6AA2791C1C8001EC568; 130 | packageReferences = ( 131 | C73EE6CC2791C4AB001EC568 /* XCRemoteSwiftPackageReference "CocoaLumberjack" */, 132 | ); 133 | productRefGroup = C73EE6B42791C1C8001EC568 /* Products */; 134 | projectDirPath = ""; 135 | projectRoot = ""; 136 | targets = ( 137 | C73EE6B22791C1C8001EC568 /* ShareLogs */, 138 | ); 139 | }; 140 | /* End PBXProject section */ 141 | 142 | /* Begin PBXResourcesBuildPhase section */ 143 | C73EE6B12791C1C8001EC568 /* Resources */ = { 144 | isa = PBXResourcesBuildPhase; 145 | buildActionMask = 2147483647; 146 | files = ( 147 | C73EE6C32791C1CA001EC568 /* LaunchScreen.storyboard in Resources */, 148 | C73EE6C02791C1CA001EC568 /* Assets.xcassets in Resources */, 149 | C73EE6BE2791C1C8001EC568 /* Main.storyboard in Resources */, 150 | ); 151 | runOnlyForDeploymentPostprocessing = 0; 152 | }; 153 | /* End PBXResourcesBuildPhase section */ 154 | 155 | /* Begin PBXSourcesBuildPhase section */ 156 | C73EE6AF2791C1C8001EC568 /* Sources */ = { 157 | isa = PBXSourcesBuildPhase; 158 | buildActionMask = 2147483647; 159 | files = ( 160 | C73EE6BB2791C1C8001EC568 /* ViewController.swift in Sources */, 161 | C73EE6CB2791C3D4001EC568 /* Logger.swift in Sources */, 162 | C73EE6B72791C1C8001EC568 /* AppDelegate.swift in Sources */, 163 | ); 164 | runOnlyForDeploymentPostprocessing = 0; 165 | }; 166 | /* End PBXSourcesBuildPhase section */ 167 | 168 | /* Begin PBXVariantGroup section */ 169 | C73EE6BC2791C1C8001EC568 /* Main.storyboard */ = { 170 | isa = PBXVariantGroup; 171 | children = ( 172 | C73EE6BD2791C1C8001EC568 /* Base */, 173 | ); 174 | name = Main.storyboard; 175 | sourceTree = ""; 176 | }; 177 | C73EE6C12791C1CA001EC568 /* LaunchScreen.storyboard */ = { 178 | isa = PBXVariantGroup; 179 | children = ( 180 | C73EE6C22791C1CA001EC568 /* Base */, 181 | ); 182 | name = LaunchScreen.storyboard; 183 | sourceTree = ""; 184 | }; 185 | /* End PBXVariantGroup section */ 186 | 187 | /* Begin XCBuildConfiguration section */ 188 | C73EE6C52791C1CA001EC568 /* Debug */ = { 189 | isa = XCBuildConfiguration; 190 | buildSettings = { 191 | ALWAYS_SEARCH_USER_PATHS = NO; 192 | CLANG_ANALYZER_NONNULL = YES; 193 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 194 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 195 | CLANG_CXX_LIBRARY = "libc++"; 196 | CLANG_ENABLE_MODULES = YES; 197 | CLANG_ENABLE_OBJC_ARC = YES; 198 | CLANG_ENABLE_OBJC_WEAK = YES; 199 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 200 | CLANG_WARN_BOOL_CONVERSION = YES; 201 | CLANG_WARN_COMMA = YES; 202 | CLANG_WARN_CONSTANT_CONVERSION = YES; 203 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 204 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 205 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 206 | CLANG_WARN_EMPTY_BODY = YES; 207 | CLANG_WARN_ENUM_CONVERSION = YES; 208 | CLANG_WARN_INFINITE_RECURSION = YES; 209 | CLANG_WARN_INT_CONVERSION = YES; 210 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 211 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 212 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 213 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 214 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 215 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 216 | CLANG_WARN_STRICT_PROTOTYPES = YES; 217 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 218 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 219 | CLANG_WARN_UNREACHABLE_CODE = YES; 220 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 221 | COPY_PHASE_STRIP = NO; 222 | DEBUG_INFORMATION_FORMAT = dwarf; 223 | ENABLE_STRICT_OBJC_MSGSEND = YES; 224 | ENABLE_TESTABILITY = YES; 225 | GCC_C_LANGUAGE_STANDARD = gnu11; 226 | GCC_DYNAMIC_NO_PIC = NO; 227 | GCC_NO_COMMON_BLOCKS = YES; 228 | GCC_OPTIMIZATION_LEVEL = 0; 229 | GCC_PREPROCESSOR_DEFINITIONS = ( 230 | "DEBUG=1", 231 | "$(inherited)", 232 | ); 233 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 234 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 235 | GCC_WARN_UNDECLARED_SELECTOR = YES; 236 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 237 | GCC_WARN_UNUSED_FUNCTION = YES; 238 | GCC_WARN_UNUSED_VARIABLE = YES; 239 | IPHONEOS_DEPLOYMENT_TARGET = 15.2; 240 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 241 | MTL_FAST_MATH = YES; 242 | ONLY_ACTIVE_ARCH = YES; 243 | SDKROOT = iphoneos; 244 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 245 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 246 | }; 247 | name = Debug; 248 | }; 249 | C73EE6C62791C1CA001EC568 /* Release */ = { 250 | isa = XCBuildConfiguration; 251 | buildSettings = { 252 | ALWAYS_SEARCH_USER_PATHS = NO; 253 | CLANG_ANALYZER_NONNULL = YES; 254 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 255 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 256 | CLANG_CXX_LIBRARY = "libc++"; 257 | CLANG_ENABLE_MODULES = YES; 258 | CLANG_ENABLE_OBJC_ARC = YES; 259 | CLANG_ENABLE_OBJC_WEAK = YES; 260 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 261 | CLANG_WARN_BOOL_CONVERSION = YES; 262 | CLANG_WARN_COMMA = YES; 263 | CLANG_WARN_CONSTANT_CONVERSION = YES; 264 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 265 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 266 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 267 | CLANG_WARN_EMPTY_BODY = YES; 268 | CLANG_WARN_ENUM_CONVERSION = YES; 269 | CLANG_WARN_INFINITE_RECURSION = YES; 270 | CLANG_WARN_INT_CONVERSION = YES; 271 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 272 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 273 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 274 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 275 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 276 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 277 | CLANG_WARN_STRICT_PROTOTYPES = YES; 278 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 279 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 280 | CLANG_WARN_UNREACHABLE_CODE = YES; 281 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 282 | COPY_PHASE_STRIP = NO; 283 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 284 | ENABLE_NS_ASSERTIONS = NO; 285 | ENABLE_STRICT_OBJC_MSGSEND = YES; 286 | GCC_C_LANGUAGE_STANDARD = gnu11; 287 | GCC_NO_COMMON_BLOCKS = YES; 288 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 289 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 290 | GCC_WARN_UNDECLARED_SELECTOR = YES; 291 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 292 | GCC_WARN_UNUSED_FUNCTION = YES; 293 | GCC_WARN_UNUSED_VARIABLE = YES; 294 | IPHONEOS_DEPLOYMENT_TARGET = 15.2; 295 | MTL_ENABLE_DEBUG_INFO = NO; 296 | MTL_FAST_MATH = YES; 297 | SDKROOT = iphoneos; 298 | SWIFT_COMPILATION_MODE = wholemodule; 299 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 300 | VALIDATE_PRODUCT = YES; 301 | }; 302 | name = Release; 303 | }; 304 | C73EE6C82791C1CA001EC568 /* Debug */ = { 305 | isa = XCBuildConfiguration; 306 | buildSettings = { 307 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 308 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 309 | CODE_SIGN_STYLE = Manual; 310 | CURRENT_PROJECT_VERSION = 1; 311 | DEVELOPMENT_TEAM = ""; 312 | GENERATE_INFOPLIST_FILE = YES; 313 | INFOPLIST_FILE = ShareLogs/Info.plist; 314 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 315 | INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; 316 | INFOPLIST_KEY_UIMainStoryboardFile = Main; 317 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 318 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 319 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 320 | LD_RUNPATH_SEARCH_PATHS = ( 321 | "$(inherited)", 322 | "@executable_path/Frameworks", 323 | ); 324 | MARKETING_VERSION = 1.0; 325 | PRODUCT_BUNDLE_IDENTIFIER = com.app.sharelogs; 326 | PRODUCT_NAME = "$(TARGET_NAME)"; 327 | PROVISIONING_PROFILE_SPECIFIER = ""; 328 | SWIFT_EMIT_LOC_STRINGS = YES; 329 | SWIFT_VERSION = 5.0; 330 | TARGETED_DEVICE_FAMILY = 1; 331 | }; 332 | name = Debug; 333 | }; 334 | C73EE6C92791C1CA001EC568 /* Release */ = { 335 | isa = XCBuildConfiguration; 336 | buildSettings = { 337 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 338 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 339 | CODE_SIGN_STYLE = Manual; 340 | CURRENT_PROJECT_VERSION = 1; 341 | DEVELOPMENT_TEAM = ""; 342 | GENERATE_INFOPLIST_FILE = YES; 343 | INFOPLIST_FILE = ShareLogs/Info.plist; 344 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 345 | INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; 346 | INFOPLIST_KEY_UIMainStoryboardFile = Main; 347 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 348 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 349 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 350 | LD_RUNPATH_SEARCH_PATHS = ( 351 | "$(inherited)", 352 | "@executable_path/Frameworks", 353 | ); 354 | MARKETING_VERSION = 1.0; 355 | PRODUCT_BUNDLE_IDENTIFIER = com.app.sharelogs; 356 | PRODUCT_NAME = "$(TARGET_NAME)"; 357 | PROVISIONING_PROFILE_SPECIFIER = ""; 358 | SWIFT_EMIT_LOC_STRINGS = YES; 359 | SWIFT_VERSION = 5.0; 360 | TARGETED_DEVICE_FAMILY = 1; 361 | }; 362 | name = Release; 363 | }; 364 | /* End XCBuildConfiguration section */ 365 | 366 | /* Begin XCConfigurationList section */ 367 | C73EE6AE2791C1C8001EC568 /* Build configuration list for PBXProject "ShareLogs" */ = { 368 | isa = XCConfigurationList; 369 | buildConfigurations = ( 370 | C73EE6C52791C1CA001EC568 /* Debug */, 371 | C73EE6C62791C1CA001EC568 /* Release */, 372 | ); 373 | defaultConfigurationIsVisible = 0; 374 | defaultConfigurationName = Release; 375 | }; 376 | C73EE6C72791C1CA001EC568 /* Build configuration list for PBXNativeTarget "ShareLogs" */ = { 377 | isa = XCConfigurationList; 378 | buildConfigurations = ( 379 | C73EE6C82791C1CA001EC568 /* Debug */, 380 | C73EE6C92791C1CA001EC568 /* Release */, 381 | ); 382 | defaultConfigurationIsVisible = 0; 383 | defaultConfigurationName = Release; 384 | }; 385 | /* End XCConfigurationList section */ 386 | 387 | /* Begin XCRemoteSwiftPackageReference section */ 388 | C73EE6CC2791C4AB001EC568 /* XCRemoteSwiftPackageReference "CocoaLumberjack" */ = { 389 | isa = XCRemoteSwiftPackageReference; 390 | repositoryURL = "https://github.com/CocoaLumberjack/CocoaLumberjack.git"; 391 | requirement = { 392 | kind = upToNextMajorVersion; 393 | minimumVersion = 3.0.0; 394 | }; 395 | }; 396 | /* End XCRemoteSwiftPackageReference section */ 397 | 398 | /* Begin XCSwiftPackageProductDependency section */ 399 | C73EE6CD2791C4AB001EC568 /* CocoaLumberjack */ = { 400 | isa = XCSwiftPackageProductDependency; 401 | package = C73EE6CC2791C4AB001EC568 /* XCRemoteSwiftPackageReference "CocoaLumberjack" */; 402 | productName = CocoaLumberjack; 403 | }; 404 | C73EE6CF2791C4AB001EC568 /* CocoaLumberjackSwift */ = { 405 | isa = XCSwiftPackageProductDependency; 406 | package = C73EE6CC2791C4AB001EC568 /* XCRemoteSwiftPackageReference "CocoaLumberjack" */; 407 | productName = CocoaLumberjackSwift; 408 | }; 409 | /* End XCSwiftPackageProductDependency section */ 410 | }; 411 | rootObject = C73EE6AB2791C1C8001EC568 /* Project object */; 412 | } 413 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved: -------------------------------------------------------------------------------- 1 | { 2 | "object": { 3 | "pins": [ 4 | { 5 | "package": "CocoaLumberjack", 6 | "repositoryURL": "https://github.com/CocoaLumberjack/CocoaLumberjack.git", 7 | "state": { 8 | "branch": null, 9 | "revision": "80ada1f753b0d53d9b57c465936a7c4169375002", 10 | "version": "3.7.4" 11 | } 12 | }, 13 | { 14 | "package": "swift-log", 15 | "repositoryURL": "https://github.com/apple/swift-log.git", 16 | "state": { 17 | "branch": null, 18 | "revision": "5d66f7ba25daf4f94100e7022febf3c75e37a6c7", 19 | "version": "1.4.2" 20 | } 21 | } 22 | ] 23 | }, 24 | "version": 1 25 | } 26 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs.xcodeproj/project.xcworkspace/xcuserdata/nitinkumar.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nitin-agam/swift_logger/cda3a6ce783f02ab00fd59896cf5f9c9e0877d2a/ShareLogs/ShareLogs.xcodeproj/project.xcworkspace/xcuserdata/nitinkumar.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /ShareLogs/ShareLogs.xcodeproj/xcuserdata/nitinkumar.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | ShareLogs.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // ShareLogs 4 | // 5 | // Created by Nitin Aggarwal on 14/01/22. 6 | // 7 | 8 | import UIKit 9 | 10 | @main 11 | class AppDelegate: UIResponder, UIApplicationDelegate { 12 | 13 | var window: UIWindow? 14 | 15 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 16 | return true 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | UIApplicationSceneManifest 6 | 7 | UIApplicationSupportsMultipleScenes 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs/Logger/Logger.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Logger.swift 3 | // ShareLogs 4 | // 5 | // Created by Nitin Aggarwal on 14/01/22. 6 | // 7 | 8 | import UIKit 9 | import CocoaLumberjackSwift 10 | 11 | var Log: Logger { Logger.shared } 12 | 13 | class Logger: NSObject { 14 | 15 | // MARK: - Properties 16 | static let shared = Logger() 17 | private static let logEnabled = true 18 | 19 | enum LogLevel: String { 20 | case verbose = "[VERBOSE 📋]" 21 | case info = "[INFO ℹ️]" 22 | case warn = "[WARN ⚠️]" 23 | case error = "[ERROR 🔴]" 24 | case debug = "[DEBUG 🐛]" 25 | } 26 | 27 | let fileLogger: DDFileLogger = { 28 | let logger = DDFileLogger() 29 | logger.rollingFrequency = 60 * 60 * 24 // 24 hours 30 | logger.logFileManager.maximumNumberOfLogFiles = 7 31 | logger.logFormatter = LogFormatter.shared 32 | logger.maximumFileSize = 1024 * 1024 // 1 MB 33 | return logger 34 | }() 35 | 36 | 37 | // MARK: - Initializer 38 | override init() { 39 | super.init() 40 | 41 | // Add OS Logger 42 | DDOSLogger.sharedInstance.logFormatter = LogFormatter() 43 | DDLog.add(DDOSLogger.sharedInstance) 44 | 45 | /* 46 | These tie into the log level just as you would expect 47 | If you set the log level to DDLogLevelError, then you will only see Error statements. 48 | If you set the log level to DDLogLevelWarn, then you will only see Error and Warn statements. 49 | If you set the log level to DDLogLevelInfo, you'll see Error, Warn and Info statements. 50 | If you set the log level to DDLogLevelDebug, you'll see Error, Warn, Info and Debug statements. 51 | If you set the log level to DDLogLevelVerbose, you'll see all DDLog statements. 52 | If you set the log level to DDLogLevelOff, you won't see any DDLog statements. 53 | ref: https://github.com/CocoaLumberjack/CocoaLumberjack/blob/master/Documentation/GettingStarted.md 54 | */ 55 | DDLog.add(fileLogger, with: .info) 56 | } 57 | 58 | 59 | // MARK: - Methods 60 | func p(_ object: Any, type: LogLevel = .info, file: String = #file, function: String = #function, line: Int = #line) { 61 | if Logger.logEnabled { 62 | let loggedMessage = "[\((file as NSString).lastPathComponent)(\(function): \(line)] \(type.rawValue) \(object)" 63 | switch type { 64 | case .info: DDLogInfo(loggedMessage) 65 | case .error: DDLogError(loggedMessage) 66 | case .verbose: DDLogVerbose(loggedMessage) 67 | case .warn: DDLogWarn(loggedMessage) 68 | case .debug: DDLogDebug(loggedMessage) 69 | } 70 | } 71 | } 72 | } 73 | 74 | // MARK: - Networking Logs 75 | extension Logger { 76 | 77 | func logStartRequest(_ request: URLRequest) { 78 | let body = request.httpBody.map { String(decoding: $0, as: UTF8.self) } ?? "Nil" 79 | let requestUrl = request.url?.absoluteString ?? "Nil" 80 | let networkRequest = """ 81 | ⚡️⚡️⚡️⚡️ REQUEST ⚡️⚡️⚡️⚡️ 82 | ⚡️⚡️⚡️⚡️ URL -> \(requestUrl) 83 | ⚡️⚡️⚡️⚡️ METHOD -> \(String(describing: request.httpMethod)) 84 | ⚡️⚡️⚡️⚡️ BODY -> \(body) 85 | ⚡️⚡️⚡️⚡️ HEADERS -> \(String(describing: request.allHTTPHeaderFields)) 86 | ⚡️⚡️⚡️⚡️ ---------------------- ⚡️⚡️⚡️⚡️ 87 | """ 88 | Log.p(networkRequest, type: .info) 89 | } 90 | 91 | func logEndRequest(_ response: URLResponse?, data: Data?, error: Error?) { 92 | var statusCode = 0 93 | if let httpUrlResponse = response as? HTTPURLResponse { 94 | statusCode = httpUrlResponse.statusCode 95 | } 96 | 97 | let networkResponse = """ 98 | ⚡️⚡️⚡️⚡️ RESPONSE ⚡️⚡️⚡️⚡️ 99 | ⚡️⚡️⚡️⚡️ URL -> \(response?.url?.absoluteString ?? "NIL") 100 | ⚡️⚡️⚡️⚡️ STATUS CODE -> \(statusCode) 101 | ⚡️⚡️⚡️⚡️ DATA -> \(data?.dictionary ?? [:]) 102 | ⚡️⚡️⚡️⚡️ ERROR -> \(String(describing: error)) 103 | ⚡️⚡️⚡️⚡️ ---------------------- ⚡️⚡️⚡️⚡️ 104 | """ 105 | Log.p(networkResponse, type: .info) 106 | } 107 | } 108 | 109 | 110 | // MARK: - Supported Class 111 | class LogFormatter: NSObject, DDLogFormatter { 112 | 113 | static let shared = LogFormatter() 114 | 115 | func format(message logMessage: DDLogMessage) -> String? { 116 | logMessage.message 117 | } 118 | } 119 | 120 | extension Data { 121 | 122 | var dictionary: [String: Any]? { 123 | do { 124 | let responseData = try JSONSerialization.jsonObject(with: self, options: .allowFragments) as? [String: Any] 125 | return responseData 126 | } catch { 127 | return nil 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /ShareLogs/ShareLogs/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // ShareLogs 4 | // 5 | // Created by Nitin Aggarwal on 14/01/22. 6 | // 7 | 8 | import UIKit 9 | 10 | class ViewController: UIViewController { 11 | 12 | override func viewDidLoad() { 13 | super.viewDidLoad() 14 | 15 | // adding some dummy logs to check in sharable log file. 16 | logDummyValues() 17 | } 18 | 19 | private func logDummyValues() { 20 | Log.p(#function, type: .info) 21 | Log.p("this is log to test log level: error", type: .error) 22 | Log.p("this is log to test log level: warn", type: .warn) 23 | Log.p("this is log to test log level: info", type: .info) 24 | Log.p("this is log to test log level: debug", type: .debug) 25 | Log.p("this is log to test log level: verbose", type: .verbose) 26 | } 27 | 28 | @IBAction func handleShareLogClicked(_ sender: UIButton) { 29 | if let logFilePath = Log.fileLogger.currentLogFileInfo?.filePath { 30 | DispatchQueue.main.async { 31 | self.shareItem(item: URL(fileURLWithPath: logFilePath)) 32 | } 33 | } 34 | } 35 | } 36 | 37 | extension ViewController { 38 | 39 | func shareItem(item: Any) { 40 | let activityViewController = UIActivityViewController(activityItems: [item], applicationActivities: nil) 41 | self.present(activityViewController, animated: true) { } 42 | } 43 | } 44 | --------------------------------------------------------------------------------