-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logging): add a new logging category and plugin interface
- Loading branch information
Nika Hassani
committed
Nov 1, 2023
1 parent
448f372
commit 0fb1bbc
Showing
6 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/amplify_core/lib/src/category/amplify_logging_category.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
part of 'amplify_categories.dart'; | ||
|
||
/// {@template amplify_core.amplify_logging_category} | ||
/// The Amplify Logging category provides an interface for interacting with | ||
/// a logging plugin. | ||
/// | ||
/// It comes with default, built-in support for Amazon CloudWatch service | ||
/// leveraging Amplify Auth Category for authorization. | ||
/// {@endtemplate} | ||
class LoggingCategory extends AmplifyCategory<LoggingPluginInterface> { | ||
@override | ||
@nonVirtual | ||
Category get category => Category.logging; | ||
|
||
@override | ||
@nonVirtual | ||
Set<Category> get categoryDependencies => const {Category.auth}; | ||
|
||
P getPlugin<P extends LoggingPluginInterface>( | ||
LoggingPluginKey<P> pluginKey, | ||
) => | ||
plugins.singleWhere( | ||
(p) => p is P, | ||
orElse: () => throw PluginError( | ||
'No plugin registered for $pluginKey', | ||
), | ||
) as P; | ||
|
||
/// {@template amplify_core.amplify_logging_category.logger} | ||
/// Creates a logger for the specified namespace. If a [category] is provided, | ||
/// the logger namespace is prefixed with the `category.name`. | ||
/// {@endtemplate} | ||
Logger logger(String nameSpace, {Category? category}) { | ||
return defaultPlugin.logger(nameSpace); | ||
} | ||
|
||
/// {@template amplify_core.amplify_logging_category.enable} | ||
/// Starts recording logs emitted by [logger]. | ||
/// {@endtemplate} | ||
void enable() { | ||
return defaultPlugin.enable(); | ||
} | ||
|
||
/// {@template amplify_core.amplify_logging_category.disable} | ||
/// Stops recording logs emitted by [logger]. | ||
/// {@endtemplate} | ||
void disable() { | ||
return defaultPlugin.disable(); | ||
} | ||
|
||
/// {@template amplify_core.amplify_logging_category.flushLogs} | ||
/// Sends recorded logs to an output. | ||
/// {@endtemplate} | ||
void flushLogs() { | ||
return defaultPlugin.flushLogs(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/amplify_core/lib/src/plugin/amplify_logging_plugin_interface.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
library amplify_logging_plugin_interface; | ||
|
||
import 'package:amplify_core/amplify_core.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
/// Defines Amplify Logging plugin interface. | ||
/// | ||
/// {@macro amplify_core.amplify_logging_category} | ||
abstract class LoggingPluginInterface extends AmplifyPluginInterface { | ||
@override | ||
@nonVirtual | ||
Category get category => Category.logging; | ||
|
||
/// {@macro amplify_core.amplify_logging_category.logger} | ||
Logger logger(String namespace, {Category? category}) { | ||
throw UnimplementedError('logger has not been implemented'); | ||
} | ||
|
||
/// {@macro amplify_core.amplify_logging_category.enable} | ||
void enable() { | ||
throw UnimplementedError('enable has not been implemented'); | ||
} | ||
|
||
/// {@macro amplify_core.amplify_logging_category.disable} | ||
void disable() { | ||
throw UnimplementedError('disable has not been implemented'); | ||
} | ||
|
||
/// {@macro amplify_core.amplify_logging_category.flushLogs} | ||
void flushLogs() { | ||
throw UnimplementedError('flushLogs has not been implemented'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import 'package:aws_common/aws_common.dart'; | ||
|
||
/// {@template amplify_core.logging.logger} | ||
/// Logger interface for emitting log messages at different log levels. | ||
/// {@endtemplate} | ||
abstract interface class Logger { | ||
/// The namespace of this logger. | ||
String get namespace; | ||
|
||
/// Logs a [message] at the given [level]. | ||
void log( | ||
LogLevel level, | ||
String message, [ | ||
Object? error, | ||
StackTrace? stackTrace, | ||
]); | ||
|
||
/// Logs a [message] with level [LogLevel.verbose]. | ||
void verbose(String message, [Object? error, StackTrace? stackTrace]); | ||
|
||
/// Logs a [message] with level [LogLevel.debug]. | ||
void debug(String message, [Object? error, StackTrace? stackTrace]); | ||
|
||
/// Logs a [message] with level [LogLevel.info]. | ||
void info(String message, [Object? error, StackTrace? stackTrace]); | ||
|
||
/// Logs a [message] with level [LogLevel.warn]. | ||
void warn(String message, [Object? error, StackTrace? stackTrace]); | ||
|
||
/// Logs a [message] with level [LogLevel.error]. | ||
void error(String message, [Object? error, StackTrace? stackTrace]); | ||
} |