Skip to content

Commit 901b07b

Browse files
committed
Added DateTimeFormatter to PrettyPrinter, closes #80
1 parent 4d8d595 commit 901b07b

File tree

4 files changed

+97
-24
lines changed

4 files changed

+97
-24
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ var logger = Logger(
8484
lineLength: 120, // Width of the output
8585
colors: true, // Colorful log messages
8686
printEmojis: true, // Print an emoji for each log message
87-
printTime: false // Should each log print contain a timestamp
87+
// Should each log print contain a timestamp
88+
dateTimeFormat: DateTimeFormat.onlyTimeAndSinceStart,
8889
),
8990
);
9091
```

lib/src/date_time_format.dart

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import 'printers/pretty_printer.dart';
2+
3+
typedef DateTimeFormatter = String Function(DateTime time);
4+
5+
class DateTimeFormat {
6+
/// Omits the date and time completely.
7+
static const DateTimeFormatter none = _none;
8+
9+
/// Prints only the time.
10+
///
11+
/// Example:
12+
/// * `12:30:40.550`
13+
static const DateTimeFormatter onlyTime = _onlyTime;
14+
15+
/// Prints only the time including the difference since [PrettyPrinter.startTime].
16+
///
17+
/// Example:
18+
/// * `12:30:40.550 (+0:00:00.060700)`
19+
static const DateTimeFormatter onlyTimeAndSinceStart = _onlyTimeAndSinceStart;
20+
21+
/// Prints only the date.
22+
///
23+
/// Example:
24+
/// * `2019-06-04`
25+
static const DateTimeFormatter onlyDate = _onlyDate;
26+
27+
/// Prints date and time (combines [onlyDate] and [onlyTime]).
28+
///
29+
/// Example:
30+
/// * `2019-06-04 12:30:40.550`
31+
static const DateTimeFormatter dateAndTime = _dateAndTime;
32+
33+
DateTimeFormat._();
34+
35+
static String _none(DateTime t) => throw UnimplementedError();
36+
37+
static String _onlyTime(DateTime t) {
38+
String threeDigits(int n) {
39+
if (n >= 100) return '$n';
40+
if (n >= 10) return '0$n';
41+
return '00$n';
42+
}
43+
44+
String twoDigits(int n) {
45+
if (n >= 10) return '$n';
46+
return '0$n';
47+
}
48+
49+
var now = t;
50+
var h = twoDigits(now.hour);
51+
var min = twoDigits(now.minute);
52+
var sec = twoDigits(now.second);
53+
var ms = threeDigits(now.millisecond);
54+
return '$h:$min:$sec.$ms';
55+
}
56+
57+
static String _onlyTimeAndSinceStart(DateTime t) {
58+
var timeSinceStart = t.difference(PrettyPrinter.startTime!).toString();
59+
return '${onlyTime(t)} (+$timeSinceStart)';
60+
}
61+
62+
static String _onlyDate(DateTime t) {
63+
String isoDate = t.toIso8601String();
64+
return isoDate.substring(0, isoDate.indexOf("T"));
65+
}
66+
67+
static String _dateAndTime(DateTime t) {
68+
return "${_onlyDate(t)} ${_onlyTime(t)}";
69+
}
70+
}

lib/src/printers/pretty_printer.dart

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22
import 'dart:math';
33

44
import '../ansi_color.dart';
5+
import '../date_time_format.dart';
56
import '../log_event.dart';
67
import '../log_level.dart';
78
import '../log_printer.dart';
@@ -64,7 +65,7 @@ class PrettyPrinter extends LogPrinter {
6465
static final _browserStackTraceRegex =
6566
RegExp(r'^(?:package:)?(dart:\S+|\S+)');
6667

67-
static DateTime? _startTime;
68+
static DateTime? startTime;
6869

6970
/// The index at which the stack trace should start.
7071
///
@@ -113,7 +114,11 @@ class PrettyPrinter extends LogPrinter {
113114
final bool printEmojis;
114115

115116
/// Whether [LogEvent.time] is printed.
116-
final bool printTime;
117+
@Deprecated("Use `dateTimeFormat` instead.")
118+
bool get printTime => dateTimeFormat != DateTimeFormat.none;
119+
120+
/// Controls the format of [LogEvent.time].
121+
final DateTimeFormatter dateTimeFormat;
117122

118123
/// Controls the ascii 'boxing' of different [Level]s.
119124
///
@@ -191,14 +196,25 @@ class PrettyPrinter extends LogPrinter {
191196
this.lineLength = 120,
192197
this.colors = true,
193198
this.printEmojis = true,
194-
this.printTime = false,
199+
@Deprecated(
200+
"Use `dateTimeFormat` with `DateTimeFormat.onlyTimeAndSinceStart` or `DateTimeFormat.none` instead.")
201+
bool? printTime,
202+
DateTimeFormatter dateTimeFormat = DateTimeFormat.none,
195203
this.excludeBox = const {},
196204
this.noBoxingByDefault = false,
197205
this.excludePaths = const [],
198206
this.levelColors,
199207
this.levelEmojis,
200-
}) {
201-
_startTime ??= DateTime.now();
208+
}) : assert(
209+
(printTime != null && dateTimeFormat == DateTimeFormat.none) ||
210+
printTime == null,
211+
"Don't set printTime when using dateTimeFormat"),
212+
dateTimeFormat = printTime == null
213+
? dateTimeFormat
214+
: (printTime
215+
? DateTimeFormat.onlyTimeAndSinceStart
216+
: DateTimeFormat.none) {
217+
startTime ??= DateTime.now();
202218

203219
var doubleDividerLine = StringBuffer();
204220
var singleDividerLine = StringBuffer();
@@ -241,6 +257,8 @@ class PrettyPrinter extends LogPrinter {
241257
var errorStr = event.error?.toString();
242258

243259
String? timeStr;
260+
// Keep backwards-compatibility to `printTime` check
261+
// ignore: deprecated_member_use_from_same_package
244262
if (printTime) {
245263
timeStr = getTime(event.time);
246264
}
@@ -332,24 +350,7 @@ class PrettyPrinter extends LogPrinter {
332350
}
333351

334352
String getTime(DateTime time) {
335-
String threeDigits(int n) {
336-
if (n >= 100) return '$n';
337-
if (n >= 10) return '0$n';
338-
return '00$n';
339-
}
340-
341-
String twoDigits(int n) {
342-
if (n >= 10) return '$n';
343-
return '0$n';
344-
}
345-
346-
var now = time;
347-
var h = twoDigits(now.hour);
348-
var min = twoDigits(now.minute);
349-
var sec = twoDigits(now.second);
350-
var ms = threeDigits(now.millisecond);
351-
var timeSinceStart = now.difference(_startTime!).toString();
352-
return '$h:$min:$sec.$ms (+$timeSinceStart)';
353+
return dateTimeFormat(time);
353354
}
354355

355356
// Handles any object that is causing JsonEncoder() problems

lib/web.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
library web;
33

44
export 'src/ansi_color.dart';
5+
export 'src/date_time_format.dart';
56
export 'src/filters/development_filter.dart';
67
export 'src/filters/production_filter.dart';
78
export 'src/log_event.dart';

0 commit comments

Comments
 (0)