TurboLogger is a versatile logging library for Node.js applications that supports logging to the console, file, and Slack channel. It provides configurable logging levels (error, info, warn), single and hybrid logging capabilities, and customizable logging contexts.
Install TurboLogger using npm:
npm i --save turbo-logger
- Logging to Slack Channel
- Console Logging
- Logging to file
- Support for different logging levels (error, info, warn)
- Single and Hybrid Logger (Ability to log to one or multiple sources)
- Configurable through environment variables (e.g., setting the context to production, development, or custom context)
- Ability to log comma-separated messages
import turboLogger from 'turbo-logger';
const config = {
"slack": {
webhook_url: `${process.env.WEBHOOK_URL}`,
channel: 'sample-channel',
}
}
const env = {
prod: ['console', 'slack'],
dev: ['file', 'console'],
myCustomConfig: ['console']
}
const logger = turboLogger.createStream(config, env.prod);
Once the logger is initialized, you can use it to log messages:
logger.log('hello world'); // logs the message to the console and Slack with an "info" context
logger.warn('hello world'); // logs the message to the console with a "warn" context
logger.error('hello world'); // logs the message to a file and the console with an "error" context
You need to initialize the logger with the slack config if you plan on logging to Slack. If not, you can use like so.
import turboLogger from 'turbo-logger';
const logger = turboLogger.createStream(); // env will default to logging to console.
To log only to Slack, set the environment parameter to "slack" and provide the required Slack configuration. Please note that in order to use this feature, you need to create a Slack app and obtain the necessary credentials.
Here's how to set up the Single Logger for Slack:
-
Create a Slack app following the instructions in the Slack App Creation Guide..
-
Obtain the webhook_url and channel for your Slack app. The webhook_url is a unique URL that allows your application to send messages to a specific Slack channel. This is gotten from the "incoming webhook" settings for the slack app you created. Enable it and set the channel you want the message to be sent to. Slack creates a separate webhook url for eah channel. ![](Webhook Screen)
-
Configure the config object with the webhook_url and channel values:
const config = { "slack": { webhook_url: `${process.env.WEBHOOK_URL}`, channel: 'sample-channel', } } const env = ['slack'] const logger = turboLogger.createStream(config, env); logger.log('hello world'); // sends the message to the specified Slack channel
Ensure that you replace ${process.env.SECRET} in the webhook_url with the actual secret value obtained from your Slack app.
Note: If the config object does not have the required webhook_url and channel parameters, TurboLogger will throw an error.
The hybrid logger combines multiple log levels. It could be a combination of all three or any two levels. To use this, we set the environment configuration to include all three or any two log levels:
const env = ['console', 'slack', 'file']
const logger = turboLogger.createStream(config, env);
logger.log('hello world'); // sends the message to all contexts (console, Slack, and file)
TurboLogger allows you to log comma-separated messages. For example:
logger.log('My config object: ', config);
You can log as many comma-separated messages as you want.
You can log to several Slack channels. The logger streams are configured separately as every channel
import turboLogger from 'turbo-logger';
const env = ['slack'];
// Error channel set up
const errorChannelName = "error-logs";
const errorChannelConfig = {
"slack": {
webhook_url: `${process.env.SLACK_ERROR_CHANNEL_WEBHOOK_URL}`,
channel: errorChannelName,
}
};
// Success channel set up
const successChannelName = "success-logs";
const successChannelConfig = {
"slack": {
webhook_url: `${process.env.SLACK_SUCCESS_CHANNEL_WEBHOOK_URL}`,
channel: successChannelName,
}
};
// Instantiate logger
const slackErrorLogger = turboLogger.createStream(errorChannelConfig, env);
const slackSuccessLogger = turboLogger.createStream(successChannelConfig, env);
// Usage
slackErrorLogger.error("Internal server error") // This sends a message with an error context to the channel named "error-logs"
slackSuccessLogger.log("Request successful") // This sends a message with an info context to the channel named "success-logs"
TurboLogger is licensed under the MIT License.
TurboLogger was created by Gideon Odiase.
Buy me a coffee here.