Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add log level to prebuilt configurations #33

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/client_sdk_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
29 changes: 20 additions & 9 deletions lib/src/config/cache_configuration.dart
Original file line number Diff line number Diff line change
@@ -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);
}
}
8 changes: 5 additions & 3 deletions lib/src/config/cache_configurations.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}
30 changes: 30 additions & 0 deletions lib/src/config/logger.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}
29 changes: 20 additions & 9 deletions lib/src/config/topic_configuration.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import 'package:client_sdk_dart/src/config/logger.dart';

import 'transport/grpc_configuration.dart';
import 'transport/transport_strategy.dart';

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.
Expand All @@ -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);
}
}
8 changes: 5 additions & 3 deletions lib/src/config/topic_configurations.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'logger.dart';
import 'topic_configuration.dart';
import 'transport/grpc_configuration.dart';
import 'transport/transport_strategy.dart';
Expand All @@ -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);
}
}
2 changes: 2 additions & 0 deletions lib/src/topic_client.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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");
}

Expand Down