Resources

Why You Should Add Logging To Your Code (and make it more helpful) - posit::conf(2023)

Presented by Daren Eiri Learn how the log4r package can help you better understand the errors your code may produce, and how to also get promptly alerted for severe errors by leveraging cloud monitoring solutions like Azure Monitor or AWS CloudWatch When an error happens in your API, Shiny App, or quarto document, it is not always clear what line of code you need to look at, and the error messages aren’t always helpful. By walking through a simple API example, I show how you can use logging packages like log4r to provide error messages that make sense to you. I also show how you can use cloud-based data collection platforms like Azure Monitor or AWS CloudWatch to set up alerts, so you can get notified by email or text message for those severe errors that you need to be immediately aware of. Gain more visibility into the health of your code by incorporating logging and pushing your logs to the cloud. Materials: https://dareneiri.github.io/positconf2023/ Presented at Posit Conference, between Sept 19-20 2023, Learn more at posit.co/conference. -------------------------- Talk Track: Lightning talks. Session Code: TALK-1164

image: thumbnail.jpg

Transcript#

This transcript was generated automatically and may contain errors.

Hi, my name is Darren Airey, and today I'd like to talk about why you should add logging to your code, and hopefully how to make it more helpful. We have in production Shiny apps, Plumber APIs, and R Markdowns. I work in the insurance industry, and so for example, we have a Plumber API that helps price insurance policies. So if something goes wrong, we need to know about it right away.

So when we get something like this in our Markdown that runs hourly, it gets a little annoying. What is this error? How do we fix it? It'd be nice to have some helpful hints to help you understand what happened, and maybe some convenient ways of getting notified when this error happens.

So when you're going through your code, it's kind of like walking in a dark forest, and you're not sure exactly where you're going. But by including some type of logging, you can have these trail markers that are on trees, so you have some sense of idea of where you're going, and how to troubleshoot your code.

Adding logging with log4r

So to demonstrate logging, I have a simple Plumber API. We're just going to divide some numbers. We have A as a numerator, and B as a denominator. And then for example, we do 100 by 4, we're going to get 25 as a result.

So to add our first trail marker, we're going to use the log4r here as a demonstration purpose. We're going to define the logger object, and then below we have an informational message that says, attempting to divide A by B. So now when we run through this example again, we're going to see in our console, info attempting to divide 100 by 4. So that's kind of useful.

So we're going to add a second trail marker here. What if we want to know when somebody does not include an input? We want to be notified of that, so we're going to include a warning message here. So when there's no inputs in the numerator or denominator, the user is going to receive back a must apply numerator or denominator. And in our console, we're going to see this warning message with the same message. That's interesting, but only you are going to see this message. No one else that you care about is going to see this and be notified when this error happens.

Adding satellite communicator capability

So we need to go beyond these trail markers on trees, and you have some type of satellite communicator capability to let people know when these types of things happen. When I go hiking by myself, I have a satellite communication device like this to let my wife know where and when I'm at all times, so she can feel at ease when I'm doing things like this.

So if you want to add satellite communicator capability to your logging, there's a bunch of sources available, Azure, Amazon Web Services, and Google Cloud all have their own type of logging service available where you can send your OS log file directly or interact with the logging system with an API.

So to add satellite communication ability to our example, we're just going to modify the logger and add a syslog appender, and we're going to call it division calculator API. What's syslog? Well, syslog is on a Linux system, the system's primary logging file. You see a bunch of syslog files here, and in that syslog, you're going to see a bunch of random stuff in there that you don't really care about, but you're also going to see your output for, in this case, division calculator API must supply a numerator and denominator. So now there's a source file that's being written to.

So what we have, an organization, we have our plumber, Shiny applications, and it's all hosted on a Posit Connect instance on our virtual machine running Linux, and then all that is writing to our syslog file. And we have it integrated with Azure Monitor, so all those syslog files get uploaded to the cloud, and now, when you go to a web browser, you can conveniently see in your syslog table where process name equals division calculator API. So now you see all those messages, and if you wanted to, you can filter it out so you don't see the informational messages, you just see the warnings, and you can even set up alerts so you know when those things happen and get notified by email or if you really want to, by text messages.

Maybe not everyone wants to do that. But I hope from this talk you can see that when you have your code, it's kind of like walking in a dark forest. You really don't know where you are, but by including logging, you have a better sense of where things could go wrong, and by using a cloud monitoring solution, it's like having a satellite communication device to let people know what's going on.

by including logging, you have a better sense of where things could go wrong, and by using a cloud monitoring solution, it's like having a satellite communication device to let people know what's going on.

I'd love to talk more about logging, but you can use this QR code in this link below to learn more about it, and if any of these things resonate with you, let's chat. Thanks so much. Bye-bye.