#19 Golang - Revolutionize Your Go Logging: Master Advanced Logger Techniques

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome fellow coders today we're diving deep into the world of logging Ino we'll unravel the mysteries of fmt prints log package and discuss the common pitfalls developer face then brace yourselves for a groundbreaking approach to logging tailored to elevate your go- code to new heights let's start with the basics go provides the fmt and log packages for basic logging but they come with their limitations fmt println is great for simple messages but lacks the sophistication needed for structured logs log. print Ln while slightly better still doesn't offer the control We crave in large scale applications what do we not get from these methods these approaches lack structure leading to messy and hard to read logs developers often fall into traps such as excessive logging which can lead to Performance ISS issues and log file bloat or they might not categorize log levels appropriately leading to a flood of unimportant messages in critical logs let's see how these prints work we will print a message using fmt print Ln run the program it simply puts out the message without any other information now we shall try log print and here we can see it prints the date and time the log package provides more features like we can set the flags to get more information from the logs let's try a few Flags we set it with set Flags method l L date puts the date time L micros seconds prints the time in micros seconds this might be useful in rare scenarios and L long file prints the file path we add another print after setting the flags let's try these out here we can see how it prints the information we asked for this prints the full path of the file let's change this flag to short file this prints the relative path let's see it running and here it shows the file this looks much better let's try the Panic method this halts the normal execution of a program when an unexpected error occurs here it exits the program this is similar to raising an error these errors can be handled well there is another way to completely Hal the program it is the function fatal we can see here the program stops with exit code one now let's Implement our custom logger I like to keep it in a separate p package so let's create a new folder logger now create a new file logger Dogo package would be logger to implement this logger we will need logging levels and a method to set log level and methods to log at different levels now create a few constants for logging levels we will have three levels info warning and error next we create our logger structure it contains the logging level and three kinds of logger in it info logger warning logger and error logger we keep them separate so that we can set different settings or flags for these levels next we declare a pointer to the logger structure this will represent our logger instance now we will implement the init function this function defines the logger pointer that we are going to use to print logs in the application we do this in the init function so that we do not miss this definition and setting log levels in our application logger is a pointer to the logger struct let's set the level to info for this example now we create info logger with log. new method we want to print the logs on standard output prefix the logs with info text and let's use the standard Flags now similarly create the other two [Music] loggers let's say you want to have a different set of flags for error logs we will add the short file flag so that we know where the error is this is why we kept different instances for different log levels you might want to set the log level in your application to override the default value so let's create a simple method that accepts CS level as an argument set the level in the logger instance we still need methods to do logging let's Implement these methods three methods are needed info warning and error these methods accept a string that will be printed with appropriate information on standard output let's implement the info function we will print this log only if the logging level is less than or equal to info level if we set the logging level to warning or error there will be no logs from the info level now we use the info logger to print the message similarly let's implement the other two methods [Music] let's put this custom logger to use go to the main function we will add three different kinds of logs it prints three types of logs in the error log it prints the file as logger Dogo and it will always print that this is a mistake and is of no use please ignore this for now let's try setting the logging level to warning now we add another set of logging of each type this time the in info log is missing so our log levels work in the realm of go logging is not just a task it's an art with our custom logger you're not just writing code you're orchestrating a symphony of logs Embrace this approach and watch your go applications flourish with Clarity and precision thank you for watching don't forget to like subscribe and click that notification Bell for more coding Wizardry until next time keep coding and keep logging smartly and
Info
Channel: codeHeim
Views: 2,426
Rating: undefined out of 5
Keywords:
Id: uNhdp5fKU1E
Channel Id: undefined
Length: 11min 29sec (689 seconds)
Published: Thu Feb 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.