Flutter log output, Flutter print log, flutter log, flutter real machine log
More application knowledge points, the editor has summarized in the book
At present, the Flutter series of tutorials are published for free on the watermelon video, which is updated daily. Welcome to pay attention to receive reminders
[x2] Various series of tutorials
[x3] flutter accumulation series of articles
Flutter provides print(Object object) to output log information to the console of the development tool
print("test");
- 1
The basic data types that can be directly output by interpolation are as follows:
String str = "Zhang San" ;
print ( "Test output $str " ) ;
int count = 40 ;
print ( "Test output $count " ) ;
- 1
- 2
- 3
- 4
- 5
If it is an object type, such as to output the value of a property of the object, you can do this:
User user = new User ( name : "Zhang San" ) ;
///Output name
print ( "Test output ${user.name} " ) ;
- 1
- 2
- 3
- 4
The common_utils tool class has encapsulated pring as a tool class
common_utils: ^1.1.1
- 1
Use LogUtil from the common_utils tool class
//Initialize setting LogUtil
LogUtil . init ( true ) ;
// Output log
LogUtil . v ( "test" ) ;
- 1
- 2
- 3
- 4
Of course, the init method of LogUtil can configure true and false according to whether it is a production environment. If it is false, the log will not be output. Such an optimization can also save the consumption of outputting log information to the console after the version is released.
The package source code is as follows
class LogUtil {
static const String _TAG_DEF = "###common_utils###";
static bool debuggable = false ; //Whether it is debug mode, true: log v does not output.
static String TAG = _TAG_DEF ;
static void init({bool isDebug = false, String tag = _TAG_DEF}) {
debuggable = isDebug;
TAG = tag;
}
static void e(Object object, {String tag}) {
_printLog(tag, ' e ', object);
}
static void v(Object object, {String tag}) {
if (debuggable) {
_printLog(tag, ' v ', object);
}
}
static void _printLog(String tag, String stag, Object object) {
StringBuffer sb = new StringBuffer();
sb.write((tag == null || tag.isEmpty) ? TAG : tag);
sb.write(stag);
sb.write(object);
print(sb.toString());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
After completing the daily accumulation, it is all bit by bit