n8n Logging to Discord

n8n Jul 16, 2021

I hear a lot from different people who are uncertain how to know what is happening on their n8n instances when they are not there watching them. And even if you are there watching the workflows, you do not see what is happening on the active workflows that are being triggered behind the scenes without digging onto the executions.

I thought I would share with everyone how I have overcome this issue and know what is happening on all of my n8n instances in real time, even when I'm not at my computer using Discord.

In this article, you will learn how to:

  • Configure your Discord server to receive n8n notifications
  • Create an error workflow in n8n
  • Create an event log workflow
  • Integrate these workflows into your other workflows
  • Check your n8n alerts in Discord

So, let's get to it!

Assumptions

  • You already have a Discord server set up
  • You are familiar with how Discord works
  • You have admin priviledges on the Discord server
  • You have n8n available, either on-prem or cloud (if not, you can get a one month free trial for n8n)

What is Discord and Why Are We Using It?

For those of you who are unfamiliar with Discord, it is a popular chat platform that can be set up for free online. If you don't know how to set one up, you can check out the How-To Geek article How to Set Up Your Own Discord Chat Server.

There are several reasons for using Discord:

  1. Free
  2. Easy to set up
  3. Node exists in n8n
  4. Mobile app available
  5. Clients for most computer platforms

Configuring the Discord Server

Before we start working on n8n, we need to do a number of things on the Discord server.

First, create a new text channel and name it event-log:

Creating an event log channel in Discord

Next, create a new webhook for the channel:

Create webhook for channel

Name the webhook and give it an icon. Then copy the webhook URL:

Configuring channel webhook

Repeat this process to create an error-log channel.

Once you have completed these steps, it is now time to start building the n8n workflows.

Create Event Log Workflow

The first workflow that we are going to create is the event log workflow. This workflow will allow you to test and send information to the Discord event log channel.

Event log workflow with test node activated

The key to this workflow is to set the Discord node webhook URL to the value of the webhook URL created when you made the event-log channel.

Discord node webhook URL setting

The text field is the second piece of the puzzle for configuring the node. It is looking for the following inputs:

  • Title - the title of the message
  • Workflow - name of the workflow sending the alert
  • ID - ID number of the workflow sending the alert
  • Active - boolean indicating if the workflow is active or not
  • RunIndex - run index of the workflow
  • Message - message sent by the workflow

Note the information that is pushed to Discord is sent in Markdown format to give the alert a bit more punch.

Sample alert sent by n8n to Discord

Make sure that the Testing node is disabled before you launch the workflow in production so that you aren't just sent test data each time it is launched.

Event log workflow with the Testing node disabled

Workflow Code

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "webhookUri": "<Enter Your Webhook URL Here>",
        "text": "={{$json[\"Title\"]}}\n**Workflow Information**\n> **Name**: {{$json[\"Workflow\"]}}\n> **ID**: {{$json[\"ID\"]}}\n> **Active**: {{$json[\"Active\"]}}\n> **Run Index**: {{$json[\"RunIndex\"]}}\n> **Timestamp**: {{new Date().toUTCString()}}\n\n**__Message__**\n> {{$json[\"Message\"]}}"
      },
      "name": "Send Alert to Discord",
      "type": "n8n-nodes-base.discord",
      "typeVersion": 1,
      "position": [
        670,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Title",
              "value": "Title"
            },
            {
              "name": "Workflow",
              "value": "Workflow"
            },
            {
              "name": "Message",
              "value": "Message"
            }
          ],
          "number": [
            {
              "name": "ID",
              "value": 34527
            },
            {
              "name": "RunIndex",
              "value": 12
            }
          ],
          "boolean": [
            {
              "name": "Active",
              "value": true
            }
          ]
        },
        "options": {}
      },
      "name": "Testing",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Testing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Testing": {
      "main": [
        [
          {
            "node": "Send Alert to Discord",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Event Log Code

Create Error Log Workflow

The next workflow that we are going to be creating is the error log workflow. This is a very special workflow because it uses the Error Trigger node which listens to error events on specified workflows and then responds to them.

The Error Trigger has no input parameters and requires no configuration other than being connected to another node.

Error Log Workflow

The Discord node is configured very similarly to the same node in the event workflow except its webhook points to the error-log channel in Discord. The text field formats the data that is automatically generated by the error trigger node to generate an error that looks like the following:

Sample error generated by n8n

You will notice that the error message generates a lot of information pointing not only to where the error occurred in the workflow but also what errors and messages it received and a direct link to the actual workflow execution that created the error. This greatly reduces the troubleshooting effort for the workflow.

Activating the Error Log Workflow

Because the error log workflow is designed to listen to all errors, it can be used by multiple other workflows for error logging.

To enable this error logging facility, open up the workflow you wish to use to the error log and go to the Settings screen. The first entry on the screen is Error Workflow. Set this value to Error Log.

Workflow settings to use the Error Log workflow

Workflow Code

{
  "nodes": [
    {
      "parameters": {},
      "name": "Error Trigger",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        -1280,
        -160
      ]
    },
    {
      "parameters": {
        "webhookUri": "<Enter Your Webhook URL Here>",
        "text": "=Error in workflow **{{$json[\"workflow\"][\"name\"]}} ({{$json[\"workflow\"][\"id\"]}})**, node *{{$json[\"execution\"][\"error\"][\"node\"][\"name\"]}}*\n**__Node Info__**\n> **Node Type**: {{$json[\"execution\"][\"error\"][\"node\"][\"type\"]}}\n> \n> **Node Parameters**: \n>   **Keys**: `{{Object.keys($json[\"execution\"][\"error\"][\"node\"][\"parameters\"]).join(', ')}}`\n>   **Values**: `{{Object.values($json[\"execution\"][\"error\"][\"node\"][\"parameters\"]).join(', ')}}`\n\n**__Error Info__**\n> **Name**: {{$json[\"execution\"][\"error\"][\"name\"]}}\n> **Timestamp**: {{new Date($json[\"execution\"][\"error\"][\"timestamp\"]).toLocaleDateString('en-US',{month:'long', day: 'numeric', year:'numeric', hour:'numeric', minute:'numeric', second:'numeric'})}}\n> **Message**: {{$json[\"execution\"][\"error\"][\"message\"]}}\n> **Stack**: {{$json[\"execution\"][\"error\"][\"stack\"]}}\n\nFor more information, please see {{$json[\"execution\"][\"url\"]}}"
      },
      "name": "Discord",
      "type": "n8n-nodes-base.discord",
      "typeVersion": 1,
      "position": [
        -1100,
        -160
      ]
    }
  ],
  "connections": {
    "Error Trigger": {
      "main": [
        [
          {
            "node": "Discord",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Error Log Code

Create Test Workflow

In order to make sure that your workflows are working properly, you can create a test workflow to log an event and then throw an error to log an error event.

Workflow to Test Event and Error Logging

You will need to make two changes to this workflow before it will work properly. The first is enter in the workflow ID for the event log workflow in the Send To Event Log node. You can determine the workflow ID by opening up the event log workflow and looking at the number at the end of the URL.

Determining a workflow's ID

The second configuration change that you need to do is to set the error workflow in the settings to the Error Log workflow. (See above for more details).

Once you have made the configuration changes, save and activate your workflow. In under a minute you should see an event and an error entry in each of the Discord channels.

Note that the Event Log Parameters automatically pulls most of the information sent to the event log automatically by using expression variables. The only two things that you will want to set yourself will be the Title and Message sent to the event log. This can be done manually or programmatically.

Workflow Code

{
  "nodes": [
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        350,
        480
      ]
    },
    {
      "parameters": {
        "functionCode": "items[1] = \"dog\";\n\nreturn items;"
      },
      "name": "Error Generator",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        930,
        480
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Title",
              "value": "Event Log Example"
            },
            {
              "name": "Workflow",
              "value": "={{$workflow.name}}"
            },
            {
              "name": "Message",
              "value": "This message will be shown in the event log."
            }
          ],
          "number": [
            {
              "name": "ID",
              "value": "={{$workflow.id}}"
            },
            {
              "name": "RunIndex",
              "value": "={{$runIndex}}"
            }
          ],
          "boolean": [
            {
              "name": "Active",
              "value": "={{$workflow.active}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Event Log Parameters",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        530,
        480
      ]
    },
    {
      "parameters": {
        "workflowId": "<Enter Your Event Log Workflow ID>"
      },
      "name": "Send To Event Log",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1,
      "position": [
        730,
        480
      ]
    }
  ],
  "connections": {
    "Cron": {
      "main": [
        [
          {
            "node": "Event Log Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Event Log Parameters": {
      "main": [
        [
          {
            "node": "Send To Event Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send To Event Log": {
      "main": [
        [
          {
            "node": "Error Generator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Test Workflow Code

Conclusion

We have learned how to create and send event and error log entries to custom Discord channels to give visibility to these events when not monitoring them actively in n8n.

These events can then be seen on the Discord web, native and mobile apps.

Error log event as viewed from the Discord Android client

Feel free to reach out to me if you have any questions or suggestion about these workflows and I hope you find them to be useful.

Tags

Tephlon

A passionate tech nerd for over 25 years. I consider myself to be a generalist so I know a little about a lot. My latest obsessions are Raspberry Pi computers and n8n automation.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.