slack-logger-light
Version:
Zero dependency logger for slack, super light, great for service workers
42 lines • 1.46 kB
JavaScript
class SlackLogger {
APP_TOKEN;
CHANNEL;
NO_CONSOLE_LOG_ON_ERROR = false;
MESSEGE_EP = 'https://slack.com/api/chat.postMessage';
constructor({ APP_TOKEN, CHANNEL_ID, NO_CONSOLE_LOG_ON_ERROR }) {
this.APP_TOKEN = APP_TOKEN;
this.CHANNEL = CHANNEL_ID;
if (NO_CONSOLE_LOG_ON_ERROR !== undefined) {
this.NO_CONSOLE_LOG_ON_ERROR = NO_CONSOLE_LOG_ON_ERROR;
}
}
changeChannel = (channel) => {
this.CHANNEL = channel;
};
changeAppToken = (appToken) => {
this.APP_TOKEN = appToken;
};
async log(message) {
if (this.APP_TOKEN && this.CHANNEL) {
const response = await fetch(this.MESSEGE_EP, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.APP_TOKEN}`
},
body: JSON.stringify({
channel: this.CHANNEL,
text: message
})
});
if (!response.ok && !this.NO_CONSOLE_LOG_ON_ERROR) {
console.error('Error sending message to slack, status:', response.status, 'Text:', await response.text());
}
}
else if (!this.NO_CONSOLE_LOG_ON_ERROR) {
console.error('APP_TOKEN and CHANNEL are required');
}
}
}
export { SlackLogger };
//# sourceMappingURL=main.js.map