How to log data to the Flutter console?
Intellij IdeaConsoleDartFlutterIntellij Idea Problem Overview
I am a beginner and using IntelliJ IDEA, and I wanted to log data to the console?
I tried print()
and printDebug()
, but none of my data were showing in the Flutter console.
Intellij Idea Solutions
Solution 1 - Intellij Idea
If you're inside a Flutter Widget
, you can use debugPrint
, e.g.,
import 'package:flutter/foundation.dart';
debugPrint('movieTitle: $movieTitle');
Or, use Dart's built in log()
function
import 'dart:developer';
log('data: $data');
Solution 2 - Intellij Idea
The Dart print() function outputs to the system console, which you can view using flutter logs (which is basically a wrapper around adb logcat).
If you output too much at once, then Android sometimes discards some log lines. To avoid this, you can use debugPrint().
Found here: https://flutter.io/docs/testing/debugging
Solution 3 - Intellij Idea
log() from 'dart:developer'
-
It doesn't seem to have a max length limits like
print()
ordebugPrint()
. -
So it comes helpful when you want to log the whole API response.
-
And also helps in dart dev tools to show formatted logging.
import 'dart:developer'; //(auto import will do this even)
//example for api logging
log("${response?.statusCode} : ${response?.request?.path}",
name: "Response", error: response.data);
Solution 4 - Intellij Idea
To be crystal clear, debugPrint
will only work inside a Flutter widget.
Solution 5 - Intellij Idea
I used print() in my code and it prints in the Debug Console.
Solution 6 - Intellij Idea
Alternatively, I have created a Logger for Flutter Apps: https://github.com/hiteshsahu/Flutter-Logger
Simply copy AppLog.dart
& add to yor project and use it like this:
- AppLog.i("Info Message"); // Simple Info message
- AppLog.i("Home Page", tag: "User Logging"); // Info message with identifier TAG
Examples:
INPUT
AppLog.v("-----------------------------");
AppLog.d("I am Debug Log With Default TAG");
AppLog.i("I am Info Log With Default TAG");
AppLog.w("I am Warn Log With Default TAG");
AppLog.e("I am Error Log With Default TAG");
AppLog.wtf("I am Failure Log With Default TAG");
AppLog.v("I am Verbose Log With Default TAG");
//With TAGS
AppLog.v("-----------------------------");
AppLog.d("I am Debug Log With Custom TAG", tag: "Awesome Widget");
AppLog.i("I am Info Log With Custom TAG", tag: "Awesome Widget");
AppLog.w("I am Warn Log With Custom TAG", tag: "Awesome Widget");
AppLog.e("I am Error Log With Custom TAG", tag: "Awesome Widget");
AppLog.wtf("I am Failure Log With Custom TAG", tag: "Awesome Widget");
AppLog.v("I am Verbose Log With Custom TAG", tag: "Awesome Widget");
AppLog.v("-----------------------------");
OUTPUT:
> Restarted application in 347ms. > FlutterApp: ----------------------------- > DEBUG|FlutterApp: I am Debug Log With Default TAG > INFOⓘ|FlutterApp: I am Info Log With Default TAG > WARN⚠️|FlutterApp: I am Warn Log With Default TAG > ERROR⚠️|️FlutterApp: I am Error Log With Default TAG > WTF¯_(ツ)/¯|FlutterApp: I am Failure Log With Default TAG > FlutterApp: I am Verbose Log With Default TAG > FlutterApp: ----------------------------- > DEBUG|Awesome Widget: I am Debug Log With Custom TAG > INFOⓘ|Awesome Widget: I am Info Log With Custom TAG > WARN⚠️|Awesome Widget: I am Warn Log With Custom TAG > ERROR⚠️|️Awesome Widget: I am Error Log With Custom TAG > WTF¯_(ツ)/¯|Awesome Widget: I am Failure Log With Custom TAG > Awesome Widget: I am Verbose Log With Custom TAG > FlutterApp: -----------------------------
You can also set a filter for Log Levels
> AppLog.setLogLevel(log_priority); // logs below log_priority will be hidden
Where priority is always:
> VERBOSE<=log_priority<= FAILURE >
> Priority Level: VERBOSE
Example Hide all info and Debug Logs:
AppLog.setLogLevel(AppLog.WARN);
AppLog.v("-----------------------------");
AppLog.d("Debug Log Will not be Visible");
AppLog.i("Info Log Will not be Visible");
AppLog.w("Warn Log Will be Visible");
AppLog.e("Error Log Will be Visible");
AppLog.wtf("Failure Log Will be Visible");
AppLog.v("Verbose Log Will not be Visible");
AppLog.v("-----------------------------");
OUTPUT
> WARN⚠️|FlutterApp: Warn Log Will be Visible > ERROR⚠️|️FlutterApp: Error Log Will be Visible > WTF¯_(ツ)_/¯|FlutterApp: Failure Log Will be Visible
Feel free to use my logger or if you have some ideas for improvement please create PR or let me know I will improve it.
Solution 7 - Intellij Idea
you can use the Logger package it's easy and simple