Quick, Go check the Logs! - Go / Golang Logging Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how y'all doing back here again and we're going for about logging today whereas the thumbnail showed go on a long ride so in every other language i've used which that basically would be mostly php and javascript uh if you wanted to do logging you had to acquire some kind of third-party dependency in php this would be like monologue that's used pretty heavily and in javascript or i should say basically within node uh there's a i think a very popular package i see that gets used a lot with express called oregon um and if you want to do like uh if you want to utilize like a like a more formal logging framework like those those do exist with go and we will look at one of those as well but go actually has a logging package built into the standard packages and that's what we're going to start with by looking at uh first so to get started doing very simple logging and go you don't even have to instantiate your own logger by default there's like a kind like a base simple logger in the log package that you can use with some convenience so let's let's lock some information um you uh you would import the log package but my plugins will automatically import the stuff as i use it so i'm gonna do log dot print line i'm just gonna say this is my print message this is my log message blog message like that it's going to pull it in today and i'm going to run this oh wait you'll see you will see i have my log message here and you'll notice that there's in some information automatically uh prepended dependent yeah to the beginning of this and it's the um the date and the time now we can actually control to some extent what information is actually uh stuck at the beginning of our message here through some things called flags so if we go to the the log package you will see that uh in the the logger struct there is a function here called uh set flags it says sets the output flags for longer the flag bits are lda l time and so on if you remember what those are if you go to constants here's all of the um the flags for what information can be stuck at the beginning of a message there's by default it uses these which is the date and time like we saw but there's some other things you can stick on here you can have the micro seconds the complete like full path name to that uh file uh the short file name and also it has the line numbers here this will if you're using the daytime stuff this will use your uh utc time zones that whatever your like local time zone is and uh it says move the prefix from the beginning actually i don't think i've actually ever used this one uh so i don't actually really understand what that does so you could just try on your own i'm gonna kind of ignore that one right now so by default we know it uses this one so let's um let's add the uh the short file name for this okay so to change the flags that are on like the standard built-in logger uh there's a function you can use called log.set slabs drags like that and you just pass in the um all of the flags that you want separated by um uh burglar slashes or pipes whatever you wanna call and we're gonna do log dot i see i want l std flags because that's the standard one and i'm also going to do log dot l uh short file like that i'm typing like crap right now okay so if we run this now now you'll see there's an x the the file name of where the log was called and what uh what line number was called from is also stuck here now as well as far as i know there there's no real way to control the ordering of these things it's basically just the flags are used to basically turn off and turn on certain information there's a couple other convenience methods on here that you can use for the standard logger uh the two uh all there's three like varieties three varieties of ways to interact with the standard logger and they all also have a um uh two two alternatives of it and they're all kind of based off like the the printf stuff there's so there's log dot print there's print line and printf and these all by default go out to the standard error output stream of whatever whatever you're running this program but there's two other versions of this there's log dot fatal and log.panic and they do the same thing fundamentally as print but they also do something extra after they run and you want to see what happens so instead of doing uh to do print line we're going to do a fatal and if i run this i'm not just going to notice any difference so you'll notice it exits that as one so what happens is when you call log.fatal it logs this message right here and then calls the os dot exit with the exit code of one meaning your program like exited unexpected and as you might guess the uh last one is called panic and you can probably guess what that does um if we run this you will see that it printed out the message and then you'll notice that the runtime panicked which makes sense so that's basically what it did it logged this message and then it called panic to make to run make the runtime panic and crash in case you know your your program just some really weird edge case happens that should never happen that is going to just completely screw everything up you're just saying like abort stop everything that's happening kill the system now uh so this is this is fine that we have um yeah we're interacting with the standard logger and we're writing stuff out the standard error blah blah blah it'd be more useful if we could store this information somewhere so that we can access it later and to do that you can actually change where the standard logger is writing out its data to within a function called set output now set output the first the argument it takes is just anything that implements i think it's like io writer so this could be anything that you've created that that implements io writer but a very common and useful thing to do is to maybe just write it out to a file so let's write it out to a file so i'm gonna do file error equals os dot open file i'm just gonna call it um logs dot txt now we need to pass in the flags on to set how we're opening this file so i'm going to do os dot o append oso create to create the file if it doesn't exist then os [Music] uh er only what i think is was it right only i think that's right then for the file permissions we'll say 666 basis means i don't care uh of course we're going to check for errors like usual we're gonna do if error does not equal nil let's go log.fatal the error and then since we have our file open now we're gonna do log dot set out put to the file and now if i run this yes we didn't see anything right um and but if i cats if i do ll you'll see now there's a logs.txt file and if we cat logs.txt there is our log message right there so now we can um interact with the standard logger in a few different ways and we can write it out to a file but maybe there's some you want to do a couple have us some different loggers depending on some conditions uh it's not uncommon if you're uh used to working with like logging frameworks that maybe you have like a logger for warnings and one for info and one for errors and stuff like that you can actually create your own uh instances of the uh logger from the log package so let's do that we're going to go up here and i'm going to create some vars up here that i'm going to uh initialize later so we're going to have a warning logger is of type a pointer to logger log.logger and that's because the uh the new function in the log package returns a pointer to the newly created log logger and we're gonna have an info one of the same type and then an error logger of this type blogger like that okay so let's uh let's let's uh set this up now so that we're um having this stuff set up by default some would uh define an initial in the function here if you've never seen an ended function and go basically if it's kinda has a special meaning like how maine does where main is the entry point to a go program if init is defined before main is called and it will be called basically and this is typically a lot of times if you if there's some like setup that needs to be done in a program you'll see them stick everything in it so we're going to move this file being opened here to uh here like this and we're going to check our error and move that up here as well and then finally we're going to do uh info logger equals log dot new and it takes in three arguments and it for first one is the uh where it's writing out it's in its messages to so anything that implements uh io.writer so we're going to pass in our file and then the i believe it's the the prefix what info will go before the other prefix that's all manually generated so i'm going to say this is the info logger so we're going to say info and then we have to pass in our flags so i'm going to say log.l was it std right inner flags yeah and i'm going to say log dot l short name profile okay just like that and then these are going to be fairly similar uh see warning logger new file uh warning and the same flags lst flags log dot l short file then last is the error logger [Applause] log dot new file uh error log.lstd flags short file so we have our log set up now so let's let's print some stuff out to them now let's get rid of this stuff and we're gonna do info logger dot print line uh this is this is some info and then maybe we'll do warning logger print line this is probably important because you know typically if you're printing out warning it's probably something that shouldn't be happening and you want to check and then finally error logger print line uh something went wrong like that now if i want to remove that first logs.txt file that we had and we're going to run run this so we don't see anything of course because we set all these two right out to a file and if i cut out that file again you will see that we have the text that we uh put here before the information that's automatically generated from the flags and then the message so basically using the standard logging package you can essentially create something kind of akin to that you normally see with a logging framework where you have like you know info warning debug error fatal stuff like that uh so that's um that's all you that's that's pretty much all there is to the uh the standard logging library but now i'm actually going to actually use a uh a an actual logging framework that is available to using go if i go back over here we're going to use this one called logaris uh this is the one that somebody mentioned and i just kind of poked around with it and seemed like pretty good option to use i don't see no reason why not to so since we're going to use this first thing we need to do is we need to admit a go module so i can use it within this uh within this directory uh by the way the the module name i'm initializing this to this this code will be in like in a git repo called that um so if you want to reference this code layer you can just go look for it there so i'm going to go mod init and get gitlab.com 435 and go logging example so now i'm going to input make sure i import that uh dependency that i pulled in so i can actually use it s-i r-u-p-s-e-n monograss and i'm going to aliase it as log so it's you know kind of more natural to use it and you know just just to test it we're going to do log dot info let's say this is some info just like that and if i do go run log risk go [Music] uh you'll see info this is some info uh what is the accident i'm not sure what the numbers mean longer hey we just do another info we're gonna check this is some more info and run that okay i'm not really actually sure what that the numbers next to the accidents uh but yeah we can it does color code the info too which is interesting if i do error let's say this is an error oh my god something went part terribly horribly wrong yeah and you know this is color coded a little differently to indicate that it's more serious but the other really super neat feature this had is it has some formatters built into it one of the interesting ones is that you can have your log output be formatted to json which is actually super useful so if i go up here and we're gonna do log.set formatter and there is a what is it it's called json formatter there we go that's what i'm looking for so like that we're going to create an instance that struck to say that's the format that we want to use and then we have to specify some additional fields that this login has or as a log with fields uh say log.fields uh see i want to say like day four and time bar yeah i don't really have any like good information to put here so i'm just kind of playing stuff that's wrong and so now if i should do this info this is a json message this first one should have some additional information here yeah i see this when you with with fields you're basically kind of like inserting in like arbitrary information that's not normally there but you'll see now the info like what level it was and what the message was the time all this information that normally was in um this kind of just standard out like this now it's in this json string which is depending on how like how you're using things this could be much more useful because you know it's pretty common to use um certain uh storage services like i don't know like or uh if you're kind of this is usually considered time series data so maybe you want to stick this in like influx db or something these are all uh databases that tend to leverage heavily things that are in like json formats this would be super useful for that um that's that's really the only real main difference that i noticed that this that this particular library had that was super useful um i know you can also create your own formatters if you're using this library uh this loggers framework uh it's actually there is something here somewhere i just gotta find it uh there's somewhere in this documentation here there's information about uh creating your own formatters you'd have to just find it there's a um there's an interface there here it is uh yeah you can define your own formatter by implementing the formatter interface requiring a format method uh format takes a in a an argument of entry and it has some fields and then basically you just have to say like how you're um like serializing the data basically and there's a couple other third party formatters already here uh one's for logstash which is that's that's very convenient um fluentd i never use that but i know yeah it's used with kubernetes uh yeah and then by default it only has text and json but there's other formatters here you can uh so that's all that's all i got for you today about logging it's uh it's pretty straightforward even though that the the standard logger doesn't have like a whole lot to it depending on what you're using or what you need it for that could just be all you really need if you liked the video be sure to share it in case someone else would find it useful if you have any questions or have any comments about uh what you know how you find this how you may use logging in your system be sure to leave a comment down below because i'm curious about uh maybe if there's some other logging frameworks this is just like the first one that i was made aware of uh uh if you want to uh follow me on other platforms i should have some links wherever this thing is uh if you want to know when i post more tutorial videos be sure to like or follow subscribe wherever this play whatever the vernacular is for wherever you're watching this with that y'all come on back now i'll see you next time [Music] you
Info
Channel: Donald Feury
Views: 14,006
Rating: undefined out of 5
Keywords: golang logging, go logrus, golang logger, go logging, go logger, golang log file example, donald feury golang, donald feury, golang logrus, programming, feurious, golang tutorial, golang, go, go programming, golang programming, programming tutorial, go programming language, go programming language tutorial, go programming tutorial, software development, go tutorial, tutorial golang, Software development for beginners, coding tutorials, golang course, go language, learn go
Id: p45_9nOpD4k
Channel Id: undefined
Length: 22min 2sec (1322 seconds)
Published: Mon Jul 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.