Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/talker_dio_logger/lib/dio_logs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import 'package:talker_dio_logger/talker_dio_logger.dart';
const _encoder = JsonEncoder.withIndent(' ');
const _hiddenValue = '*****';

/// Shared helper that appends a formatted timestamp line to [msg] when
/// [settings.logTimestamp] is enabled.
///
/// Uses the customisable [TalkerDioLoggerSettings.timestampLabel] and
/// [TalkerDioLoggerSettings.timestampFormat] so the format and label are
/// consistent across request, response, and error logs.
String _appendTimestamp(
String msg,
TalkerDioLoggerSettings settings,
String Function({required TimeFormat timeFormat}) displayTime,
) {
if (settings.logTimestamp) {
msg +=
'\n ${settings.timestampLabel}: ${displayTime(timeFormat: settings.timestampFormat)}';
}
return msg;
}

class DioRequestLog extends TalkerLog {
DioRequestLog(
String message, {
Expand All @@ -31,6 +49,7 @@ class DioRequestLog extends TalkerLog {
TimeFormat timeFormat = TimeFormat.timeAndSeconds,
}) {
var msg = '[$title] [${requestOptions.method}] $message';
msg = _appendTimestamp(msg, settings, displayTime);

final data = requestOptions.data;
final headers = Map.from(requestOptions.headers);
Expand Down Expand Up @@ -122,6 +141,7 @@ class DioResponseLog extends TalkerLog {
TimeFormat timeFormat = TimeFormat.timeAndSeconds,
}) {
var msg = '[$title] [${response.requestOptions.method}] $message';
msg = _appendTimestamp(msg, settings, displayTime);

final responseMessage = response.statusMessage;
final data = response.data;
Expand Down Expand Up @@ -192,6 +212,7 @@ class DioErrorLog extends TalkerLog {
TimeFormat timeFormat = TimeFormat.timeAndSeconds,
}) {
var msg = '[$title] [${dioException.requestOptions.method}] $message';
msg = _appendTimestamp(msg, settings, displayTime);

final responseMessage = dioException.message;
final statusCode = dioException.response?.statusCode;
Expand Down
20 changes: 20 additions & 0 deletions packages/talker_dio_logger/lib/talker_dio_logger_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class TalkerDioLoggerSettings {
this.printRequestData = true,
this.printRequestHeaders = false,
this.printRequestExtra = false,
this.logTimestamp = false,
this.timestampFormat = TimeFormat.yearMonthDayAndTime,
this.timestampLabel = 'Date',
this.hiddenHeaders = const <String>{},
this.responseDataConverter,
this.requestPen,
Expand Down Expand Up @@ -66,6 +69,17 @@ class TalkerDioLoggerSettings {
/// Print [request.extra] if true
final bool printRequestExtra;

/// Print timestamp in logs if true
final bool logTimestamp;

/// The [TimeFormat] used when [logTimestamp] is enabled.
/// Defaults to [TimeFormat.yearMonthDayAndTime].
final TimeFormat timestampFormat;

/// The label printed before the timestamp value when [logTimestamp] is enabled.
/// Defaults to `'Date'`.
final String timestampLabel;

/// Field to set custom http request console logs color
///```
///// Red color
Expand Down Expand Up @@ -129,6 +143,9 @@ class TalkerDioLoggerSettings {
bool? printRequestData,
bool? printRequestHeaders,
bool? printRequestExtra,
bool? logTimestamp,
TimeFormat? timestampFormat,
String? timestampLabel,
AnsiPen? requestPen,
AnsiPen? responsePen,
AnsiPen? errorPen,
Expand All @@ -150,6 +167,9 @@ class TalkerDioLoggerSettings {
printRequestData: printRequestData ?? this.printRequestData,
printRequestHeaders: printRequestHeaders ?? this.printRequestHeaders,
printRequestExtra: printRequestExtra ?? this.printRequestExtra,
logTimestamp: logTimestamp ?? this.logTimestamp,
timestampFormat: timestampFormat ?? this.timestampFormat,
timestampLabel: timestampLabel ?? this.timestampLabel,
requestPen: requestPen ?? this.requestPen,
responsePen: responsePen ?? this.responsePen,
errorPen: errorPen ?? this.errorPen,
Expand Down