Visualize NodeJS Errors in Real Time with Llama Logs
Tags: webdev, javascript, node, tutorial
Published: 2020-08-26 02:20:11

---
title: Visualize NodeJS Errors in Real Time with Llama Logs
published: true
description: 
tags: Webdev, Javascript, Node, Tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/ivzegt9p4xrzpcdqhk4y.png
---
![Node Errors](https://dev-to-uploads.s3.amazonaws.com/i/hg0hby3cx9m5dhdvs8s5.gif)

#What?
Have you ever wondered what is going on inside your program?  Wanted a visual way to inspect its inner workings?

The gif above shows an example of [Llama Logs](https://llamalogs.com).  It is a new tool I created to let you see the inner workings of your applications in real time. It is ready for you to start using in your app for free today!

Below I will walk through an example of using Llama Logs to show and debug errors in a basic express app as they happen.

#The Setup
I am going to write a basic express app that takes in a user's email through a url param and saves it to the database if the email is a llamalogs.com domain.

The basic logic will look like this
```Javascript
app.get('/', (req, res) => {
  let customerEmail = req.query.email
  let isDomainOk = domainCheck(customerEmail)

  if (isDomainOk) {
      saveEmail(customerEmail)
  }

  res.send('We received your email')
})
```

Now the doozy is that I am going to write some horrible code that errors if the user forgets to include the @domain part of their email.

```Javascript
const domainCheck = (customerEmail) => {
  // toLowerCase will fail if the [1] value is undefined!
  const domain = customerEmail.split("@")[1].toLowerCase()
  const domainIsOk = domain === "llamalogs.com"
  return domainIsOk
}
```

#Visualizing with Llama Logs
Llama Logs is incredibly simple to set up.  Once you sign up on llamalogs.com, all you need to do is install the client via npm and then start logging.  Llama Logs will automatically turn your logs into an interactive graph.

So for example lets update the domainCheck method to the following
```Javascript
const domainCheck = (customerEmail) => {
  try {
    const domain = customerEmail.split("@")[1].toLowerCase()
    const domainIsOk = domain === "llamalogs.com"

    LlamaLogs.log({ sender: 'Server', receiver: 'Domain Check' })
        
    return domainIsOk

  } catch (e) {
    LlamaLogs.log({ 
      sender: 'Server', 
      receiver: 'Domain Check', 
      message: `input: ${customerEmail}; Error: ${e}`,
      isError: true
    })
  }
}
```

So we have added a logging case for both the success and failure outcomes.  Llama Logs will then use the names provided in the "sender", "receiver", and "isError" attributes to automatically visualize the activity in your app as a series of points moving between components.

In the graph below we can see the result of running a few calls to the server with valid emails and ones that cause errors. 
![Node Errors](https://dev-to-uploads.s3.amazonaws.com/i/hg0hby3cx9m5dhdvs8s5.gif)

#Debugging

Even better than visualizing the activity in the graph, Llama Logs will let you get data from your errors in real time.

Remember in the domainCheck method we attached this attribute to the Llama Log?
```Javascript
message: `input: ${customerEmail}; Error: ${e}`,
```
By using this message attribute, that means that when we hover on the red error point, it will show the message.  The image below comes shows me hovering on an error.  The request it represents had the email param == "jd", missing the email domain.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eqdh1qqsb6fkfvm4qoc6.png)

By using Llama Logs to visualize errors in your system, you can identify the sources of bugs faster and easier than ever before!

#More Info

Please visit [LlamaLogs.com](https://llamalogs.com) for more information if you are interested.  The app is free and ready to use today.  Please feel free to contact me at andrew@llamalogs.com if you have any questions.

#Full Code
This is such a small express app I think it is easiest to include all of the code within this blog post.

```Javascript
const express = require('express')
const { LlamaLogs } = require('llamalogs');

LlamaLogs.init({
    accountKey: 'YOUR_ACCOUNT_KEY',
    graphName: 'YOUR_GRAPH_NAME'
});

const app = express()
const port = 3000

app.get('/', (req, res) => {
  LlamaLogs.log({ sender: 'User', receiver: 'Server' })

  let customerEmail = req.query.email
  let isDomainOk = domainCheck(customerEmail)

  if (isDomainOk) {
      saveEmail(customerEmail)
  }

  res.send('We received your email')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})


const domainCheck = (customerEmail) => {
  try {
    const domain = customerEmail.split("@")[1].toLowerCase()
    const domainIsOk = domain === "llamalogs.com"

    LlamaLogs.log({ sender: 'Server', receiver: 'Domain Check' })
    
    return domainIsOk

  } catch (e) {
    LlamaLogs.log({ 
      sender: 'Server', 
      receiver: 'Domain Check', 
      message: `input: ${customerEmail}; Error: ${e}`,
      isError: true
    })
  }
}

const saveEmail = (customerEmail) => {
    // pretend we are saving to a database here
    LlamaLogs.log({ sender: 'Domain Check', receiver: 'Database' })
}
```