summaryrefslogtreecommitdiff
path: root/src/logger.js
blob: 87143012825855f9789cd9f712d51f384d118c58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const Fetch = require("node-fetch"); // from npm registry

// wraps the (match group) with some markdown syntax
const markdown = [
    [ /^(?:[^ ]{1,3} )?([a-z.]+)/i, "**" ], // endpoint
    [ /<(\d+@vault)>/i, "_" ], // vault account
    [ /\[([a-z0-9.:]+)\]$/i, "`" ], // ip address
];

const md_prettify = (msg) => {
    for (const R of markdown) {
        msg = msg.replace(R[0], (m, p) => {
            const i = m.indexOf(p), l = p.length;
            return m.slice(0, i) + R[1] + p + R[1] + m.slice(i + l);
        });
    }

    return msg;
};

const send_hook = (msg) => {
    console.log(msg);

    if (process.env.LOGGER__WEBHOOK) {
        Fetch(process.env.LOGGER__WEBHOOK, {
            method: "POST",
            cache: "no-cache",
            redirect: "follow",
            headers: {
                "Accept": "application/json",
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                content: md_prettify(msg),
            }),
        });
    }
};

module.exports = {
    log: msg => send_hook(`${msg}`),
    info: msg => send_hook(`ℹ ${msg}`),
    warn: msg => send_hook(`⚠ ${msg}`),
    error: msg => send_hook(`❌ ${msg}`),
};