|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 |
| -import 'dart:async'; |
16 | 15 | import 'package:amplify_core/amplify_core.dart';
|
17 |
| -import 'package:amplify_core/src/logger/logging_ext.dart'; |
18 | 16 | import 'package:logging/logging.dart';
|
19 | 17 | import 'package:meta/meta.dart';
|
20 | 18 |
|
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 { |
40 | 21 | /// Creates a top-level [AmplifyLogger].
|
41 | 22 | ///
|
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 | + } |
45 | 35 |
|
46 | 36 | /// Creates a [AmplifyLogger] for the Amplify [category].
|
47 | 37 | ///
|
48 |
| - /// {@macro amplify_core.logger.amplify_logger} |
| 38 | + /// {@macro aws_common.logging.aws_logger} |
49 | 39 | 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); |
76 | 41 |
|
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); |
95 | 43 |
|
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'; |
110 | 46 |
|
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'); |
114 | 51 | }
|
115 | 52 | }
|
116 | 53 |
|
| 54 | +/// {@template amplify_core.logger.amplify_logger_plugin} |
117 | 55 | /// A plugin to an [AmplifyLogger] which handles log entries emitted at the
|
118 | 56 | /// [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(); |
122 | 61 | }
|
123 | 62 |
|
124 | 63 | /// Mixin providing an [AmplifyLogger] to Amplify classes.
|
|
0 commit comments