Sends log events to a Discord channel. This is an appender for use with log4js.
This is a Node.js module available through the npm registry.
npm i log4js-discord-appender
- Open your Server Settings and head into the Integrations tab:
- Click the "Create Webhook" button to create a new webhook!
You'll have a few options here. You can:
- Edit the avatar: By clicking the avatar next to the Name in the top left
- Choose what channel the Webhook posts to: By selecting the desired text channel in the dropdown menu.
- Name your Webhook: Good for distinguishing multiple webhooks for multiple different services.
type
-log4js-discord-appender
webhookURL
- Enter the Discord-generated URL
log4js.configure({
appenders: {
discordAlert: {
type: 'log4js-discord-appender',
webhookUrl: 'https://discord.com/api/webhooks/*****/**********'
}
},
categories: {default: {appenders: ['discordAlert'], level: 'warn'}}
});
This configuration will send all warn (and above) messages to the respective discord channel.
log4js.configure({
appenders: {
out: {type: 'stdout'},
allLogs: {type: 'file', filename: 'logs/all.log', maxLogSize: 10485760, backups: 10, compress: true},
outFilter: {
type: 'logLevelFilter', appender: 'out', level: process.env.LOG_LEVEL || 'all'
},
discordAlert: {
type: 'log4js-discord-appender',
webhookUrl: 'https://discord.com/api/webhooks/*****/**********'
},
discordFilter: {
type: 'logLevelFilter', appender: 'discordAlert', level: process.env.ALERT_LOG_LEVEL || 'warn'
}
},
categories: {
default: {appenders: ['allLogs','outFilter', 'discordFilter'], level: process.env.LOG_LEVEL || 'all'}
}
});
This configuration displays use of multiple appenders.
outFilter
: Push log in stdout with filterLOG_LEVEL
set in environment, if not set thenall
levelsdiscordFilter
: Push log in discord channel with filterALERT_LOG_LEVEL
set in environment, if not set thenwarn
levels
/**
* System and 3rd party libs
*/
const log4js = require('log4js');
const morgan = require('morgan');
/**
* Configure log4js
*/
log4js.configure({
appenders: {
out: { type: 'stdout' },
allLogs: { type: 'file', filename: 'logs/all.log', maxLogSize: 10485760, backups: 10, compress: true },
errorLogs: { type: 'file', filename: 'logs/error.log', maxLogSize: 10485760, backups: 10, compress: true },
errorLogsFiltered: {
type: 'logLevelFilter',
appender: 'errorLogs',
level: 'warn',
},
outFilter: {
type: 'logLevelFilter',
appender: 'out',
level: process.env.LOG_LEVEL || 'all'
},
discordAlert: {
type: 'log4js-discord-appender',
webhookUrl: process.env.DISCORD_WEBHOOK_URL || 'https://discord.com/api/webhooks/*****/**********',
},
discordFilter: {
type: 'logLevelFilter',
appender: 'discordAlert',
level: process.env.DISCORD_ALERT_LOG_LEVEL || 'warn',
},
},
categories: {
default: { appenders: ['allLogs', 'outFilter', 'errorLogsFiltered', 'discordFilter'], level: process.env.LOG_LEVEL || 'all' },
},
});
const log = log4js.getLogger();
const morganInstance = morgan('dev', {
stream: {
write: str => {
if (str && str.split('?')[1]) {
if (str.split('?')[1].split('=')[0] !== 'watermark') {
log.debug(str);
}
} else {
log.debug(str);
}
},
},
});
/**
* Service Export
*/
module.exports = {
log,
morgan: morganInstance,
};