 All right, so We're going to get together to talk about Logging today, so welcome to another Microsoft soundbites, and I'm Stratus I'm Gez and We're just gonna jump right in here because I don't think many people need much introduction to logging and why we're gonna do it So we're just gonna jump right into the command line today let's do it all right, we We've got our terminal up and we are already looks like we're already in the ice cream shop skill directory, so let's Jump into the init file where our skill is defined Here we have as you can see the the very basic Ice cream shop skill that we that we've created in previous videos But what we want to do today is is look at this logging and You know, how can we use logging to? a better understand what our skill is doing and B so that we can let users know what's going on Particularly, you know, if something hasn't gone perfectly to plan so So maybe to to Give them the easiest possible example. Let's um, let's use the initialize method. So We'll add a new method And if you if you're pretty new to skill development Just as a quick primer the init file It's sorry the init method is gonna get called as the skill is being constructed Whereas initialize will wait until the skill has has completely Completely initialized and then this will run before the rest of the skill So this is a good place for if you are doing anything with skill settings, then you want to do that in here or anything with intent handlers or Anything that needs to interact with other things inside your skill Such as login credentials for example if you're connecting to an outside I don't know for example if you're making if your skill makes a call against the API that has authentication behind it you might set up your Authentication handlers here Yeah, perfect example. Yeah Anyway, so it also because it runs when the skill first loads It's a good place for us to to see this logging in action So so let's do an info level log. So the the logger It's it's just you know a regular python logger But it's available on the micro skill class. So we can say self Dot log dot info Or dot the level of logging that we want And so here we can say that the skill has loaded beautiful And so if we save that and we jump over to the micro of CLI We should see Yeah, about halfway up ice cream shop. The skill has loaded has our great So there we can see that it's working we can see the hot reloading is obviously working because you know we saved the file and and micro for reloaded the skill and Here we've got you know our first bit of text coming from a skill and showing in the logs So it'll show up here in the log output and it'll also be in the in the in the skills dot log You know file system Sorry about that my craft so What is it that's getting logged here? So if we if we look at this line, you know bit by bit we can see that it starts with a time stamp When it was generated it's got a level So in this case we did self log info. So it's an info level log The then there's the process ID Most of the time you probably won't care about that, but it could be useful if you're doing some some low like low-level checking Then we have the class of the skill that's emitting the log. So you'll see that that matches our class name and Then we've got the text And that's it. So For those of us that are old school that are just used to making print statements Why would you want to use the logging over print or vice-versa is is print still a valid thing to use? inside of your skill for debugging purposes Yes print print is a completely valid to use while you're doing development It's not something you should be leaving in there when you Distribute that more broadly But you can absolutely just use print when you're doing your own development purposes You know for something like my croft print very quickly You know Becomes Isn't isn't useful because you know as you can see like there's a lot of different stuff that's going on There's a lot of different systems that are all Interacting to make my croft do what it does And so we really need to have an understanding of what's generating all of these messages And you know where they're coming from and and when they happened and and all of that sort of thing so Can I show you an example of why I I asked the question? Yeah, totally. So when I do things I will actually do something like this and Then have my message here Mmm, right and the reason for this is Because when I'm watching this to me when I'm scanning for when I'm scanning this right? Assume that I didn't catch this or there's a lot of output that's happening or you know It doesn't happen in the initialized function I'm looking for something and because the text is all purple or you know if you're like you're looking through the log It's way easier in my mind to have something that that is unlikely to be omitted by any Thing else in order to like scan through it like if I scan through this this cause like There's just a ton of white space here to the end where everything else like this the text wraps around and so It's it's something that I do but I was always like should I be doing that? I don't know I obviously remove it when when I'm done with it, but Yeah, it's something that I you know I've seen I'm pretty sure I've seen everyone You know employees and in the community Regularly using that for their own development. So it's totally fine to do that But yeah, it's just best to well We definitely want to pull that out before it gets you know submitted to the marketplace or anything But we can also have a look a little bit later at how we can make it a bit easier to see how logs You know as a part from everything else But for now, we've we've got an info level log And if if you're familiar at all with the with you know, Python's normal logging Class then Then you'll know that there's some you know, there are a series of Levels or types of logging. So here we've got an info level. That's just going to describe The normal behavior so it should be that you know things operating as expected But for some reason we want to provide some information To To the user or more so to someone who's looking at you know, what's the system doing and why is it doing it? If there was Something that You know, the next level up is it is a warning so we can go self log warning and This would generally be used Is it warning or warn Warning, I believe So this would be used if you know the skills able to continue but perhaps, you know, you have a Method in your skill that you've exposed by the skill API But you're now deprecating that and changing it slightly or something like that So it's it's not, you know, a critical error or anything like that But but it's something that you want to you want to warn people about and so so we can use warning So there you can see the the warning that we just omitted there Yeah And so then the next the next sort of level is an error. So that's you know, something's actually You know, it's a it's more of a serious problem We've been unable to perform some kind of function Then we can do self log error Exactly And here we you know, we can see it's it's in bright red because it's it's clearly something that's really going wrong I Do want to change warning to be a little bit more obvious, but it's just normal for now So that's an error And then the next one would be an exception. So that's where You know, particularly when we're actually catching the exception It is a type of it's actually a subtype of the error log But it provides, you know, the standard stack trace Alongside that message so that you don't just sort of tell people something's broken You can actually point to them, you know here and here's where it is broken but You can see at the moment we get none type none because it hasn't received a stack trace. So Let's generate and get an actual error in our skill We could do something like Let's just try and access a variable that we haven't haven't defined perfect Awesome Great that is much more helpful. So not only do we do we see that the code broke But we can actually see, you know, what type of Exception that was that we got a name error and exactly where it happened. So it's on line 12 of a knit top pie So much more helpful for you as a developer much more helpful for your users Or if people are, you know Coming to us for support. It also helps us to track down what's going on so that's an exception and the next level did I was gonna ask It normally I would have thought that it would go to critical Does does my craft make use of the critical logging function? You can you can do critical so critical Haven't actually to be honest. I haven't seen many people use it Or anyone use it that I can think of but I mean I generally think of critical as Things are so broken that the skill cannot continue So But yeah, we can we can put one of those in here as well So the same thing you just go self log critical Goodbye cruel world Love it, I think you might need one more space at the start Oh, no because I put it outside. Oh, you're right. You're right. Oh Python you lovable step Indeed Cool critical But again, we need to update the the coloring on that so that it you know, it doesn't just fall into the background But yeah, so you can do a critical a critical message So there's there's what five types of logs and there's one more So these are the ones that you're generally going to be using particularly during development but Particularly if you if you you know if you're watching logs and you're seeing, you know huge amounts of logs go past You probably realize that you don't End users don't really need to know, you know, absolutely everything that's going on in your skill There's a lot of information that is only really helpful while we're debugging And so that's when we use the self log dot debug level Yeah, we don't normally need it for everyday usage, but it could be useful at some point And if we jump back to the CLI We will see that it is not there It doesn't show up because by default mycroft doesn't log the debug level messages Because there's just there's so much of them That it would be a bit silly We can turn them on though. If we if we use a Colon command in here, we can say log level debug. I think we might need I don't know that worked great And then if we reloaded the skill If we go back and hit save again Yeah, lots of fun stuff Yeah, so Yeah, there we saw for a brief moment that there was there was our debug message and now we're getting a whole bunch of other stuff Yeah, and so this is why we don't don't have debug on by default But we can turn it on and it'll be in our log files as well This highlights, you know part of that problem that we were talking about before and why people might want to use print statements because You know, there's so much going on here that like how do we how can we possibly you know keep track of what our skills doing? And here's where some of the other command colon commands come in so Let's do colon help for a second. Just so we can you can see what's available to us If we look at It's on the next page Yeah, here we go. So we've got got some filter and find Commands available. So there's there's also that log level there But filter and find other things that that we're interested in so filtering the log is going to remove Remove logs that match that string And then we can we can add and remove those filters or clear and reset them and show what filters exist But probably a more useful one if you if you're developing a skill is the find command So if we do if we go back Hit any key to continue and we do colon find And we'll use the the class name because that's quite unique so we can say ice cream with a capital I Boom and there we have only our logs for our skill So this is this is now much easier to see, you know, what our skill is doing and and You know without without being bothered by just endless amounts of text-to-speech and audio service and You know enclosure code And you know all the other things that are that are doing their logging for for important reasons But we can just ignore them for a second and just focus in on on our skill So it's a really really useful powerful feature as a developer And does that only last as long as your instance of the CLI is open? This is only operating on the CLI so so the the log file will continue to be receiving Logs for everything This is only changing what is displayed here in this window Right. So if I exited and came back I'd have to reset up the find Oh, yes. Yeah, that's right. Yeah Cool so I think that's mostly it One other one other thing is that so far we've been using the logger That's available on the Minecraft skill class Which is gonna work, you know, particularly for simple skills gonna gonna work most of the time But what if we have a function that's that's outside of our skill, you know, because it's You know, we just have a pure function doesn't need to have any Any access to anything in the skill. So we might go above the class and just Random little function. Yeah And so we've got this function But we also sometimes we want to log from that as well, right? And so obviously we can't do self log because Self in this instance is is the function rather than rather than the micro skill class But we can import the logger from mycroft util So if we if we jump up above into our import area Mycroft util Oh, sorry, we can do from from mycroft util might be might be easier And then at the end import all caps log That's it And so now in our new function We can use it in the same way that we do everything else And so to make that show up. We'll just need to call new function from our From our skill because currently the code's not executed anywhere All right, so before we do this we probably want to turn off that debug logging just so we can clean this up a little bit and now let's go and and trigger our skill so that we'll See this log message show up There it is there tells you the function and then I assume is this the line number. Yeah, yeah line number Yeah But because it hasn't come from the ice cream shop class It's it's showing that it's still come from the skill, but it hasn't come from the ice cream shop class. So So that's why I wouldn't show up with our with our find applied Because we we had the find applied very specifically to the ice cream shop class name But you know, we could we could we could do a find ice cream for example, and then that should Or at least find CE cream would certainly work To show you both of those Perfect great So that's logging Anything else complex that you want to talk about or is it pretty much just that straightforward? It really is that straightforward You know, I think When to use the different levels of logs is always slightly debated by different developers You know We try and try and provide some guidance, but in the end it's really up to you You know, if if there is a skill that's being Submitted to the marketplace and it's full of print statements, then then we're gonna ask you to remove those if you log info level messages, you know 50 times a second, then we're gonna ask you to remove those Or at least at least change them to debug, you know, just so that it doesn't fill up a devices log files with messages messages but But mostly yeah, that's that's all you need to know and then you can use these to To either yeah report when things are going wrong or just help to understand, you know What's going on in your skill at different points in time? All right, so that was logging it takes a little bit longer to explain Logging even though we didn't do a whole lot of coding today It was mostly just you know, here's what the log level is and here's what it looks like on the CLI and You know, we just kind of walk through that process So it was I think it was pretty straightforward But you know, you can always come and find us or members of the community on the the chat up mycroft.ai We're always around to help and I do kind of scour the messages on YouTube if we ever get left and I know that Chris does as well. So you can also ask questions there and we can Attempt to help you out or at least direct you to the documentation. But with that Chris, is there anything else you want to talk about here? No, no, I think that's it. I think next time we'll we'll start getting into how we can make mycroft talk and and start to do some cool stuff in our skill so Good kid then until next time Until next time