# Getting Logs Source: https://www.nianticspatial.com/docs/nsdk/getting_logs/?platform=swift This guide explains how to access logs for your NSDK application on Kotlin and Swift platforms. ## Viewing Logs in Xcode Console For iOS applications using NSDK with Swift, you can view logs in **Xcode's Console** during development and debugging. To view logs: 1. Open your project in **Xcode** 2. Connect your iOS device or start the iOS Simulator 3. Run your application 4. Open the **Console** window (View -> Debug Area -> Activate Console, or press `⌘⇧Y`) 5. The console will display real-time logs from your application, including NSDK debug and error messages You can filter the console output by typing keywords in the filter box at the bottom of the console window. ## Log File Location In addition to the Xcode console, NSDK also writes logs to a file on the device: **Path:** `{App Files}/log.txt` The `{App Files}` directory refers to your application's documents directory. You can access this file by: - Using Xcode's **Window -> Devices and Simulators** -> Select your device -> Select your app -> Download Container - Programmatically accessing the file through your app's file system APIs - Using a file manager app on the device (if your app shares files or uses iCloud Drive) ## Custom Logging Callbacks You can implement a custom logging callback to reroute NSDK logs to your own logging system. This is useful for integrating NSDK logs with your existing logging infrastructure or sending logs to remote services. Create a class that conforms to the `NsdkLogCallback` protocol: ```swift import Foundation import NSDK import os.log class MyCustomLogCallback: NsdkLogCallback { private let logger = Logger(subsystem: "com.yourapp.nsdk", category: "NSDK") func onLog(level: NsdkLogLevel, message: String, fileName: String?, fileLine: Int, funcName: String?) { // Format the log message with location information var locationInfo = "" if let fileName = fileName, let funcName = funcName { locationInfo = "[\(fileName):\(fileLine)] \(funcName)" } else if let fileName = fileName { locationInfo = "[\(fileName):\(fileLine)]" } let logMessage = locationInfo.isEmpty ? message : "\(locationInfo): \(message)" // Route to your logging system switch level { case .all, .debug: logger.debug("[NSDK] [DEBUG] \(logMessage)") case .info: logger.info("[NSDK] [INFO] \(logMessage)") case .warn: logger.warning("[NSDK] [WARN] \(logMessage)") case .error: logger.error("[NSDK] [ERROR] \(logMessage)") case .off: break } } } ``` Pass your callback when creating the `NsdkSession`: ```swift // Create your custom callback let logCallback = MyCustomLogCallback() // Initialize session with callback let nsdk = NsdkSession(apiKey: "your-api-key", logCallback: logCallback) // Or with other initializers let nsdk = NsdkSession(withJson: configPath, logCallback: logCallback) ``` ## Configuring Log Levels You can control the verbosity of NSDK logs by setting log levels for different output destinations. Log levels follow a hierarchical structure where higher levels include all messages from lower levels. Available log levels: - `.all` - Logs all messages regardless of level (most verbose, useful for debugging) - `.debug` - Logs debug messages and all higher priority messages - `.info` - Logs informational messages and all higher priority messages - `.warn` - Logs warning messages and all higher priority messages - `.error` - Logs error messages and fatal messages only - `.off` - Disables all logging output Configure log levels on your `NsdkSession` instance: ```swift // Set log level for console output (Xcode console) nsdk.setStdoutLogLevel(logLevel: NsdkLogLevel.all) // Set log level for file output (log.txt) nsdk.setFileLogLevel(logLevel: NsdkLogLevel.all) // Set log level for callback output (if using custom callback) nsdk.setCallbackLogLevel(logLevel: NsdkLogLevel.all) ``` Example usage: ```swift // Initialize your NSDK session let nsdk = NsdkSession(apiKey: "your-api-key") // Enable verbose logging for debugging nsdk.setStdoutLogLevel(logLevel: .debug) nsdk.setFileLogLevel(logLevel: .debug) nsdk.setCallbackLogLevel(logLevel: .debug) // Or use different levels for different outputs nsdk.setStdoutLogLevel(logLevel: .info) // Less verbose in console nsdk.setFileLogLevel(logLevel: .all) // Full logs in file nsdk.setCallbackLogLevel(logLevel: .warn) // Only warnings and errors to callback // For production, reduce verbosity nsdk.setStdoutLogLevel(logLevel: .warn) nsdk.setFileLogLevel(logLevel: .error) nsdk.setCallbackLogLevel(logLevel: .error) ```