How to Properly Log Things in Go (Native) - STOP using fmt.Println()

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we are talking about how to properly lock things in the google programming language and why you should not use the package fonts which is the package that you typically use for your very first holo program to print out hello world what's up ninjas welcome back to the golang dojo your number one resource for all things go make sure to get your free golang cheat sheets at golangdojo.com right in this video we are talking about the log package the log package implements simple struct methods and functions for us to log information the runtime information of a program so that later we can easily go back and see what's happening behind the scenes and be able to debug if there were any issues that we encountered now you might be thinking why can't we just use the fonzer package the find package not only can print out things on the terminal but can also redirect all of these print statements into a specific file that we can use as a locker files as well there are two primary reasons the very first one is the locker package is concurrently safe compared to the font package if you are executing font package instructions across multiple processes concurrently the messages may not come out as you initially intended the second reason is if you use a dedicated locker package the locker package can attach additional information automatically to the locker messages such as time date and a file path to start using the log package we can go ahead and type in log the locker package should be imported automatically and we're just going to get started with the most basic print lines function and we will type in sub ninjas go ahead and run this program as you can see we are seeing subway ninjas as well as some additional information attached to this locker message now what if we want to change the information that's attached to this unlock message we can do data by calling the set the flag function and use a different flag let's say that we just want the dates and the file name say that we want the file name do the same message again run this program again as you can see now we are seeing only the date the file name attached to the log a message of a sub dangerous right here now besides the simple repair line function we can also call panic say panicking right here go ahead and run this program again as you can see not only is it a printing now panicking it is also calling the pan maker function so if you want to call the panic function anyways you can just use a log and that panic instead of a calling log that a print line and then calling the panic function specifically now of course because you are calling the panic function you can also call the recover functions to recover your panic however if you don't care to recover and that you want the programmer to exit right away you can also call the fatal function in the log package coming out the panic go ahead and run this program again as you can see we are exiting the uh program right away after printing out this uh this fatal mess lock message right here now if you have a user login before you know that we commonly log these messages into a locker file here we have two instructions to create this a locker file and then close the dialog file in between these are two instructions that we can go ahead and set the output file of all of these login messages to be this locker file instead and afterwards we can go ahead and try to print out another locker message and if we go ahead and run this program again we would actually not see hello world being imprinted out on the terminal right here however if we do ls we will be able to see that there is a new file being created and if we open that file we will be able to see the locker message right here instead and if we set the output to be standard out again and then i print out this line of string right here go ahead and run this program again as you can see we are seeing the locker messages on a standard out on the terminal right here instead of if we open that file again we are only seeing one line of a locker message in this file dialog right here now in production there are some common logs that we use all the time and they are info log a warning log and error log so let's go ahead and instantiate these different loggers using the new function in the locker package info logger we're just going to use a standalone and the second would be the preface we'll do info and then we need to give it the flags that we want and copy this error one arrow if we do log dot print line um this is a an info log go ahead and run this program again as you can see we are seeing the preface of info error and one attached with the additional information that we want along with the locker message right here if you want these messages to be in a locker file instead you can just swap this out to be a file rather than standard out with these purposes of the logger messages if you are logging into a file or a third-party service that's specifically designed for logging you can now filter or query by these preferences of info warning and error which is the severity of the logger messages very very conveniently now as you can see we are instantiating and maintaining three separate loggers which is an eyesore but that doesn't have to be the case that we can aggregate all three into one single logger we can create a struct of an aggregated logger and create pointers for all three of these loggers of info 1 and error add 3 methods for this aggregated struct for info 1 and error respectively to this info log right here warning log right here and the error log right here and these methods they don't really do anything besides asking for the exact same parameters as these print lines and functions are asking and passing these parameters straight down with a destructor defined we can go ahead and instantiate this aggregated logger and assign all three of these remember variables with d loggers that we've already instantiated in the main function and then go ahead and call these methods make sure to call the right method and go ahead and run this program again as you can see we are seeing the info one and error logs are being printed out for the second time and this is how you can utilize the locker package and properly a lot of things in guild if you have any questions please feel free to leave them in the comment section down below for the time being like this video subscribe to the channel if you've watched this video to this point you might as well just subscribe and like this video make sure to get your free highlight sheets at the golangdojo.com cheat sheet and i'll see you ninjas in the next video [Music] you
Info
Channel: Golang Dojo
Views: 24,799
Rating: undefined out of 5
Keywords: golang, learn golang, go lang, go language, golang tutorial, go programming, golang for beginners, golang 2022, golang log, golang logging, go log, go logging, golang package log, golang log package, golang logging package, golang logging library, go logging errors, golang error logging, golang info logging, golang logrus, golang logger, golang fmt, golang logging tutorial, go logging tutorial, golang zap logger, golang zap vs logrus, golang zap logging
Id: yF7k6PxtRU8
Channel Id: undefined
Length: 9min 59sec (599 seconds)
Published: Thu Feb 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.