diff --git a/lib/src/printers/single_pretty_printer.dart b/lib/src/printers/single_pretty_printer.dart index 381e6c4..739ce11 100644 --- a/lib/src/printers/single_pretty_printer.dart +++ b/lib/src/printers/single_pretty_printer.dart @@ -205,7 +205,10 @@ class SinglePrettyPrinter extends LogPrinter { line.startsWith(selfPath); } - String _getCurrentTime() { + @visibleForTesting + String getCurrentTime({ + DateTime? dateTime, + }) { String _threeDigits(int n) { if (n >= 100) { return '$n'; @@ -223,7 +226,7 @@ class SinglePrettyPrinter extends LogPrinter { return '0$n'; } - final now = DateTime.now(); + final now = dateTime ?? DateTime.now(); final h = _twoDigits(now.hour); final min = _twoDigits(now.minute); final sec = _twoDigits(now.second); @@ -287,7 +290,7 @@ class SinglePrettyPrinter extends LogPrinter { buffer.add(levelLabels[level]!); } if (printTime) { - buffer.add(_getCurrentTime()); + buffer.add(getCurrentTime()); } if (printCaller) { final caller = getCaller(); diff --git a/lib/src/roggle.dart b/lib/src/roggle.dart index bb82a01..9f059a1 100644 --- a/lib/src/roggle.dart +++ b/lib/src/roggle.dart @@ -19,7 +19,7 @@ class Roggle { _output = output ?? ConsoleOutput() { _filter.init(); // ignore: cascade_invocations - _filter.level = level ?? Logger.level; + _filter.level = level ?? Roggle.level; _printer.init(); _output.init(); } diff --git a/test/printers/single_pretty_printer_test.dart b/test/printers/single_pretty_printer_test.dart index 4e1bb70..d9544cf 100644 --- a/test/printers/single_pretty_printer_test.dart +++ b/test/printers/single_pretty_printer_test.dart @@ -551,6 +551,22 @@ packages/flutter_sample_custom_logger/main.dart 5:10 main ); }); }); + group('getCurrentTime()', () { + test('getCurrentTime()', () { + _wrapCurrentTimeTest( + DateTime(2022, 1, 1, 0, 0, 0, 0), + '00:00:00.000', + ); + _wrapCurrentTimeTest( + DateTime(2022, 1, 1, 10, 10, 10, 10), + '10:10:10.010', + ); + _wrapCurrentTimeTest( + DateTime(2022, 1, 1, 20, 20, 20, 100), + '20:20:20.100', + ); + }); + }); } String _readMessage(List log) { @@ -593,3 +609,14 @@ void _wrapCallerTest( ); expect(actualCaller, expectCaller); } + +void _wrapCurrentTimeTest( + DateTime dateTime, + String? expectTime, +) { + final printer = SinglePrettyPrinter(); + final actualTime = printer.getCurrentTime( + dateTime: dateTime, + ); + expect(actualTime, expectTime); +} diff --git a/test/roggle_test.dart b/test/roggle_test.dart index aae65bc..520b202 100644 --- a/test/roggle_test.dart +++ b/test/roggle_test.dart @@ -56,11 +56,16 @@ void main() { printedStackTrace = null; }); - test('Constructor', () { - Roggle(filter: _NeverFilter(), printer: callbackPrinter) - .log(Level.debug, 'Some message'); - - expect(printedMessage, null); + group('Constructor', () { + test('default', () { + Roggle().d('some message'); + }); + test('filter', () { + Roggle(filter: _NeverFilter(), printer: callbackPrinter) + .log(Level.debug, 'Some message'); + + expect(printedMessage, null); + }); }); test('Roggle.log', () { @@ -177,4 +182,51 @@ void main() { logger.w('This is'); expect(printedMessage, 'This is'); }); + + test('setting static log level above log level of message', () { + printedMessage = null; + Roggle.level = Level.warning; + final logger = Roggle( + filter: ProductionFilter(), + printer: callbackPrinter, + )..d('This isn\'t logged'); + expect(printedMessage, isNull); + + logger.w('This is'); + expect(printedMessage, 'This is'); + + Roggle.level = Level.verbose; + }); + + test('get filter', () { + final filter = ProductionFilter(); + final logger = Roggle( + filter: filter, + ); + expect(logger.filter.hashCode, filter.hashCode); + }); + + test('get printer', () { + final printer = SinglePrettyPrinter(); + final logger = Roggle( + printer: printer, + ); + expect(logger.printer.hashCode, printer.hashCode); + }); + + test('get output', () { + final output = ConsoleOutput(); + final logger = Roggle( + output: output, + ); + expect(logger.output.hashCode, output.hashCode); + }); + + test('get active', () { + final logger = Roggle(); + expect(logger.active, true); + + logger.close(); + expect(logger.active, false); + }); }