From 7d7275dbbd279c24a324ff87a902c45d1c3b82b0 Mon Sep 17 00:00:00 2001 From: Matt Straathof <11823378+bruuuuuuuce@users.noreply.github.com> Date: Tue, 19 Dec 2023 16:13:49 -0800 Subject: [PATCH] chore: add log level to prebuilt configurations (#33) --- README.md | 19 ++++++++++----- lib/client_sdk_dart.dart | 1 + lib/src/config/cache_configuration.dart | 29 ++++++++++++++++------- lib/src/config/cache_configurations.dart | 8 ++++--- lib/src/config/logger.dart | 30 ++++++++++++++++++++++++ lib/src/config/topic_configuration.dart | 29 ++++++++++++++++------- lib/src/config/topic_configurations.dart | 8 ++++--- lib/src/topic_client.dart | 2 ++ 8 files changed, 96 insertions(+), 30 deletions(-) create mode 100644 lib/src/config/logger.dart diff --git a/README.md b/README.md index 15b93e4..b97de40 100644 --- a/README.md +++ b/README.md @@ -47,13 +47,20 @@ const like = 'sample'; ## Logging -We use the [Dart logging package](https://github.com/dart-lang/logging) to create internal Loggers for producing Momento-related logs. The default logger does not do anything with the logs, so you must configure the logging level and handler at the beginning of your program: +There is a `LogLevel` enum that contains the following log levels: -``` -Logger.root.level = Level.ALL; // defaults to Level.INFO -Logger.root.onRecord.listen((record) { - print('${record.level.name}: ${record.time}: ${record.message}'); -}); +* `LogLevel.trace` +* `LogLevel.debug` +* `LogLevel.info` +* `LogLevel.warn` +* `LogLevel.error` +* `LogLevel.fatal` +* `LogLevel.off` + +This enum can be used to configure the logging level of the Momento package. By default, the logging level is set to `LogLevel.info`. To change the logging level, pass the desired level to the `Configuration` constructor: + +```dart +var config = Mobile.latest(logLevel: LogLevel.debug); ``` ## Additional information diff --git a/lib/client_sdk_dart.dart b/lib/client_sdk_dart.dart index 6c393d2..df856ea 100644 --- a/lib/client_sdk_dart.dart +++ b/lib/client_sdk_dart.dart @@ -4,5 +4,6 @@ library; export 'src/topic_client.dart'; +export 'src/config/logger.dart' show LogLevel; // TODO: Export any libraries intended for clients of this package. diff --git a/lib/src/config/cache_configuration.dart b/lib/src/config/cache_configuration.dart index 912dd67..6d03f8f 100644 --- a/lib/src/config/cache_configuration.dart +++ b/lib/src/config/cache_configuration.dart @@ -1,36 +1,47 @@ import 'package:client_sdk_dart/src/config/transport/grpc_configuration.dart'; import 'package:client_sdk_dart/src/config/transport/transport_strategy.dart'; +import '../../client_sdk_dart.dart'; + abstract interface class CacheConfiguration { /// Configures low-level options for network interactions with the Momento service late TransportStrategy transportStrategy; + /// Configures the verbosity of the client-side logger + LogLevel logLevel; + /// Constructor for a CacheConfiguration - CacheConfiguration(this.transportStrategy); + CacheConfiguration(this.transportStrategy, this.logLevel); /// Copy constructor for overriding TransportStrategy - CacheConfiguration withTransportStrategy(TransportStrategy transportStrategy); + CacheConfiguration withTransportStrategy(TransportStrategy transportStrategy, + {LogLevel logLevel = LogLevel.info}); /// Convenience copy constructor that updates the client-side /// timeout setting in the transport strategy - CacheConfiguration withDeadline(Duration deadline); + CacheConfiguration withDeadline(Duration deadline, + {LogLevel logLevel = LogLevel.info}); } class CacheClientConfiguration implements CacheConfiguration { @override late TransportStrategy transportStrategy; - CacheClientConfiguration(this.transportStrategy); + @override + LogLevel logLevel; + + CacheClientConfiguration(this.transportStrategy, this.logLevel); @override - CacheConfiguration withTransportStrategy( - TransportStrategy transportStrategy) { - return CacheClientConfiguration(transportStrategy); + CacheConfiguration withTransportStrategy(TransportStrategy transportStrategy, + {LogLevel logLevel = LogLevel.info}) { + return CacheClientConfiguration(transportStrategy, logLevel); } @override - CacheConfiguration withDeadline(Duration deadline) { + CacheConfiguration withDeadline(Duration deadline, + {LogLevel logLevel = LogLevel.info}) { return CacheClientConfiguration( - StaticTransportStrategy(StaticGrpcConfiguration(deadline))); + StaticTransportStrategy(StaticGrpcConfiguration(deadline)), logLevel); } } diff --git a/lib/src/config/cache_configurations.dart b/lib/src/config/cache_configurations.dart index 38d868b..af2e6a6 100644 --- a/lib/src/config/cache_configurations.dart +++ b/lib/src/config/cache_configurations.dart @@ -1,6 +1,7 @@ import 'package:client_sdk_dart/src/config/transport/grpc_configuration.dart'; import 'package:client_sdk_dart/src/config/transport/transport_strategy.dart'; +import '../../client_sdk_dart.dart'; import 'cache_configuration.dart'; /// Prebuilt configurations for Momento Cache clients @@ -12,8 +13,9 @@ class Mobile extends CacheClientConfigurations { return latest(); } - static CacheClientConfiguration latest() { - return CacheClientConfiguration(StaticTransportStrategy( - StaticGrpcConfiguration(Duration(seconds: 15)))); + static CacheClientConfiguration latest({LogLevel logLevel = LogLevel.info}) { + return CacheClientConfiguration( + StaticTransportStrategy(StaticGrpcConfiguration(Duration(seconds: 15))), + logLevel); } } diff --git a/lib/src/config/logger.dart b/lib/src/config/logger.dart new file mode 100644 index 0000000..2d897f3 --- /dev/null +++ b/lib/src/config/logger.dart @@ -0,0 +1,30 @@ +import 'package:logging/logging.dart'; + +enum LogLevel { + trace, + debug, + info, + warn, + error, + fatal, + off, +} + +Level determineLoggerLevel(LogLevel logLevel) { + switch (logLevel) { + case LogLevel.trace: + return Level.ALL; + case LogLevel.debug: + return Level.FINE; + case LogLevel.info: + return Level.INFO; + case LogLevel.warn: + return Level.WARNING; + case LogLevel.error: + return Level.SEVERE; + case LogLevel.fatal: + return Level.SHOUT; + case LogLevel.off: + return Level.OFF; + } +} diff --git a/lib/src/config/topic_configuration.dart b/lib/src/config/topic_configuration.dart index 9b9396a..c916216 100644 --- a/lib/src/config/topic_configuration.dart +++ b/lib/src/config/topic_configuration.dart @@ -1,3 +1,5 @@ +import 'package:client_sdk_dart/src/config/logger.dart'; + import 'transport/grpc_configuration.dart'; import 'transport/transport_strategy.dart'; @@ -5,15 +7,20 @@ abstract interface class TopicConfiguration { /// Configures low-level options for network interactions with the Momento service late TransportStrategy transportStrategy; + /// Configures the verbosity of the client-side logger + LogLevel logLevel; + /// Constructor for a TopicConfiguration - TopicConfiguration(this.transportStrategy); + TopicConfiguration(this.transportStrategy, this.logLevel); /// Copy constructor for overriding TransportStrategy - TopicConfiguration withTransportStrategy(TransportStrategy transportStrategy); + TopicConfiguration withTransportStrategy(TransportStrategy transportStrategy, + {LogLevel logLevel = LogLevel.info}); /// Convenience copy constructor that updates the client-side /// timeout setting in the transport strategy - TopicConfiguration withDeadline(Duration deadline); + TopicConfiguration withDeadline(Duration deadline, + {LogLevel logLevel = LogLevel.info}); } /// Configuration options for Momento TopicClient. @@ -23,17 +30,21 @@ class TopicClientConfiguration implements TopicConfiguration { @override late TransportStrategy transportStrategy; - TopicClientConfiguration(this.transportStrategy); + @override + LogLevel logLevel; + + TopicClientConfiguration(this.transportStrategy, this.logLevel); @override - TopicConfiguration withTransportStrategy( - TransportStrategy transportStrategy) { - return TopicClientConfiguration(transportStrategy); + TopicConfiguration withTransportStrategy(TransportStrategy transportStrategy, + {LogLevel logLevel = LogLevel.info}) { + return TopicClientConfiguration(transportStrategy, logLevel); } @override - TopicConfiguration withDeadline(Duration deadline) { + TopicConfiguration withDeadline(Duration deadline, + {LogLevel logLevel = LogLevel.info}) { return TopicClientConfiguration( - StaticTransportStrategy(StaticGrpcConfiguration(deadline))); + StaticTransportStrategy(StaticGrpcConfiguration(deadline)), logLevel); } } diff --git a/lib/src/config/topic_configurations.dart b/lib/src/config/topic_configurations.dart index 2a8fea0..39566a7 100644 --- a/lib/src/config/topic_configurations.dart +++ b/lib/src/config/topic_configurations.dart @@ -1,3 +1,4 @@ +import 'logger.dart'; import 'topic_configuration.dart'; import 'transport/grpc_configuration.dart'; import 'transport/transport_strategy.dart'; @@ -11,8 +12,9 @@ class Mobile extends TopicClientConfigurations { return latest(); } - static TopicClientConfiguration latest() { - return TopicClientConfiguration(StaticTransportStrategy( - StaticGrpcConfiguration(Duration(seconds: 15)))); + static TopicClientConfiguration latest({LogLevel logLevel = LogLevel.info}) { + return TopicClientConfiguration( + StaticTransportStrategy(StaticGrpcConfiguration(Duration(seconds: 15))), + logLevel); } } diff --git a/lib/src/topic_client.dart b/lib/src/topic_client.dart index ae2bcb0..439aba2 100644 --- a/lib/src/topic_client.dart +++ b/lib/src/topic_client.dart @@ -1,4 +1,5 @@ import 'package:client_sdk_dart/src/auth/credential_provider.dart'; +import 'package:client_sdk_dart/src/config/logger.dart'; import 'package:logging/logging.dart'; import 'config/topic_configuration.dart'; import 'internal/pubsub_client.dart'; @@ -20,6 +21,7 @@ class TopicClient implements ITopicClient { TopicClient( CredentialProvider credentialProvider, TopicConfiguration configuration) : _pubsubClient = ClientPubsub(credentialProvider, configuration) { + _logger.level = determineLoggerLevel(configuration.logLevel); _logger.finest("initializing topic client"); }