Monday, September 5, 2022
HomeWordPress DevelopmentColoured logs in Flutter utility with Talker

Coloured logs in Flutter utility with Talker


Hi there everybody 👋
This can be a long-awaited continuation of the collection of articles in regards to the Talker library 😅

On this publish I’ll let you know methods to customise colours and textual content of your logs utilizing this lovely library.

Talker colored logs

Let’s do that 🚀



Base logs

1) Create utility or open an existed
You possibly can create dart console or flutter utility.
Talker relies solely on dart with out flutter sdk dependency subsequently you should utilize this package deal in all places 🙂

For instance I create default dart console utility
Example flutter application

2) Add talker dependency in pubspec.yaml

dependencies:
  talker: ^1.3.0
Enter fullscreen mode

Exit fullscreen mode

Talker dependency

3) Init talker in essential file of utility and make easy logs in essential methodology

import 'package deal:talker/talker.dart';

closing talker = Talker();

void essential() {
  talker.error('It appears to be like like this button isn't working');
  talker.information('The meals for lunch has already arrived');
  talker.warning('One thing dangerous has occurred, however the app remains to be working');
}
Enter fullscreen mode

Exit fullscreen mode

With this code, the output shall be as proven under
Base talker logs

It already appears to be like good.
Talker can show 8 forms of logs by default.
All talker base logs

However that will not be sufficient 🧐
Good, talker have answer for this circumstances too 🥳



Customized logs

For instance our utility can work with server facet backend code through http-requests. And we have to present http logs with completely different colour to spotlight them within the whole checklist of messages.

1) For make customized http logs talker have TalkerLog class which you can extends together with your realization.

class HttpLog extends TalkerLog {
  HttpLog(tremendous.message);
}
Enter fullscreen mode

Exit fullscreen mode

2) OK, however methods to spotlight this log with a particular colour?
You possibly can override pen discipline of your TalkerLog inheritor class.

class HttpLog extends TalkerLog {
  HttpLog(tremendous.message);

  @override
  AnsiPen? get pen => AnsiPen()..cyan();
}
Enter fullscreen mode

Exit fullscreen mode

3) And in essential operate name talker.logTyped() methodology

void essential() {
  talker.logTyped(HttpLog('Http response 200'));
}

class HttpLog extends TalkerLog {
  HttpLog(tremendous.message);

  @override
  AnsiPen? get pen => AnsiPen()..cyan();
}
Enter fullscreen mode

Exit fullscreen mode

This code will present message like instance bellow
Custom first example

4) Extra customization! ⚙️
Like easy instance we are able to override title discipline and generateTextMessage methodology

  • title – Default message title. That used for console output and messages filtering.

  • generateTextMessage() – this methodology creates log messages earlier than you see it in output console. With this methodology you’ll be able to format your messages as you need.

Let’s have a look at in instance

void essential() {
  talker.logTyped(
    HttpLog(
      'Consumer id is loaded',
      knowledge: {'userId': 1234},
    ),
  );
}

class HttpLog extends TalkerLog {
  HttpLog(
    String message, {
    this.knowledge,
  }) : tremendous(message);

  closing dynamic knowledge;

  @override
  AnsiPen get pen => AnsiPen()..cyan();

  @override
  String get title => 'HTTP';

  @override
  String generateTextMessage() {
    var msg = '[$displayTitle] $message';

    if (knowledge != null) {
      closing prettyData = encoder.convert(knowledge);
      msg += 'nDATA:$prettyData';
    }
    return msg;
  }
}
Enter fullscreen mode

Exit fullscreen mode

This code will present message like instance bellow
Custom second example

With talker you’ll be able to customise lots of different issues. The article format isn’t sufficient for your complete description. If you’re , you’ll be able to take a look at the detailed examples within the mission repository.

❤️ Thanks for studying publish
💻 Article instance supply code right here

😌 And hold anticipating posts about different talker chips.
Imagine me, there are lots of them left !

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments