Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”. This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

Today, we’ll look at one of the tasks that can greatly reduce application debugging time. Once you get used to logging a certain way in your application, you’ll soon be able to notice why something doesn’t work. You can view the flow of the application, and more if you need to.

We will use the Logger package for all logging.

Set up the

Add the logger package to your project

The logger: ^ 0.6.0Copy the code

usage

To use loggers, you create a new logger in the class and record it using one of the method calls.

final logger = Logger();
​
logger.v('You don't always want to see all of these');
logger.d('Logs a debug message');
logger.i('Public Function called');
logger.w('This might become a problem');
logger.e('Something has happened');
Copy the code

By default, these calls will output the following logs.

This may not be to everyone’s taste. I personally am not a big fan of all print lines and there are some things I would like to delete, so let’s provide an instance of a PrettyPrinter and do some customization to it.

I want to remove the method count printed above, and when an exception has a stack trace, I want to see up to 5 methods in that trace. I wanted the lines around the log to be reduced and I wanted to keep the color to provide visual feedback. Emoticons are reserved. I want to disable the timestamp.

final logger = Logger(
  printer: PrettyPrinter(
    methodCount: 0,
    errorMethodCount: 5,
    lineLength: 50,
    colors: true,
    printEmojis: true,
    printTime: false,
  )
);
Copy the code

This results in the following output

Custom log printer

In some cases, it may even be too much for people. Honestly, the only thing I like is the color of each log, with emojis in front of it. I like to use visual queues to help me debug faster. As I mentioned earlier, given a particular scenario, you begin to understand the log flow in your application, and visual queues will help you more. One thing the logger is missing is the name of the class it is printing. I want that to be the first message.

Create a new file called log_printer.dart to print the incoming message. This is the most basic type of printer, there is nothing special about it.

import 'package:logger/logger.dart'; class SimpleLogPrinter extends LogPrinter { @override void log(Level level, message, error, StackTrace stackTrace) { println(message); }}Copy the code

Then we will set the SimpleLogPrinter to be the printer for our Logger.

  final logger = Logger(printer: SimpleLogPrinter());
Copy the code

The logging type that works for me is one that has color and knows which class is printing the log. So SimpleLogPrinter takes a name to display and uses the defined color PrettyPrinter to print the log. We want the following format

[emoji] [ClassName] - [Message]
​
💡 LocationService - Request Location Update
Copy the code

Let’s do it fast.

class SimpleLogPrinter extends LogPrinter { final String className; SimpleLogPrinter(this.className); @override void log(Level level, message, error, StackTrace stackTrace) { var color = PrettyPrinter.levelColors[level]; var emoji = PrettyPrinter.levelEmojis[level]; println(color('$emoji $className - $message')); }}Copy the code

This is the output.

I like it. Sometimes I add spacing around certain levels. The informational message is especially important because it is often the entry point for subsequent logs. I use the information I use to record public method calls, so it’s easy to see what your code is doing.

Let’s just get out of here. You can customize it as much as you like. The code to create the logger is currently shown below, which is a bit too much for me.

 final logger = Logger(printer: SimpleLogPrinter('PermissionService'));
Copy the code

I usually use top-level functions to create loggers for me. Create a new file called Logger and add it to it.

import 'package:logger/logger.dart';
import 'log_printer.dart';
​
Logger getLogger(String className) {
  return Logger(printer: SimpleLogPrinter(className));
}
Copy the code

Now all you do in your code is this.

final log = getLogger('PostService');
Copy the code

The last thing to do is set the logging level so that you don’t see all the logs all the time. Set the level before the application runs in your main file.

void main() {
  Logger.level = Level.verbose;
  runApp(MyApp());
}
Copy the code

This is logging.