#22 Golang - Structured Logging with slog

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone today we are going to explore a new way of logging slog logging provides insights into the behavior of software and helps in the identification and troubleshooting of issues Go's standard log package offers basic logging functionalities such as logging messages with timestamps however as applications grow in complexity the limitations of the default log package become apparent leading developers to seek more sophisticated logging one of the main limitations of the default log package is its lack of organized logging slog addresses this by offering first class support for structured logging making it easier to produce human and machine readable logs slog offers logging levels out of the box is log offers greater flexibility and extensibility allowing developers to customize logging Behavior to fit their application's needs this includes support for custom log levels multiple output destinations and context specific logging slog is designed with performance in mind providing a faster and more efficient logging solution than the default log package this is particularly beneficial in high through applications where logging can significantly impact performance let's see how slog works we will begin with a simple log using logging level info here we are using air it reloads the app automatically upon saving the code we discussed it in the episode 16 here it prints the timestamp log level and the message now let's see how different logging levels work we will try the error and warning levels here in the terminal it prints three different levels let's see how we can print variables from the code in slog the first argument is the me message and after that we pass key and value pairs say we want to print the goang version the key would be version and get the value from runtime using the version method here it gets printed as a key value pair still the logs are not looking organized and machine readable slog provides two handlers text and Json these handlers format the logs well let's try the text Handler for this we need to create a new logger let's create a new logger with slog do new method the first argument is a slog Handler we will create a new text Handler here it requires the io writer we will write on standard error which is our screen for now we will not use any Handler options we have a new logger now let's replace log with logger here are the prints time is printed in milliseconds here it shows different log levels the message has a key message and other key values are printed here there is a way to change the default slog logger setting we can set the default logger with the set default method let's replace logger with slog it works as before there is another logging level that we have missed the debug level let's try this out and we don't see the debug print in the console what is wrong by default the debug level is off we need to change the logging level we will play with the Handler options now let's create Handler options here we will set the level to the debug level let's put this in two lines for better visibility now the debug print appears in this way we can set any log level based on our needs next let's try the Json Handler replace the function new text Handler with new Json Handler here we can see the output is formatted as a Json these are the keys time level message and version for now we will comment out error warning and debug prints if we have too many key value pairs in a print it is hard to keep track for this purpose slog provides a way to Club the key and the value together let's use slog do string and put key version and its value as arguments to this function this signifies that this value is a string similarly slog has in for integers and other data type support let's try to add an integer value slog dotin key is random number and let's Generate random number in the value here it prints a random number slog provides another way to organize our logs further we can create a group of logs it is like nested Jason let's create a group here OS info we will put a few key value pairs related to the operating system within this group add a new slog string with key OS get the value from the runtime similarly let's add the number of CPUs and the CPU architecture in the output the OS info appears as a nested Json there could be a scenario where you want to add a key value pair to all log prints for example you might want to put the version of the app in all prints we can do so by adding attributes to the logger we can add attributes with the method with attributes on the Handler this requires a list of slog attributes we will add a slog string the key is app version let's put a random version as a value now the app version gets added to the print let's uncomment the other prints and and see if the app version shows up yes all prints have an app version in them thank you for joining me on this journey through structured logging with slog as we've seen with slog logging becomes not just a task but an art an art that enables clearer communication better problem solving and a deeper understanding of our digital Creations happy logging and may your dogs always lead you to Swift resolutions and insightful discoveries
Info
Channel: codeHeim
Views: 4,677
Rating: undefined out of 5
Keywords:
Id: KTGHn62a4tI
Channel Id: undefined
Length: 9min 34sec (574 seconds)
Published: Tue Feb 13 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.