A Guide to Setting up Better Logging in Flutter

Dane Mackier
Jul 2 · 4 min read

Setup

logger: ^0.6.0

Usage

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');
final logger = Logger(
printer: PrettyPrinter(
methodCount: 0,
errorMethodCount: 5,
lineLength: 50,
colors: true,
printEmojis: true,
printTime: false,
)
);

Custom Log Printer

import 'package:logger/logger.dart';class SimpleLogPrinter extends LogPrinter {
@override
void log(Level level, message, error, StackTrace stackTrace) {
println(message);
}
}
final logger = Logger(printer: SimpleLogPrinter());
[emoji] [ClassName] - [Message]💡 LocationService - Request Location Update
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'));
}
}
final logger = Logger(printer: SimpleLogPrinter('PermissionService'));
import 'package:logger/logger.dart';
import 'log_printer.dart';
Logger getLogger(String className) {
return Logger(printer: SimpleLogPrinter('PermissionService'));
}
final log = getLogger('PostService');
void main() {
Logger.level = Level.verbose;
runApp(MyApp());
}

Flutter Community

Articles and Stories from the Flutter Community

Dane Mackier

Written by

A full stack software developer focused on building mobile products, its tools and architecture. Always reducing boiler plate code and experimenting.

Flutter Community

Articles and Stories from the Flutter Community