Skip to content

Commit 45d8cf1

Browse files
Dillon Nysdnys1
authored andcommitted
chore(core): Move logger to aws_common
Moves logging to `aws_common` and adds an `AWSLogger` class which can be used throughout core packages. commit-id:d64ce8ca
1 parent 811707a commit 45d8cf1

File tree

13 files changed

+471
-368
lines changed

13 files changed

+471
-368
lines changed

packages/amplify_core/lib/amplify_core.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ export 'src/hub/hub_event.dart';
4747

4848
// Logger
4949
export 'src/logger/amplify_logger.dart';
50-
export 'src/logger/log_entry.dart';
51-
export 'src/logger/log_level.dart';
52-
export 'src/logger/simple_printer.dart';
5350

5451
/// Plugin
5552
export 'src/plugin/amplify_analytics_plugin_interface.dart';

packages/amplify_core/lib/src/amplify_class.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ abstract class AmplifyClass {
103103
);
104104
}
105105

106-
AmplifyLogger().registerPlugin(const SimplePrinter());
107-
108106
await configurePlatform(configuration);
109107
_configCompleter.complete(amplifyConfig);
110108
}

packages/amplify_core/lib/src/logger/amplify_logger.dart

Lines changed: 28 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -12,113 +12,52 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import 'dart:async';
1615
import 'package:amplify_core/amplify_core.dart';
17-
import 'package:amplify_core/src/logger/logging_ext.dart';
1816
import 'package:logging/logging.dart';
1917
import 'package:meta/meta.dart';
2018

21-
/// {@template amplify_core.logger.amplify_logger}
22-
/// A logging utility providing the ability to emit log entries, configure the
23-
/// level at which entries are emitted, and register plugins which can handle
24-
/// log entries as they're emitted.
25-
/// {@endtemplate}
26-
///
27-
/// Plugins are created by implementing [AmplifyLoggerPlugin] and calling
28-
/// [AmplifyLogger.registerPlugin] on an [AmplifyLogger] instance.
29-
///
30-
/// By default, a [SimplePrinter] is registered on the root [AmplifyLogger]
31-
/// which impacts all category and child loggers.
32-
class AmplifyLogger {
33-
/// The root namespace for all [AmplifyLogger] instances.
34-
static const rootNamespace = 'AWS.Amplify';
35-
36-
static final Map<AmplifyLoggerPlugin, StreamSubscription<LogRecord>>
37-
_subscriptions = {};
38-
final Logger _logger;
39-
19+
/// {@macro aws_common.logging.aws_logger}
20+
class AmplifyLogger extends AWSLogger {
4021
/// Creates a top-level [AmplifyLogger].
4122
///
42-
/// {@macro amplify_core.logger.amplify_logger}
43-
factory AmplifyLogger([String namespace = rootNamespace]) =>
44-
AmplifyLogger._(Logger(namespace));
23+
/// {@macro aws_common.logging.aws_logger}
24+
factory AmplifyLogger([String namespace = rootNamespace]) {
25+
// Create a logger inside the Amplify hierarchy so that printing and log
26+
// level behavior are consistent with public API.
27+
//
28+
// Use AWSLogger to create a logger which can have its own hierarchy.
29+
if (!namespace.startsWith(rootNamespace)) {
30+
namespace = '$rootNamespace.$namespace';
31+
}
32+
return (AWSLogger.activeLoggers[namespace] ??=
33+
AmplifyLogger._(Logger(namespace))) as AmplifyLogger;
34+
}
4535

4636
/// Creates a [AmplifyLogger] for the Amplify [category].
4737
///
48-
/// {@macro amplify_core.logger.amplify_logger}
38+
/// {@macro aws_common.logging.aws_logger}
4939
factory AmplifyLogger.category(Category category) =>
50-
AmplifyLogger._(Logger('$rootNamespace.${category.name}'));
51-
52-
AmplifyLogger._(this._logger) {
53-
hierarchicalLoggingEnabled = true;
54-
}
55-
56-
/// Creates an [AmplifyLogger] with `this` as the parent.
57-
AmplifyLogger createChild(String name) =>
58-
AmplifyLogger('${_logger.fullName}.$name');
59-
60-
/// Registers an [AmplifyLoggerPlugin] to handle logs emitted by this logger
61-
/// instance.
62-
void registerPlugin(AmplifyLoggerPlugin plugin) {
63-
unregisterPlugin(plugin);
64-
_subscriptions[plugin] = _logger.onRecord.listen(
65-
(record) => plugin.handleLogEntry(record.toLogEntry()),
66-
);
67-
}
68-
69-
/// Unregisters [plugin] from this logger instance.
70-
void unregisterPlugin(AmplifyLoggerPlugin plugin) {
71-
final currentSubscription = _subscriptions.remove(plugin);
72-
if (currentSubscription != null) {
73-
unawaited(currentSubscription.cancel());
74-
}
75-
}
40+
AmplifyLogger().createChild(category.name);
7641

77-
/// Unregisters all [AmplifyLoggerPlugin]s on this logger instance.
78-
void unregisterAllPlugins() {
79-
for (final plugin in List.of(_subscriptions.keys)) {
80-
unregisterPlugin(plugin);
81-
}
82-
}
83-
84-
/// The minimum [LogLevel] that will be emitted by the logger.
85-
LogLevel get logLevel => _logger.level.logLevel;
86-
87-
set logLevel(LogLevel logLevel) {
88-
_logger.level = logLevel.level;
89-
}
90-
91-
/// Logs a message with level [LogLevel.verbose].
92-
void verbose(String message, [Object? error, StackTrace? stackTrace]) {
93-
_logger.finer(message, error, stackTrace);
94-
}
42+
AmplifyLogger._(Logger logger) : super.protected(logger);
9543

96-
/// Logs a message with level [LogLevel.debug].
97-
void debug(String message, [Object? error, StackTrace? stackTrace]) {
98-
_logger.fine(message, error, stackTrace);
99-
}
100-
101-
/// Logs a message with level [LogLevel.info].
102-
void info(String message, [Object? error, StackTrace? stackTrace]) {
103-
_logger.info(message, error, stackTrace);
104-
}
105-
106-
/// Logs a message with level [LogLevel.warn].
107-
void warn(String message, [Object? error, StackTrace? stackTrace]) {
108-
_logger.warning(message, error, stackTrace);
109-
}
44+
/// The root namespace for all [AmplifyLogger] instances.
45+
static const rootNamespace = '${AWSLogger.rootNamespace}.Amplify';
11046

111-
/// Logs a message with level [LogLevel.error].
112-
void error(String message, [Object? error, StackTrace? stackTrace]) {
113-
_logger.severe(message, error, stackTrace);
47+
@override
48+
AmplifyLogger createChild(String name) {
49+
assert(name.isNotEmpty, 'Name should not be empty');
50+
return AmplifyLogger('$namespace.$name');
11451
}
11552
}
11653

54+
/// {@template amplify_core.logger.amplify_logger_plugin}
11755
/// A plugin to an [AmplifyLogger] which handles log entries emitted at the
11856
/// [LogLevel] of the logger instance.
119-
abstract class AmplifyLoggerPlugin {
120-
/// Handles a log entry emitted by the [AmplifyLogger].
121-
void handleLogEntry(LogEntry logEntry);
57+
/// {@endtemplate}
58+
abstract class AmplifyLoggerPlugin extends AWSLoggerPlugin {
59+
/// {@macro amplify_core.logger.amplify_logger_plugin}
60+
const AmplifyLoggerPlugin();
12261
}
12362

12463
/// Mixin providing an [AmplifyLogger] to Amplify classes.

0 commit comments

Comments
 (0)