🌎 Follow me here:
X: https://x.com/tomdoes_tech
Discord: https://discord.gg/4ae2Esm6P7
Instagram: https://www.instagram.com/tomdoestech
TikTok: https://www.tiktok.com/@tomdoes_tech
Bluesky: https://bsky.app/profile/tomdoestech.bsky.social
☕ Buy me a coffee: https://www.buymeacoffee.com/tomn
Оглавление (1 сегментов)
Segment 1 (00:00 - 02:00)
Have you ever been debugging an issue, and to find out what happened on a single request, you need to go through a ton of logs? Now, you need to find those logs, and you might have a correlation ID that makes finding the logs really easy. But at the end of the day, you need to go through each log to find out what happened for that single request. Why did it fail? Why did it produce this outcome? What if there was a way to put all of the data for a single request in a single log? That's what I've been doing here with this build wide event function. Now, I'm calling it a wide event, but really it's a span. And this wide event comes with a few methods. One is the emit method, and then the other one is the set method. Now, I use this in some middleware, so I'm creating this wide event, and then I'm going to add this to context. I'm passing in some data here about the wide event. And now I can continue by setting properties on the event. So, if my request succeeds, then I'm going to say the outcome is success, the status code is 200, and I'm going to emit it with an info level log. If my request fails, then I'm going to set the status code to 500, and error, outcome to error. Now, I'm setting this outcome here as error and success, so I can easily filter on those properties. But of course, I'm going to want to filter these logs on lots of other properties, and I'm going to want to know a ton of other information about what happened in the request. So, if we go over to where I'm actually using one of these wide events, you can see here that I'm getting the wide event off context, and that's because it's put there by middleware, and I'm setting the function to this create post function. Now, throughout this request, I'm going to continue setting data on the event. So, I'm going to say the post IDs, the channel IDs, and then finally, once the request has finished, I'm going to emit the log. Now, if you want to see what this looks like, I can open up my terminal here, and I can tail these logs, and I'm just going to pass them through JQ, so we get some nice formatting here. And you can see that this is a single log here, and we have the function that was called, the time that it was called, my log level, we have the workspace role for the user that called the action, the user ID, the workspace ID, and my outcome. And then finally, we have the duration that this request took in milliseconds. So, that's an approach to logging that I like to take. If you do something similar or you do something different, let me know in the comments sections cuz I'm really interested what strategies you have for logging and debugging.