This video demonstrates how to create a logger in Rust that handles INFO, DEBUG, WARNING, and ERROR logs, with a focus on making it non-blocking using channels. The instructor shows how to properly handle file operations, manage errors using Result types instead of unwrap(), and implement thread-safe logging using Mutex. The session covers practical aspects of Rust programming including file I/O, error propagation, and concurrent programming patterns, providing beginners with foundational knowledge for building robust logging systems.
Building a Non-Blocking Logger in Rust: Channel-Based Logging
Added:Hey, are we live?
I think so.
Uh, let me see.
Okay, this is good.
Mhm.
Uh [snorts] maybe.
Okay, this is let's do this.
Okay, we are here.
And let me see one thing.
Can I just scroll it over here?
I think this is good.
Uh let me just go here.
Okay, this is on the broader screen.
This is for the mobile, right?
Uh this thing.
Should I? Should I? Should I? Should I?
Should I or not?
Maybe I can just shift it over here.
Let's see how it goes.
[snorts] Sorry.
[snorts] >> [snorts] >> All right. So, hello. Hello. Hello.
What's up?
How you doing?
How you doing?
Uh, just give me a moment.
Yep, it just went away.
Okay. Now, uh let's start. [snorts] So, if you are here, uh you must see the title of part two of whatever we were doing earlier, creating a logger. Uh this time uh what we'll do we'll try to use channel to you know make it non-blocking and we'll try to handle or remove those un like unwraps that we have written to a more something uh more you know clean way [snorts] so that we can handle the errors and we'll generate an error as well for that to see if we are like getting any shutdowns of program or not.
Okay, [snorts] I hope I am audable.
Let me start the poll.
>> [snorts] >> Amazing.
[snorts] So guys, we have a poll running down.
Make sure you hit that thing.
[snorts] Off for the polls.
Okay. Uh we'll start now.
[snorts] Uh before that, let me see if everything is going well on the OBS side.
Okay.
So last thing that we did was we wrote all these stuff [snorts] and we generated few things so that we can you know see the logs and all.
Let me just keep an eye on chat as well.
Okay.
Uh now [snorts] what we are going to do is uh so here what we did uh we propagated the error up right from here.
Probably we can do something else.
[snorts] We can create another helper function where uh we can pass in the file name and based on the type we can create this thing info log or something right so this way this would be very clean and but how do I get these things over here.
Yeah. Uh but that would be I have to do that as well like multiple times.
Maybe create uh no uh maybe let's start. [snorts] I'm just uh hitting here.
>> [clears throat] >> So I'll have the the log level as log levels, right? What else do we need? Nothing.
Yeah, we only need uh love levels. And do we need this?
Uh here what I'll get? I'll return uh probably I can return mutx uh info file but that info file would be a result of a file.
Uh can I do something like this?
[snorts] Here I can just say file probably it's not uh it could be an error as well.
We can and from here maybe based on the log levels we can return this thing.
What do you think?
[snorts] Yeah, there's pole running down there.
So, make sure you hit that thing.
[sighs] >> [snorts] [snorts] >> I was just thinking that if I return this thing, you know, so if I return this thing, I can directly say this thing over here.
So I can, you know, logger info and all.
[snorts] But I need to check uh info file. So if I have open over here returns a result of file and error. So I think yeah so you see the return return type over here of open is result file error and that's what we will return right here.
So [clears throat] [snorts] if it's uh info, we can uh then open options.
new, right?
Read true.
Uh uh what if I declare something over here and then you know do that? Can I do that?
Let but I need separate one right if I want to create this Okay.
I want to create it.
uh and open I'll do info uh uh this thing I can also you know what does it expect uh string slice right maybe I can just give him a Let's say file birth as string slice I can just say then why I need a log file over here I don't need this right it's a oneliner now so I I think I hope I'm right. Uh I hope so.
Let's not do this.
I can simply say file path end.
Now [snorts] this returns something uh a result right so I can say I can wrap it in mutx here I'll say mutx okay [snorts] now I have some issue here uh Type allocation. I cannot see it on full screen. Uh you know what? Can I do full screen? Uh I think uh going on mobile view is quite you know I cannot see the [snorts] screen much. Uh let me try it with my OBS here. So here is my OBS. You can see probably uh this is what I'm doing right. This is a single uh vertical layout. Maybe I can just switch it to complete desktop layout.
That be gate.
Can I do that uh while streaming?
No sir, I cannot do that.
Uh let's leave it that way.
[sighs] I'll have to maybe I can just [snorts] open it like full screen.
Okay. So here I can see the error now.
As you can see, can you see it now?
No.
Let me just stretch it again.
Yeah. See it says type aliases uh take one generic argument but two are uh were supplied like that.
So, [snorts] so, so [snorts] if I remove this.
Okay. Uh, okay. Now, this is done. Uh, get file instance. So instead of now doing this thing open file in all of the cases I can say uh self dot get file instance over here.
What the heck?
Get I'm not getting this.
Okay. Okay. Okay. I think I have to blow here.
I can What is this?
self.
So I have this thing uh get file instance I can do that. So instead of passing this thing I don't need that. I can make it associated.
So I'll remove this thing.
Uh I need a file path right? So I'll simply say debug dot log.
Cool.
Now [snorts] it says uh yeah now I can simply just shorthand it.
So let's see.
Uh it says expectract sync mutx found found uh result. Okay, it needs a file but I found this thing. [snorts] So probably uh I need to have can do something like this.
uh file.
I hope you get it.
A little bit too low to understand what you're saying. Oh, actually I'm talking too slow. Just like uh talking to myself here. Is it okay now?
Let me check. Hey, hey, hey. Is it okay now?
The volume I can see is hitting the yellow bar. So I think it is [snorts] pretty audible.
Yep.
Thanks for pointing that out.
Uh error here. So probably we can just uh see error occurred here.
Uh mismatched type expected this Got that.
Let me see. Just open it. Found enum. Uh match types expected error.
Found this.
[snorts] Okay.
[snorts] Uh what is this?
So it needs this thing right from the struct I declared mutex of file.
So I'll be getting this thing uh but this is only returning a result of this thing.
Right.
So probably maybe maybe maybe this is completely uh maybe we can do something like if I do this uh what is this?
Okay.
But maybe we can say okay uh debug file [snorts] equals self uh get file instance and debug file.
[snorts] And then maybe I can just say >> [clears throat] >> cannot find value debug file on the scope.
What do I use this thing? So let me just [snorts] found enum this uh expected strruct this.
[snorts] How can we uh so it returns uh this thing. I need to have a mutx of file here right. So it returns some mutx file.
So instead of wrapping it over here, maybe I can just simply do this over here.
Let's just not do that because and maybe I can then uh it returns result of file and error. Now so I can do it over here.
So I'll say let tense.
[sighs] I mean, uh, does that make sense?
Does that make sense?
Uh, match arms of compatible type expected foil.
And then here I can uh [snorts] uh this this is found to be of type file. Okay.
Uh hair.
[snorts] >> [sighs] [snorts] [snorts] [snorts] >> expected file.
Uh no let's not go here. Uh this has to be handled separately. But over here guys can you uh figure out what is happening here. So match option it will basically it's a result right? Open will return a result of file and an error. So if it's a file I'll return that wrap it in mutx and return from the function. But on the error uh error we have to do something uh [snorts] so maybe maybe let's do this thing.
Let's open something over here. Uh so I'll say >> [clears throat] [snorts] [snorts] >> So this it returns from the function right? if this thing uh but instead I want to log that error right so we created this thing onto a separate function uh because this open will return a result of this type this you know an error and something so uh yeah this is what it's doing this is what we did right open okay file now we have a file and we did that the exact same thing right where is uh this thing right so [snorts] if we have a file I'll return it and then wrap it in mutx right but in case of error it just using a match statement over here uh if it's not find you could try creating a file here okay but it is expected to return a file [snorts] right uh here is panic for other errors It's a permission denied but but I need to send the file uh right the file file already exists.
[snorts] >> [snorts] >> it is creating a new file and returning it and on the other cases it is just panicking right so maybe we can do that as well if you are handling the thing but this could be an error here the file result is open file handling And again it's using a match here because it's returning a result of file probably.
Uh maybe we can uh do this thing. Is this on your GitHub? Uh not right now.
I forgot to push [snorts] Maybe I can Let me just do that.
Uhoh.
[snorts] Uh, let's not put this thing.
So, so, so, so this is >> [snorts] >> Yeah, give me a minute. I will just upload it.
[sighs] >> [snorts] [snorts] >> Can you see it now?
[snorts] [snorts] >> [snorts] [snorts] [snorts] [snorts] [snorts] >> It's late here. I'll check tomorrow if you put it up there. Okay. Okay. Sure.
Sure. Sure. Yep.
[snorts] >> [snorts] [snorts] [snorts] [snorts] [snorts] [snorts] [snorts] >> Anyways, [snorts] [snorts] [snorts] uh [sighs] yeah. [snorts] So maybe we can do this based on the error kind. We can do this or or over here on this thing we can either create a new file because the file could not be there right.
So maybe we can do that or [snorts] or or we can simply do this. So let me just maybe All right. So scroll it up.
>> [snorts] >> So [snorts] file name we can Okay. Uh no no file create.
>> [snorts] >> Maybe we can return open options here again. Then uh we'll be in loop there.
[snorts] It says consider using uh result expect.
[snorts] >> [snorts] [snorts] [snorts] >> So this okay but I think we should handle it that way that we were doing earlier.
Uh you know using match statement.
So this is it and here it is I'm getting this thing right.
It is returning either this and it will return a file. [snorts] So this will probably I don't have to now do this thing because I'll get Cool.
Now instead of this maybe I can directly do this.
No, that is it. Now [snorts] let's remove all this thing and use this uh >> [snorts] >> Maybe we can pass it all the file names on the new functions as well. Uh you know what I mean?
So instead of getting it like manually typing it here we can you know I mean we have to manually type it somewhere but not here on the new when we initiate this thing. Uh self dot so we'll have errors.log log.
Right. So, it's pretty clean now.
What is this?
Info.log.
[snorts] Right now I can simply just do this.
[snorts] So the new is now very concise right instead of all those writing uh we have written it over here based on the file path it will generate this thing correct [snorts] now yeah I was saying that uh you know on the new over here we can get the file name like this [snorts] uh we can get something like info Debug [clears throat] warning.
Maybe [snorts] I don't know if these are maybe I can get something I can Okay.
Right. And over here I can just pass in info log error log [snorts] and debug log right uh now that [snorts] it's uh written in new guys only four votes we have till now uh so if you have any questions feel free to come on chat feel free to visit chat Don't take it. Okay. Uh new here. Right.
So this is good. Now instead of multiple lines over here, we just wrote this uh [snorts] uh format logs is written over here. And this this this this.
Okay.
uh unwrap.
[snorts] Maybe instead of doing this we should propagate it on top. We should propagate the error on top and then handle there.
What do you think? [snorts] Am I right? Am I right?
Hm.
Uh, what if I instead of it only used where uh results is used, right? So, but it's not returning anything. So we can return a result from here and then handle those errors on the main function from the main file that we are calling. So this way if we have any error over here the things we are calling from there we can handle the errors over there.
[snorts] Right? Uh so what do we want here? uh we want result uh so I don't need anything if it's uh okay but for error I need uh error which [snorts] okay uh result this from this. Okay, we need to do an okay over here because this unwrap we need to figure out what unwrap returns or what this thing returns, right?
What it returns is an result of this an error. Error is considered an error if not all bytes returned to the IU errors.
Okay.
>> [snorts] >> So this is it result and okay it will return an error. So it's an IO result.
Okay. And Uh, what's up?
Hello.
>> [snorts] >> Hello. Hello.
What's your name, buddy?
[snorts] >> [snorts] >> Yes, I can see that. That's your handle.
[snorts] [sighs] [clears throat] >> [snorts] [snorts] [snorts] >> phone micro file.
This is gone, right?
Uh [snorts] guys, how do we like uh return it?
Uh no no.
[snorts] Okay.
[snorts] So the thing is if I will return it from here the operation is already done right. So it will like flush and all like this file dot flush.
So [snorts] can say error in writing all byes to file.
I can do that.
[snorts] Yeah.
Uh the same goes for here as well.
Maybe I can.
[snorts] Yes. Yes. Yes.
[snorts] Mayonnaise.
again the same message. Let me just copy it.
video format.
[snorts] Uh next time maybe getting uh so there is a new feature on YouTube studio where uh like I used to do earlier I used to you know For the mobile view, I used to create a vertical layout. That's what I'm doing right now.
But, uh, YouTube Studio has a new feature feature that allows you to stream on both ways. It's a new feature.
So, I'll try it on in my next stream. So I'll have to do is I have to stream on the desktop view and it will automatically clip the uh clip uh for vertical layout for me.
[snorts] Let me just show you. So here is my another screen that has this thing over here chat window and all. So if I go over here uh how do I go this maybe I can do this. Yeah you see there is a vertical layout over here as well.
So this is the new feature.
Go there. Go there.
Yeah. [sighs] [snorts] Okay.
>> [snorts] >> How is the weather? [sighs] It is bad.
What kind of coding is What kind of coding is this?
[snorts] What kind of coding is this? This is Rust.
Nothing fancy, just some stupid stuff.
[snorts] >> [sighs] >> Yeah.
Are you into coding?
Is it Java, Python, C++? So, yeah, it's Rust.
Rust.
>> [snorts] >> Okay. So, I hope this is not an error. Maybe let's remove this thing error kind as well.
And let's remove this initial layout as well.
So we are good. We have some issue on main file. Why is that?
Are you a computer? I am working as a software engineer and I am a bachelor of computer. I did bachelor's in computer science.
So it takes this thing, right? So I'll have to do this info dot log [sighs] uh debug.log log warning.
[snorts] Cool. Where did you complete your degree?
I have two degrees basically. So one I have it from uh from college UTU uh it's a university second I have BCA as well it's open.
[snorts] Is there snow falling on your side, bro?
No.
Slow snow. Uh [snorts] [snorts] >> [cough] [clears throat] >> through coding.
>> [sighs] >> I think uh we should always skill up. So I'm [snorts] not sure what's your age but if you are a kid right now or you know learning then it's a very wrong question in this era.
uh when I was a kid not kid when I just saw first time computer then I thought of hacking something Wi-Fi is a different thing but yes so there are tools that you can use to hack Wi-Fi so if you [snorts] uh go in through Kali Linux there is a dro of this so uh on their website you can see the all the certifications to be an ethical hacker.
So, they won't just teach you how to hack Wi-Fi or, you know, crack any password. They'll they'll tell you how to do things ethically.
So, maybe you should give it a try there.
[snorts] >> [snorts] >> Linux can't be fine in Google. What?
[snorts] [sighs] What the hell?
>> [snorts] [snorts] >> Okay. [snorts] [sighs] So, okay. So, this is the blocking way to do. So how to make it non-blocking is we can use channels over here and uh uh we can use channels over here. For that maybe we can create you know these are the things.
So for that to show I [snorts] did a tour of bro. Very beautiful place. Merry on your side.
Yeah. Yes. Nice. Nice. [snorts] I have been to both the places uh like many times.
Uh so it's beautiful.
>> [snorts] [snorts] >> So let's create another file over here.
Uh let's name it uh n blocking ops.
RS [snorts] is hacking a crime?
Yes.
If you do it without consent, it's a crime.
Uh [sighs] [clears throat] [snorts] but uh you can you know help other people's by doing this crime that is called ethical crime ethical hacking. [snorts] So industries, organizations and other some you know that are uh digitally there online they take help of ethical hackers to make their network or their system secure.
[sighs] What do you think where I am?
I'm from India.
>> [snorts] >> foundity ownable and basically Wi-Fi driver. Okay, cool. Now we are talking and what was that? [snorts] >> [snorts] [sighs] >> uh uh yeah.
So we can have this thing channel uh clone we can thread dotspawn here.
It's the first thread and from here we can we can do something like this right we'll create a channel this channel will have all these things transmitter and a receiver.
So as soon as any any error occurs it will send it will you know transmit a log. Just give me a minute.
Is there God according to you?
Is there God according to your computer knowledge?
[snorts] I'm not sure.
What do you think?
[sighs] [clears throat] >> [snorts] [snorts] [sighs] [sighs] [snorts] [snorts] [snorts] [clears throat] [snorts] >> Just give me a minute, guys. I'll be back. [sighs] Repausing my camera. I need to go somewhere. I need to relieve myself.
I am back.
Uh, what's that?
Extreme bit rate is lower.
[sighs] [snorts] What is this? What is this?
I don't think the bit is lower.
>> [sighs] [snorts] [sighs] [clears throat] [sighs] >> Uh C++ why I mean uh why you need to which are you a C++ dev.
[snorts] I don't need to switch just for hype.
[clears throat] Oh gosh.
[sighs] Green, green, green, green, green.
>> [snorts] [snorts] [snorts] >> just curious to learn. I have been hearing a lot about rest lately. So if you are from C or C++ background, it would be fairly easy for you to move along. But if you uh [snorts] >> [snorts] [snorts] [snorts] >> Okay.
Yeah. So, we have to uh where were we?
We can use this thing uh channels to make our logger uh nonb blocking. [snorts] So how we are going to do this? Uh we have created a separate file for that right.
So we have a blocking code on a separate file and a non-blocking code on a separate file. The time is uh should I do this? [snorts] [snorts] I have to eventually leave the stream in half an hour only.
So I'm not sure should I do this or no?
Okay. So I think I will close the stream now. [snorts] Uh we can do it next time.
So I have to go somewhere and I like after half an hour. So thank you very much for coming on the stream and I'll see you next time.
Bye guys.
Up Next

Understanding the SHA-1 Secure Hashing Algorithm in Cryptography
@Computerphile
1.3M views•2017-04-11

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science







![[SLOWER VERSION] A Firehose of Rust, for busy people who know some C++](https://i.ytimg.com/vi_webp/FSyfZVuD32Y/maxresdefault.webp)






























