DEV Community

Guilherme Ananias for Woovi

Posted on

SlackOps: logging applications into a Slack channel with JavaScript

Here at Woovi, we manage a lot of logs of our applications in Slack channels. We can know things like:

  • For what reason some requests failed;
  • Who is the new user, the company, what is the flow that they went, etc;
  • Things related to Pix like if someone paid a QR code, withdrawals, some refund, etc;

We care about the data and security of all our users, therefore we sanitize and avoid sending any sensitive data.

All of it is even in our development environments too, local and staging.

How can I log things into some channels?

Here we create a simple function called sendtoSlack where we handle both @slack/node and @slack/webhook libraries internally.

So we get our webhook request URL created by Slack and put it into an environment variable like: process.env.SLACK_WEBHOOK. See here:

// .env
SLACK_WEBHOOK = 
"https://api.slack.com/messaging/webhooks/<foo>/<bar>";
Enter fullscreen mode Exit fullscreen mode

You can see here how to create your webhook URL.


After you own your URL, we just create an instance from IncomingWebhook with this webhook as your url argument. For example:

import { IncomingWebhook } from '@slack/webhook'; 

const wh = new IncomingWebhook(process.env.SLACK_WEBHOOK);
Enter fullscreen mode Exit fullscreen mode

Now, we just do a simple conditional validating in which environment you are and get the Slack channel based on it. Like this:

const getChannel = (channel: string) => {
  const isProduction = process.env.NODE_ENV === 'production';

  if (!isProduction) return `${channel}-dev`;

  return channel;
}
Enter fullscreen mode Exit fullscreen mode

Then, you can send your message now. Using your IncomingWebhook instance, just execute the send method.

const wh = new IncomingWebhook(process.env.SLACK_WEBHOOK);

await wh.send({
  channel: getChannel('<some channel here>'),
  blocks: // your message will go here
});
Enter fullscreen mode Exit fullscreen mode

And you will see your messages in your Slack channels! Now, you can abstract it to a function to use it anywhere.


The final result will be something like this:

const sendToSlack = async ({
  channel,
  blocks,
}) => {
  const getChannel = (channel: string) => {
    const isProduction = process.env.NODE_ENV === 'production';

    if (!isProduction) return `${channel}-dev`;

    return channel;
  }

  try {
    const wh = new IncomingWebhook(process.env.SLACK_WEBHOOK);

    wh.send({
      channel: getChannel(channel),
      blocks,
      // feel free to add any other arguments from this function that fits your needs.
    });
  } catch (err) {
    // do something with the error
    // here, we logged into Sentry
  }
}
Enter fullscreen mode Exit fullscreen mode

With that function, you can reuse the logging anywhere on your codebase.

You can reach a similar behavior using another communication tool, like Discord or anything else. Just see how they handle webhooks or things like that.

By the way, if you are curious about what is blocks, you can see here what is the Block Kit from Slack. Is just a prettier way to log things.

If you want just simple messages, you can use attachment argument from wh.send.


You can see a simple example of how we log things using the sendToSlack function:

sendToSlack({
  channel: 'join',
  attachments: [
    {
      text: 'Invalid recaptcha attempt',
    },
    ...getSomeUsefulData(), // here we get some useful data like timestamp, etc
  ],
});
Enter fullscreen mode Exit fullscreen mode

Woovi is a Startup that enables shoppers to pay as they like. To make this possible, Woovi provides instant payment solutions for merchants to accept orders.

Here we do a lot of things with Slack to improve our daily processes, if you are curious about that, we are hiring!

Top comments (1)

Collapse
 
tgmarinhodev profile image
Thiago Marinho

very good!