Skip to content

Commit

Permalink
Moved level from LogFilter to Logger, closes #38
Browse files Browse the repository at this point in the history
  • Loading branch information
Bungeefan committed Apr 14, 2024
1 parent 0a8b4b8 commit 6a65866
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 44 deletions.
2 changes: 1 addition & 1 deletion lib/src/filters/development_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DevelopmentFilter extends LogFilter {
bool shouldLog(LogEvent event) {
var shouldLog = false;
assert(() {
if (event.level.value >= level!.value) {
if (event.level.value >= level.value) {
shouldLog = true;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/filters/production_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import '../log_filter.dart';
class ProductionFilter extends LogFilter {
@override
bool shouldLog(LogEvent event) {
return event.level.value >= level!.value;
return event.level.value >= level.value;
}
}
10 changes: 4 additions & 6 deletions lib/src/log_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import 'logger.dart';
/// An abstract filter of log messages.
///
/// You can implement your own `LogFilter` or use [DevelopmentFilter].
/// Every implementation should consider [level].
/// Every implementation should consider [Logger.level].
abstract class LogFilter {
Level? _level;
late Logger logger;

// Still nullable for backwards compatibility.
Level? get level => _level ?? Logger.defaultLevel;

set level(Level? value) => _level = value;
/// Shortcut getter for [Logger.level].
Level get level => logger.level;

Future<void> init() async {}

Expand Down
3 changes: 3 additions & 0 deletions lib/src/log_output.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'logger.dart';
import 'output_event.dart';

/// Log output receives a [OutputEvent] from [LogPrinter] and sends it to the
Expand All @@ -8,6 +9,8 @@ import 'output_event.dart';
/// This can be an output stream, a file or a network target. [LogOutput] may
/// cache multiple log messages.
abstract class LogOutput {
late Logger logger;

Future<void> init() async {}

void output(OutputEvent event);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/log_printer.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'log_event.dart';
import 'logger.dart';

/// An abstract handler of log events.
///
Expand All @@ -10,6 +11,8 @@ import 'log_event.dart';
///
/// You can implement a `LogPrinter` from scratch or extend [PrettyPrinter].
abstract class LogPrinter {
late Logger logger;

Future<void> init() async {}

/// Is called every time a new [LogEvent] is sent and handles printing or
Expand Down
20 changes: 12 additions & 8 deletions lib/src/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ typedef OutputCallback = void Function(OutputEvent event);

/// Use instances of logger to send log messages to the [LogPrinter].
class Logger {
/// The current logging level of the app.
///
/// All logs with levels below this level will be omitted.
/// The default logging level of new loggers.
static Level defaultLevel = Level.trace;

/// The current default implementation of log filter.
Expand All @@ -33,6 +31,10 @@ class Logger {
static final Set<OutputCallback> _outputCallbacks = {};

late final Future<void> _initialization;

/// All logs with levels below this level will be omitted.
Level level;

final LogFilter _filter;
final LogPrinter _printer;
final LogOutput _output;
Expand All @@ -44,17 +46,19 @@ class Logger {
/// defaults: [PrettyPrinter], [DevelopmentFilter] and [ConsoleOutput] will be
/// used.
Logger({
Level? level,
LogFilter? filter,
LogPrinter? printer,
LogOutput? output,
Level? level,
}) : _filter = filter ?? defaultFilter(),
}) : level = level ?? defaultLevel,
_filter = filter ?? defaultFilter(),
_printer = printer ?? defaultPrinter(),
_output = output ?? defaultOutput() {
_filter.logger = this;
_printer.logger = this;
_output.logger = this;

var filterInit = _filter.init();
if (level != null) {
_filter.level = level;
}
var printerInit = _printer.init();
var outputInit = _output.init();
_initialization = Future.wait([filterInit, printerInit, outputInit]);
Expand Down
34 changes: 6 additions & 28 deletions test/logger_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,6 @@ class _AsyncOutput extends LogOutput {
}
}

/// Test class for the lazy-initialization of variables.
class LazyLogger {
static bool? printed;
static final filter = ProductionFilter();
static final printer = _CallbackPrinter((l, m, e, s) {
printed = true;
return [];
});
static final logger = Logger(filter: filter, printer: printer);
}

void main() {
Level? printedLevel;
Object? printedMessage;
Expand Down Expand Up @@ -248,7 +237,7 @@ void main() {
expect(printedMessage, heavyComputation);
});

test('setting log level above log level of message', () {
test('Setting log level above log level of message', () {
printedMessage = null;
var logger = Logger(
filter: ProductionFilter(),
Expand All @@ -263,21 +252,17 @@ void main() {
expect(printedMessage, 'This is');
});

test('Setting filter Levels', () {
var filter = ProductionFilter();
expect(filter.level, Logger.defaultLevel);

test('Setting log level', () {
final initLevel = Level.warning;
// ignore: unused_local_variable
var logger = Logger(
filter: filter,
filter: ProductionFilter(),
printer: callbackPrinter,
level: initLevel,
);
expect(filter.level, initLevel);
expect(logger.filter.level, initLevel);

filter.level = Level.fatal;
expect(filter.level, Level.fatal);
logger.level = Level.fatal;
expect(logger.filter.level, Level.fatal);
});

test('Logger.close', () async {
Expand All @@ -287,13 +272,6 @@ void main() {
expect(logger.isClosed(), true);
});

test('Lazy Logger Initialization', () {
expect(LazyLogger.printed, isNull);
LazyLogger.filter.level = Level.warning;
LazyLogger.logger.i("This is an info message and should not show");
expect(LazyLogger.printed, isNull);
});

test('Async Filter Initialization', () async {
var comp = _AsyncFilter(const Duration(milliseconds: 100));
var logger = Logger(
Expand Down

0 comments on commit 6a65866

Please sign in to comment.