diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f905119..32a58feb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +### [#1013] Enable more granular control over logging behavior + +Now the SDK allows for more fine-tuning of its logging behavior. The `LoggingPolicy` enum +provides for three options: `.default(OSLogger.LogLevel)` wherein the SDK will use its own logger, with the option +to customize the log level by passing an `OSLogger.LogLevel` to the enum case. +`custom` allows one to pass a custom `Logger` implementation for completely customized logging. +Lastly, `noLogging` disables logging entirely. + +To utilize this new configuration option, pass a `loggingPolicy` into the `Initializer`. If unspecified, the SDK +will utilize an internal `Logger` implementation with an `OSLogger.LogLevel` of `.debug` + # 0.21.0-beta New checkpoints diff --git a/Example/ZcashLightClientSample/ZcashLightClientSample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Example/ZcashLightClientSample/ZcashLightClientSample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index d3c26613..4474fbd8 100644 --- a/Example/ZcashLightClientSample/ZcashLightClientSample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Example/ZcashLightClientSample/ZcashLightClientSample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/grpc/grpc-swift.git", "state" : { - "revision" : "783ed8ddcde07ac0332a5ec4647b665f82e95b78", - "version" : "1.14.0" + "revision" : "130467153ff0acd642d2f098b69c1ac33373b24e", + "version" : "1.15.0" } }, { @@ -68,8 +68,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-atomics.git", "state" : { - "revision" : "ff3d2212b6b093db7f177d0855adbc4ef9c5f036", - "version" : "1.0.3" + "revision" : "6c89474e62719ddcc1e9614989fff2f68208fe10", + "version" : "1.1.0" } }, { @@ -104,8 +104,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-nio.git", "state" : { - "revision" : "45167b8006448c79dda4b7bd604e07a034c15c49", - "version" : "2.48.0" + "revision" : "d1690f85419fdac8d54e350fb6d2ab9fd95afd75", + "version" : "2.51.1" } }, { @@ -113,8 +113,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-nio-extras.git", "state" : { - "revision" : "f73ca5ee9c6806800243f1ac415fcf82de9a4c91", - "version" : "1.10.2" + "revision" : "0e0d0aab665ff1a0659ce75ac003081f2b1c8997", + "version" : "1.19.0" } }, { @@ -122,8 +122,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-nio-http2.git", "state" : { - "revision" : "22757ac305f3d44d2b99ba541193ff1d64e77d00", - "version" : "1.24.1" + "revision" : "6d021a48483dbb273a9be43f65234bdc9185b364", + "version" : "1.26.0" } }, { @@ -131,8 +131,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-nio-ssl.git", "state" : { - "revision" : "4fb7ead803e38949eb1d6fabb849206a72c580f3", - "version" : "2.23.0" + "revision" : "e866a626e105042a6a72a870c88b4c531ba05f83", + "version" : "2.24.0" } }, { @@ -140,8 +140,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-nio-transport-services.git", "state" : { - "revision" : "c0d9a144cfaec8d3d596aadde3039286a266c15c", - "version" : "1.15.0" + "revision" : "41f4098903878418537020075a4d8a6e20a0b182", + "version" : "1.17.0" } }, { @@ -149,8 +149,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-protobuf.git", "state" : { - "revision" : "ab3a58b7209a17d781c0d1dbb3e1ff3da306bae8", - "version" : "1.20.3" + "revision" : "0af9125c4eae12a4973fb66574c53a54962a9e1e", + "version" : "1.21.0" } }, { @@ -158,8 +158,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/zcash-hackworks/zcash-light-client-ffi", "state" : { - "revision" : "bf5992c2e53749ad11c1e85ad5d9c63e39bdf3cc", - "version" : "0.3.0" + "revision" : "75821e2b859600707318e4a788abbe27e6615833", + "version" : "0.3.1" } } ], diff --git a/Example/ZcashLightClientSample/ZcashLightClientSample/Sync Blocks/SyncBlocksListViewController.swift b/Example/ZcashLightClientSample/ZcashLightClientSample/Sync Blocks/SyncBlocksListViewController.swift index bf72037c..fdd523bf 100644 --- a/Example/ZcashLightClientSample/ZcashLightClientSample/Sync Blocks/SyncBlocksListViewController.swift +++ b/Example/ZcashLightClientSample/ZcashLightClientSample/Sync Blocks/SyncBlocksListViewController.swift @@ -114,7 +114,7 @@ class SyncBlocksListViewController: UIViewController { outputParamsURL: try! outputParamsURLHelper(), saplingParamsSourceURL: SaplingParamsSourceURL.default, alias: data.alias, - logLevel: .debug + loggingPolicy: .default(.debug) ) return SDKSynchronizer(initializer: initializer) diff --git a/README.md b/README.md index 80b9d4fd..f12803bc 100644 --- a/README.md +++ b/README.md @@ -71,9 +71,15 @@ public protocol Logger { } ``` -To enable logging you need to do 2 simple steps: -1. have one class conform the `Logger` protocol -2. inject that logger when creating the `Initializer` + +You have a few different options when it comes to logging: +1. Leave it to the SDK. It will use its own `Logger` with sensible defaults. For this option, simply omit the `loggingPolicy` parameter when creating the `Initializer` + +2. Provide a custom logger. For this option, do the following: + a). have one class conform to the `Logger` protocol + b). inject that logger when creating the `Initializer` by passing a `loggingPolicy` of `.custom(yourLogger)` + +3. No logging. The SDK will not log any events. For this option, pass a `loggingPolicy` of `.noLogging` when creating the `Initializer` For more details look the Sample App's `AppDelegate` code. diff --git a/Sources/ZcashLightClientKit/Initializer.swift b/Sources/ZcashLightClientKit/Initializer.swift index 65ce0f06..149b7801 100644 --- a/Sources/ZcashLightClientKit/Initializer.swift +++ b/Sources/ZcashLightClientKit/Initializer.swift @@ -98,6 +98,12 @@ public class Initializer { case success case seedRequired } + + public enum LoggingPolicy { + case `default`(OSLogger.LogLevel) + case custom(Logger) + case noLogging + } // This is used to uniquely identify instance of the SDKSynchronizer. It's used when checking if the Alias is already used or not. let id = UUID() @@ -152,7 +158,7 @@ public class Initializer { outputParamsURL: URL, saplingParamsSourceURL: SaplingParamsSourceURL, alias: ZcashSynchronizerAlias = .default, - logLevel: OSLogger.LogLevel = .debug + loggingPolicy: LoggingPolicy = .default(.debug) ) { let urls = URLs( fsBlockDbRoot: fsBlockDbRoot, @@ -166,7 +172,16 @@ public class Initializer { // from constructor. So `parsingError` is just stored in initializer and `SDKSynchronizer.prepare()` throw this error if it exists. let (updatedURLs, parsingError) = Self.tryToUpdateURLs(with: alias, urls: urls) - let logger = OSLogger(logLevel: logLevel, alias: alias) + let logger: Logger + switch loggingPolicy { + case let .default(logLevel): + logger = OSLogger(logLevel: logLevel, alias: alias) + case let .custom(customLogger): + logger = customLogger + case .noLogging: + logger = NullLogger() + } + let rustBackend = ZcashRustBackend( dbData: updatedURLs.dataDbURL, fsBlockDbRoot: updatedURLs.fsBlockDbRoot, diff --git a/Sources/ZcashLightClientKit/Utils/LoggingProxy.swift b/Sources/ZcashLightClientKit/Utils/LoggingProxy.swift index c919e068..a76a8c1c 100644 --- a/Sources/ZcashLightClientKit/Utils/LoggingProxy.swift +++ b/Sources/ZcashLightClientKit/Utils/LoggingProxy.swift @@ -43,3 +43,14 @@ extension Logger { error(message, file: file, function: function, line: line) } } + +/** +A concrete logger implementation that logs nothing at all + */ +struct NullLogger: Logger { + func debug(_ message: String, file: StaticString, function: StaticString, line: Int) {} + func info(_ message: String, file: StaticString, function: StaticString, line: Int) {} + func event(_ message: String, file: StaticString, function: StaticString, line: Int) {} + func warn(_ message: String, file: StaticString, function: StaticString, line: Int) {} + func error(_ message: String, file: StaticString, function: StaticString, line: Int) {} +} diff --git a/Tests/OfflineTests/InitializerOfflineTests.swift b/Tests/OfflineTests/InitializerOfflineTests.swift index 8a135843..b4fbd286 100644 --- a/Tests/OfflineTests/InitializerOfflineTests.swift +++ b/Tests/OfflineTests/InitializerOfflineTests.swift @@ -36,7 +36,7 @@ class InitializerOfflineTests: XCTestCase { outputParamsURL: outputParamsURL, saplingParamsSourceURL: .default, alias: alias, - logLevel: .debug + loggingPolicy: .default(.debug) ) } diff --git a/Tests/OfflineTests/SynchronizerOfflineTests.swift b/Tests/OfflineTests/SynchronizerOfflineTests.swift index fb5aab31..9f8ad130 100644 --- a/Tests/OfflineTests/SynchronizerOfflineTests.swift +++ b/Tests/OfflineTests/SynchronizerOfflineTests.swift @@ -249,7 +249,7 @@ class SynchronizerOfflineTests: XCTestCase { outputParamsURL: validFileURL, saplingParamsSourceURL: .default, alias: .default, - logLevel: .debug + loggingPolicy: .default(.debug) ) XCTAssertNotNil(initializer.urlsParsingError) @@ -290,7 +290,7 @@ class SynchronizerOfflineTests: XCTestCase { outputParamsURL: validFileURL, saplingParamsSourceURL: .default, alias: .default, - logLevel: .debug + loggingPolicy: .default(.debug) ) XCTAssertNotNil(initializer.urlsParsingError) diff --git a/Tests/PerformanceTests/SynchronizerTests.swift b/Tests/PerformanceTests/SynchronizerTests.swift index 18679127..4e6ac8d3 100644 --- a/Tests/PerformanceTests/SynchronizerTests.swift +++ b/Tests/PerformanceTests/SynchronizerTests.swift @@ -79,7 +79,7 @@ class SynchronizerTests: XCTestCase { outputParamsURL: try __outputParamsURL(), saplingParamsSourceURL: SaplingParamsSourceURL.tests, alias: .default, - logLevel: .debug + loggingPolicy: .default(.debug) ) try? FileManager.default.removeItem(at: databases.fsCacheDbRoot) diff --git a/Tests/TestUtils/TestCoordinator.swift b/Tests/TestUtils/TestCoordinator.swift index 00ad4d7e..8d2fcee7 100644 --- a/Tests/TestUtils/TestCoordinator.swift +++ b/Tests/TestUtils/TestCoordinator.swift @@ -77,7 +77,7 @@ class TestCoordinator { outputParamsURL: try __outputParamsURL(), saplingParamsSourceURL: SaplingParamsSourceURL.tests, alias: alias, - logLevel: .debug + loggingPolicy: .default(.debug) ) let derivationTool = DerivationTool(networkType: network.networkType)