187

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.

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59

7 Answers7

345

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');
excitedmicrobe
  • 2,338
  • 1
  • 14
  • 30
  • Is there any way where we can write `array [0]`, `array[1]` or any run time assignment in the debugger ? – Jack Sep 27 '19 at 10:09
  • 3
    Note that debugPrint is now included in flutter/material so you don't necessarily need to import flutter/foundation. – Antonin GAVREL Jul 13 '20 at 16:48
  • 8
    Does debugPrint prints only one debug mode? In my app, I have lots of print statements which I use `print()` to print. What I want to do is to print only on debug mode. – SametSahin Mar 17 '21 at 19:08
  • Can an example using the `time` and `zone` param on `log()` be added please? How can one add those so it actually shows? – Corné Sep 13 '21 at 23:22
  • May I know which window in Android Studio shows the outputs of `log('blah')`? I don't see it in the `Run` tab. – Nav Jul 24 '23 at 05:08
37

log() from 'dart:developer'

  • It doesn't seem to have a max length limits like print() or debugPrint().

  • 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);
Rahul Dange
  • 2,371
  • 20
  • 15
36

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

max
  • 571
  • 7
  • 19
6

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:

  1. AppLog.i("Info Message"); // Simple Info message
  2. 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<DEBUG<INFO<WARNING<ERROR<FAILURE

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.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

I replace to print command to logPrint

In Android Studio debugging Emulator.EMULATOR equal "" I will see to the logs

In production or testing I will set it to a number

const bool debugMode = (Emulator.EMULATOR=="");
void logPrint(String _s) {
    if(debugMode){
        print(_s);
    }
}
class Emulator {
    static const EMULATOR = String.fromEnvironment('EMULATOR', defaultValue: "");
}
Kobi
  • 127
  • 1
  • 11
0

I recommend the package Logger : https://pub.dev/packages/logger

This is the most used package for logging, and it clearly shows different levels of log in the console, with colors.

Jack'
  • 1,722
  • 1
  • 19
  • 27
-2

'''import 'dart:developer' as dev;'''

and then

dev.log('$var')

Nirosh
  • 63
  • 8
  • Welcome to SO. Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since another user already posted that solution. If a previous answer was helpful to you, you should [vote it up](https://stackoverflow.com/help/privileges/vote-up) once you have enough [reputation](https://stackoverflow.com/help/whats-reputation) – lepsch Sep 01 '22 at 15:10