Skip to content

Commit

Permalink
! d r updated docs, example and some other minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed Nov 20, 2024
1 parent 716f819 commit aec71c6
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 67 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,27 @@ And you have **full customization control** over them!
class YourCustomLog extends TalkerLog {
YourCustomLog(String message) : super(message);
/// Your custom log title
@override
String get title => 'CUSTOM';
/// Your own log key (for color customization in settings)
static const logKey = 'custom_log_key';
/// Your custom log color
@override
AnsiPen get pen => AnsiPen()..xterm(121);
String? get key => logKey;
}
final talker = Talker();
final talker = Talker(
settings: TalkerSettings(
colors: {
TalkerLogType.info.key: AnsiPen()..magenta(),
YourCustomLog.logKey: AnsiPen()..green(),
},
titles: {
TalkerLogType.exception.key: 'Whatever you want',
TalkerLogType.error.key: 'E',
TalkerLogType.info.key: 'i',
YourCustomLog.logKey: 'Custom',
},
),
);
talker.logCustom(YourCustomLog('Something like your own service message'));
```

Expand Down
Binary file modified docs/assets/logger/custom_log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 7 additions & 14 deletions packages/talker/example/talker_example.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import 'package:talker/talker.dart';

const _customLogKey = 'your_cutom_log_key';

Future<void> main() async {
final talker = Talker(
settings: TalkerSettings(
colors: {
TalkerLogType.info.key: AnsiPen()..magenta(),
_customLogKey: AnsiPen()..green(),
YourCustomLog.logKey: AnsiPen()..green(),
},
titles: {
TalkerLogType.exception: 'Whatever you want',
TalkerLogType.error: 'E',
TalkerLogType.info: 'i',
TalkerLogType.exception.key: 'Whatever you want',
TalkerLogType.error.key: 'E',
TalkerLogType.info.key: 'i',
YourCustomLog.logKey: 'Custom',
},
),
);
Expand Down Expand Up @@ -40,14 +39,8 @@ class YourCustomLog extends TalkerLog {
YourCustomLog(String message) : super(message);

/// Your own log key (for color customization in settings)
@override
String? get key => _customLogKey;
static const logKey = 'custom_log_key';

/// Your custom log title
@override
String get title => 'CUSTOM';

// /// Your custom log color
// @override
// AnsiPen get pen => AnsiPen()..green();
String? get key => logKey;
}
6 changes: 2 additions & 4 deletions packages/talker/lib/src/models/talker_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ class TalkerData {
/// [TalkerError] -> [TalkerError.generateTextMessage]
///
/// {@endtemplate}
String generateTextMessage(
{TimeFormat timeFormat = TimeFormat.timeAndSeconds}) {
String generateTextMessage({TimeFormat timeFormat = TimeFormat.timeAndSeconds}) {
return '${displayTitleWithTime(timeFormat: timeFormat)}$message$displayStackTrace';
}
}
Expand All @@ -81,8 +80,7 @@ class TalkerData {
extension FieldsToDisplay on TalkerData {
/// Displayed title of [TalkerData]
String displayTitleWithTime(
{TimeFormat timeFormat = TimeFormat.timeAndSeconds}) {
String displayTitleWithTime({TimeFormat timeFormat = TimeFormat.timeAndSeconds}) {
return '[$title] | ${displayTime(timeFormat: timeFormat)} | ';
}

Expand Down
66 changes: 35 additions & 31 deletions packages/talker/lib/src/settings.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import 'package:talker/talker.dart';

const _defaultTitles = {
final _defaultTitles = {
/// Base logs section
TalkerLogType.critical: 'critical',
TalkerLogType.warning: 'warning',
TalkerLogType.verbose: 'verbose',
TalkerLogType.info: 'info',
TalkerLogType.debug: 'debug',
TalkerLogType.error: 'error',
TalkerLogType.exception: 'exception',
TalkerLogType.critical.key: 'critical',
TalkerLogType.warning.key: 'warning',
TalkerLogType.verbose.key: 'verbose',
TalkerLogType.info.key: 'info',
TalkerLogType.debug.key: 'debug',
TalkerLogType.error.key: 'error',
TalkerLogType.exception.key: 'exception',

/// Http section
TalkerLogType.httpError: 'http-error',
TalkerLogType.httpRequest: 'http-request',
TalkerLogType.httpResponse: 'http-response',
TalkerLogType.httpError.key: 'http-error',
TalkerLogType.httpRequest.key: 'http-request',
TalkerLogType.httpResponse.key: 'http-response',

/// Bloc section
TalkerLogType.blocEvent: 'bloc-event',
TalkerLogType.blocTransition: 'bloc-transition',
TalkerLogType.blocCreate: 'bloc-create',
TalkerLogType.blocClose: 'bloc-close',
TalkerLogType.blocEvent.key: 'bloc-event',
TalkerLogType.blocTransition.key: 'bloc-transition',
TalkerLogType.blocCreate.key: 'bloc-create',
TalkerLogType.blocClose.key: 'bloc-close',

/// Riverpod section
TalkerLogType.riverpodAdd: 'riverpod-add',
TalkerLogType.riverpodUpdate: 'riverpod-update',
TalkerLogType.riverpodDispose: 'riverpod-dispose',
TalkerLogType.riverpodFail: 'riverpod-fail',
TalkerLogType.riverpodAdd.key: 'riverpod-add',
TalkerLogType.riverpodUpdate.key: 'riverpod-update',
TalkerLogType.riverpodDispose.key: 'riverpod-dispose',
TalkerLogType.riverpodFail.key: 'riverpod-fail',

/// Flutter section
TalkerLogType.route: 'route',
TalkerLogType.route.key: 'route',
};

final _defaultColors = {
Expand Down Expand Up @@ -73,7 +73,7 @@ class TalkerSettings {
bool useHistory = true,
bool useConsoleLogs = true,
int maxHistoryItems = 1000,
this.titles = _defaultTitles,
Map<String, String>? titles,
Map<String, AnsiPen>? colors,
TimeFormat timeFormat = TimeFormat.timeAndSeconds,
}) : _useHistory = useHistory,
Expand All @@ -83,7 +83,11 @@ class TalkerSettings {
if (colors != null) {
_defaultColors.addAll(colors);
}
if (titles != null) {
_defaultTitles.addAll(titles);
}
this.colors.addAll(_defaultColors);
this.titles.addAll(_defaultTitles);
}
// _writeToFile = writeToFile;

Expand Down Expand Up @@ -126,9 +130,9 @@ class TalkerSettings {
///
/// ```dart
/// final customTitles = {
/// TalkerTitle.info: "Information",
/// TalkerTitle.error: "Error",
/// TalkerTitle.warning: "Warning",
/// TalkerTitle.info.key: "Information",
/// TalkerTitle.error.key: "Error",
/// TalkerTitle.warning.key: "Warning",
/// };
///
/// final logger = Talker(
Expand All @@ -137,7 +141,7 @@ class TalkerSettings {
/// )
/// );
/// ```
final Map<TalkerLogType, String> titles;
final Map<String, String> titles = _defaultTitles;

/// Custom Logger Colors.
///
Expand All @@ -149,9 +153,9 @@ class TalkerSettings {
///
/// ```dart
/// final customColors = {
/// TalkerKey.info: AnsiPen()..white(bold: true),
/// TalkerKey.error: AnsiPen()..red(bold: true),
/// TalkerKey.warning: AnsiPen()..yellow(bold: true),
/// TalkerKey.info.key: AnsiPen()..white(bold: true),
/// TalkerKey.error.key: AnsiPen()..red(bold: true),
/// TalkerKey.warning.key: AnsiPen()..yellow(bold: true),
/// };
///
/// final logger = Talker(
Expand All @@ -164,8 +168,8 @@ class TalkerSettings {
/// By using the `colors` field, you can customize the text colors for specific log keys in the console.
final Map<String, AnsiPen> colors = _defaultColors;

String getTitleByLogType(TalkerLogType key) {
return titles[key] ?? key.key;
String getTitleByLogKey(String key) {
return titles[key] ?? key;
}

AnsiPen getAnsiPenByLogType(TalkerLogType type, {TalkerData? logData}) =>
Expand All @@ -180,7 +184,7 @@ class TalkerSettings {
bool? useHistory,
bool? useConsoleLogs,
int? maxHistoryItems,
Map<TalkerLogType, String>? titles,
Map<String, String>? titles,
Map<String, AnsiPen>? colors,
}) {
return TalkerSettings(
Expand Down
13 changes: 4 additions & 9 deletions packages/talker/lib/src/talker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ class Talker {
_logger = _logger.copyWith(
settings: _logger.settings.copyWith(
colors: {
LogLevel.critical:
settings.getAnsiPenByLogType(TalkerLogType.critical),
LogLevel.critical: settings.getAnsiPenByLogType(TalkerLogType.critical),
LogLevel.error: settings.getAnsiPenByLogType(TalkerLogType.error),
LogLevel.warning: settings.getAnsiPenByLogType(TalkerLogType.warning),
LogLevel.verbose: settings.getAnsiPenByLogType(TalkerLogType.verbose),
Expand Down Expand Up @@ -141,8 +140,7 @@ class Talker {
/// You can connect a listener to it and catch the received errors
///
/// Or you can add your observer [TalkerObserver] in the settings
Stream<TalkerData> get stream =>
_talkerStreamController.stream.asBroadcastStream();
Stream<TalkerData> get stream => _talkerStreamController.stream.asBroadcastStream();

/// The history stores all information about all events like
/// occurred errors [TalkerError]s, exceptions [TalkerException]s
Expand Down Expand Up @@ -371,7 +369,7 @@ class Talker {
final data = TalkerLog(
key: type.key,
message?.toString() ?? '',
title: settings.getTitleByLogType(type),
title: settings.getTitleByLogKey(type.key),
exception: exception,
stackTrace: stackTrace,
pen: pen ?? settings.getPenByLogKey(type.key),
Expand Down Expand Up @@ -415,10 +413,7 @@ class Talker {
final logTypeKey = data.key;

if (logTypeKey != null) {
final logType = TalkerLogType.fromKey(logTypeKey);
if (logType != null) {
data.title = settings.getTitleByLogType(logType);
}
data.title = settings.getTitleByLogKey(logTypeKey);
logPen = settings.getPenByLogKey(
logTypeKey,
fallbackPen: data.pen,
Expand Down
6 changes: 3 additions & 3 deletions packages/talker/lib/src/utils/error_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TalkerErrorHandler {
return TalkerError(
exception,
key: errType.key,
title: settings.getTitleByLogType(errType),
title: settings.getTitleByLogKey(errType.key),
message: msg,
stackTrace: stackTrace,
);
Expand All @@ -31,7 +31,7 @@ class TalkerErrorHandler {
return TalkerException(
exception,
key: exceptionType.key,
title: settings.getTitleByLogType(exceptionType),
title: settings.getTitleByLogKey(exceptionType.key),
message: msg,
stackTrace: stackTrace,
);
Expand All @@ -40,7 +40,7 @@ class TalkerErrorHandler {
return TalkerLog(
exception.toString(),
key: errType.key,
title: settings.getTitleByLogType(errType),
title: settings.getTitleByLogKey(errType.key),
logLevel: LogLevel.error,
stackTrace: stackTrace,
);
Expand Down

0 comments on commit aec71c6

Please sign in to comment.