Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 2793b04

Browse files
committed
Add Logger.detached factory
Sometimes you just don't need to add a logger, which will be a singleton and part of the global loggers tree. So, just a logger, which will be garbage collected, and its onRecord.listen won't subscribe to the root one. Right now, there is theoretical possibility for that, but a constructor for that is private. Added Logger.detached factory, which is doing exactly this.
1 parent 4c2e9fd commit 2793b04

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
# Organization <fnmatch pattern>
88
#
99
Google Inc. <*@google.com>
10+
Anton Astashov <[email protected]>

lib/logging.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ class Logger {
6161
return _loggers.putIfAbsent(name, () => new Logger._named(name));
6262
}
6363

64+
/// Creates a new detached [Logger].
65+
///
66+
/// Returns a new [Logger] instance (unlike [new Logger], which returns a
67+
/// [Logger] singleton), which doesn't have any parent or children,
68+
/// and it's not a part of the global hierarchial loggers structure.
69+
///
70+
/// It can be useful when you just need a local short-living logger,
71+
/// which you'd like to be garbage-collected later.
72+
factory Logger.detached(String name) {
73+
return new Logger._internal(name, null, new Map<String, Logger>());
74+
}
75+
6476
factory Logger._named(String name) {
6577
if (name.startsWith('.')) {
6678
throw new ArgumentError("name shouldn't start with a '.'");

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: logging
2-
version: 0.11.1+1
2+
version: 0.11.2
33
author: Dart Team <[email protected]>
44
description: >
55
Provides APIs for debugging and error logging. This library introduces

test/logging_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,28 @@ void main() {
206206
});
207207
});
208208

209+
group('detached loggers', () {
210+
test("create new instances of Logger", () {
211+
Logger a1 = new Logger.detached("a");
212+
Logger a2 = new Logger.detached("a");
213+
Logger a = new Logger("a");
214+
215+
expect(a1, isNot(a2));
216+
expect(a1, isNot(a));
217+
expect(a2, isNot(a));
218+
});
219+
220+
test("parent is null", () {
221+
Logger a = new Logger.detached("a");
222+
expect(a.parent, null);
223+
});
224+
225+
test("children is empty", () {
226+
Logger a = new Logger.detached("a");
227+
expect(a.children, {});
228+
});
229+
});
230+
209231
group('mutating levels', () {
210232
Logger root = Logger.root;
211233
Logger a = new Logger('a');

0 commit comments

Comments
 (0)