Skip to content

Commit

Permalink
Replace _rootLevel with root._level (flutter#69)
Browse files Browse the repository at this point in the history
Previously root._level and _rootLevel both existed and could be
different.  The existing code took care to always use _rootLevel
instead of root._level, but I think it'd be better to not have the
extra Level variable at all.

This also provides an opportunity to expose a public constant for the
the default logging level, further addressing
https://github.com/dart-lang/logging/issues/57.
  • Loading branch information
jamesderlin authored and natebosch committed Jan 10, 2020
1 parent 49669c4 commit 85f2bc2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/src/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ import 'log_record.dart';
/// Whether to allow fine-grain logging and configuration of loggers in a
/// hierarchy.
///
/// When false, all logging is merged in the root logger.
/// When false, all hierarchical logging instead is merged in the root logger.
bool hierarchicalLoggingEnabled = false;

/// Automatically record stack traces for any message of this level or above.
///
/// Because this is expensive, this is off by default.
Level recordStackTraceAtLevel = Level.OFF;

/// Level for the root-logger.
///
/// This will be the level of all loggers if [hierarchicalLoggingEnabled] is
/// false.
Level _rootLevel = Level.INFO;
/// The default [Level].
const defaultLevel = Level.INFO;

/// Use a [Logger] to log debug messages.
///
Expand Down Expand Up @@ -83,7 +80,9 @@ class Logger {
Logger._internal(this.name, this.parent, Map<String, Logger> children)
: _children = children,
children = UnmodifiableMapView(children) {
if (parent != null) parent._children[name] = this;
if (parent != null) {
parent._children[name] = this;
}
}

/// Effective level considering the levels established in this logger's
Expand All @@ -93,7 +92,7 @@ class Logger {
if (_level != null) return _level;
if (parent != null) return parent.level;
}
return _rootLevel;
return root._level;
}

/// Override the level for this particular [Logger] and its children.
Expand All @@ -106,7 +105,7 @@ class Logger {
'Please set "hierarchicalLoggingEnabled" to true if you want to '
'change the level on a non-root logger.');
}
_rootLevel = value;
root._level = value;
}
}

Expand Down Expand Up @@ -235,7 +234,7 @@ class Logger {
}

/// Top-level root [Logger].
static final Logger root = Logger('');
static final Logger root = Logger('').._level = defaultLevel;

/// All [Logger]s in the system.
static final Map<String, Logger> _loggers = <String, Logger>{};
Expand Down
6 changes: 6 additions & 0 deletions test/logging_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ void main() {
expect(() => Logger('.c'), throwsArgumentError);
});

test('root level has proper defaults', () {
expect(Logger.root, isNotNull);
expect(Logger.root.parent, null);
expect(Logger.root.level, defaultLevel);
});

test('logger naming is hierarchical', () {
var c = Logger('a.b.c');
expect(c.name, equals('c'));
Expand Down

0 comments on commit 85f2bc2

Please sign in to comment.