Flutter Tutorial - Log Everything - Smart Logging

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video we want to look at how we can integrate smart logging into our flutter app and therefore we have a plugin and this plugin you get from pub.dev packages logger and then you go to the installing section copy this year and go back to your visual studio code or your ide and go into the pubspec journal file and here under dependencies we put our logger inside and then make sure to restart your application the first thing i like to do with you is to create here a new file so we create here a logging dart file and here inside i create a field which is called logger and i set it here to the logger class and then i import it here so i import this package logger and we want to use this logger variable in our class so right now i write here at the state level a new variable and i call it log and i put here the logger inside and i will import here this logging dot dart file which we have created before and now we can use this variable to lock all our messages i've built here already five buttons so we have different lock levels debug info warning error and what the and basically what we can do now is we can call here this lock instance and then we can use for debug d and then we can add here our debug message and i copy this for all of the other ones because all of them are working the same now we have to change here for info it to i for warning to w for error to e and for what the we added here to what the and we also changed all the messages and now we can press here on the different buttons so for example debug and then you see here the debug message if you click on info then you get the info message warning message error message and also the what's the message and you see like all of them have a different color and they also will show you more information it is the stack trace a little bit and you can change this and what we want to do is we want to change how it is formatted here because i don't like these boxes and what we can do now is to go into our logging and here inside we create our custom logger so we create here a custom printer and extend it from the lock printer so we can change how the locks should be printed and then we need to override here this method lock and here inside we can first of all get the color so we use the same color as the pretty printer which is by default here inside so we use the same colors and here we can access the level colors property and here inside we put our event level so this is only making sure that we have here the same color as these here then we want to get the emoji maybe so you can add an emoji because every error message has you an emoji in front if you like it then you can also get emoji so i will also extract it here from the pretty printer class and therefore we write here level emojis and then we put also again this level inside and the last thing what we have here is the message which is the most important so this is the message we every time put here into our logger inside and this is also what we want to get and therefore we write here event dot message and now we have to return here list and i simply return it then we can format this logging experience here like we want and if you want to use the color also then you put here the color around and that's it we have now our custom printer and we need to use it so we put it here inside into our logger and here at the printer variable we can put our custom printer inside and then we need to hot restart this application and i will also clear the console and now if i click here on the back then you see that we have our new debug message and here we have first of all the emoji and then our message which we have put you inside and we have set here our message to debug message right now for the debug level and therefore it is showing this message and we also can try it with the other ones so we set your info then we have here this info icon and the info message and a different color the warning the error and the what's the message and here you can change basically how this message should look like and i want also to add here a class variable so i go here inside and write here class name and i think it is better to have also the class name here in front because then you know exactly from which location you get this error so i write here in front class name and then i create this constructor and now we have to put our class name inside and now we have to supply here our custom printer with a class name every time so what i do is i write here a callback and i write here type inside so this is the type of the class which we put here inside and then we simply say here type dot to string and now we have an error in our main dart file because here we want to use our logger and this is not really working so we need to also supply here the type and i want to put you every time the class which we are inside right now so i put here the class inside and then we use later our logger with this class so if your hot restart the application and i will also make sure that the console is cleared again and before we can test it we also need to go into our login class and also put the class name here inside so i put first of all the emoji inside then we have the class name with the column and then our message and then you need to hot restart and now we can go again to our main file and now if we press here on debug or a warning or something else then you see every time this icon the class where we are inside and also the warning message and later you can create for every class which you have a different logger and then you use this logger with a different class name to lock your messages and the last thing i want to show you which is really important we can go to our logging and then go into our logger and here we have also some more attributes which we can set so we can set which is the most important one the level and that we can decide which level we want to allow and here you see the whole list and basically if you allow for example arrow then what the comes after arrow or if you allow warning then after error is allowed also and what the so every time in this direction to the bottom everything is allowed so let's try this out so now i change here the level from the default one which is where both so everything is locked we can set it for example to arrow and now if you hot restart and then go here into our file again then you see you can press here multiple times it will never be locked because we have set the lock level to arrow if you press warning you also get no message but if you press the arrow then you should get this message and if you press on what the then you also get this message and yeah basically you can then decide to which level you want to lock and then he will print only on the console the levels which you selected and the levels below hello everyone thank you so much for watching this video please make sure to give it a thumbs up and subscribe to my channel here to get the latest news about flutter and see you soon bye [Applause] you
Info
Channel: HeyFlutter․com
Views: 13,456
Rating: undefined out of 5
Keywords: Flutter, Dart
Id: GUi0n9c33os
Channel Id: undefined
Length: 7min 51sec (471 seconds)
Published: Sun Sep 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.