Software Architecture in Golang: Structured Logging using slog (Observability)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is Mario welcome to another software architecturing or video in today's episode I'm sharing with you how to implement the structure login using the new s-log package to improve off survivability so what is a structure logging a structure login is a practice that indicates the implementation of a consistent and predetermined message format for application log interest this message format is typically represented as a collection of key value pairs each one of those log entries is captured and collected by a log aggregation software too allowing searching for particular values by filtering on context of a particular key for example a concrete error code similarly if multiple applications follow the same message format their logs could be cross-referenced together data around a concrete value or key typical Keys included in a structure login include things such as date time velocity level a concrete message explained in the error and a unique request identifier just to mention a few as log is an experimental package that was the result of a proposal by Jonathan Amsterdam back in October 20th 2022. this proposal was accepted for go 1.21 in his log we have three principal types logger which provides several methods such as info worn debug and error for reporting events and those methods also support the context type next each logger is associated with the Handler which is in charge of formatting the third type record record defined as a log record which by default includes time a level and message and finally a set of key value pairs it's important to call out that Keys must be of a string type and the values may be of any order type alright enough talking let me show you the examples that I prepared for you as usual the link to the code is in the description of this video so feel free to check it out when you have some time let's start with the first example in this one I'm to be showing you the basics of this new API I have a few already prepared use case that I will be showing you by one by one as usual we imported this previously which is the experimental as log package what I did is already implemented a few lines that I call in the debug and the info methods that allow you to use the default level and the info level I want to show you first something which will be that by default this logger uses the log package so E5 let me comment that out and run it without having that I want to show you if I run this you will notice that it's using it's not using key value pairs but rather what the log package is provided it does use those right here below but is prefixed by this information that is coming from the log package and in order to confirm that if I update the flags that I gonna to be applicable to the log packages like for example display date log time use this UTC use micro seconds use the long file and things like that if I run this again you will see that now it's safe let's see that if I run again it will be now displaying the whole thing that I indicated with the flux now this is not something that we want or maybe we want it depends on the parser that we're using for collecting these logs but typically what we're looking for is for key value pairs in order to apply that directly let's comment this out we're going to be enabling this text Handler there are two handlers implemented in this log package at the moment one using Json and one using a text format so in order to do this we just follow this instruction we initialize the Handler like I told it before a Handler is going to be used as logger uses a Handler and that's the logger that we're going to be using for printing out the values we don't have to do anything other than that we clean up the screen and now we have the values everywhere and now are presented in a way now there are key values with the separator and is use the equals character if you notice we have below we have two calls one using debug and one use info by default s-log doesn't print out anything below info so info is the default value if you want to change this we will have to enable it by making a change like this let's comment it out the handle that we had before and let's enable the new Handler what we are doing here is we are initializing a few options that indicate that the minimum level that we want to print out will be debug and we associate that with a logger and again we set it as the default so these calls continue working if we run it again you will see now two instructions one for the debug and another one for info right here so this is a way to dynamically set the debug level and the perfectity actually when logging different records finally one thing that I mentioned in the beginning is that the keys Must Be Strings if you look at the API for example if you use info this API is using a variatic type of any or empty interface or any type back and forth so we could be defining incorrectly something like what I have here which is adding using 10 as an INT but it should be a string and the value that we have right here now if we run this you will notice that is going to be printing now a key that indicates hey this is a bad key I don't know exactly what to do with it so I'm just going to pre-printing now the two values that I have using a bad key this gets worse because now if I add a new extra something which will be in this case a key now these two fields are going to be the key and the and the value instead let me show you if you notice now we have bat key which is the wrong key in the first place and second we have 10 which is the one that we had above so it's it's a little bit interesting the way it works with logs so it's important to keep that in mind all the keys Must Be Strings anything else could be any of any other type let me show you the second example as log introduces a concept called groups that allow you to group a specific attributes and in this case if we go into the second folder and open the main.co file you would notice that I have two groups one indicating an application one and the other one indicating an application too if we use the default text handle you will see that the result is going to be printed out depending on the Handler itself so if I do a goal run you will notice that now the app one an IDR using a DOT and up to actually using a adult as well but if you use it different Handler for example if you use the Json Handler let me comment this out I'll bring this it change this enable this pack if we run it now it will show a typical JavaScript and like an object in JavaScript and similarly with the application too so the whole point of using groups in this case is in cases we have a we need to log different data from different systems and perhaps there is the case where they may be using the same IDs this is a way to separate those and keep them in in interfering between each one of them let me show you the third example is this final example shows you the support of context in the s-log package so if we open our main.co file I want to show you something first let's let's ignore the first line I want to show you what's happening with the s-log implementation we have an info that happens to be received in a context which in this case I'm doing some manipulation with the context you will see the implementation of that code in a moment but what I'm doing specifically is I'm going to be using the context of Value method to pass in a value in the context itself if you saw my video previously covering context and values you you most likely have an idea of what I'm talking about now the important thing is that by default this log package doesn't do anything with the context so if I do if I go ahead and go run this you will notice that it's just printing out the types and the fields that we have by default not the ones that were supposed to be in the context so so it's not doing anything with it but if we go ahead and Implement a custom Handler that happens to be supporting that implementation it will be making the use of the context and therefore the values that we set in the context to print out something in the log so if we run this again you will notice that now there is a new field called oauth token that has this Val this value of some fancy JWT now how does this work I implemented a custom Handler right here there is a type custom hand there is on Handler that happens to be embedding the original s-lock Json Handler I take care of the handle function that is the handle function is the one that is called when we're trying to format the result of the record and then what I'm doing is I'm doing a typical Paradigm that is used in go when you're trying to assign assign concrete or constant values to context in this case I'm I'm using this auth from Context function that is defined above that happens to be using these two important bits one will be this context key type and these are auth token context key of the same type type the refinable so what is happening here is I'm defining these two functions to get a constant value from a context in order to determine if the value exists or not so we can set it okay we can retrieve it which is the value of the function that is called right here below so this is a nice way to not lick the values that you specify and make them constant in such a way that you can use them to in your packages themselves this is a typical way to define values in contexts that apply and only to your package or the package that you're using so when we run this you will notice that now the contexts exist because I'm setting it right here so this is a nice way to to use context in the context of the s-log package and that's it thank you for watching I will talk to you next time take care and stay safe see ya
Info
Channel: Mario Carrion
Views: 7,107
Rating: undefined out of 5
Keywords: golang, go lang, golang tutorial, go lang tutorial, golang beginners, golang for beginners, learn golang, software architecture, golang software architecture, golang observability, golang structured logging, golang logging, structured logging, golang json
Id: htbGdhW3JdQ
Channel Id: undefined
Length: 10min 46sec (646 seconds)
Published: Fri Apr 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.