A colorful, configurable logger for Node.js applications. Perfect for when you need more pizzazz than console.log but don't want the overhead of big logging libraries. Let's make those terminal outputs pretty!
import { Logger } from '@medishn/toolkit';
const logger = new Logger({
context: 'AuthService',
logLevels: ['error', 'warn', 'debug'],
});
logger.info('User logged in'); // Won't show (not in logLevels)
logger.error('Login failed', { userId: 123 }); // Red alert! 🔴No external dependencies are required. Just import and use:
import { Logger } from '@medishn/toolkit';The Logger class can be configured using an optional LoggerOptions object.
| Option | Type | Default | Description |
|---|---|---|---|
context |
string | 'Application' |
Your service/module name |
logLevels |
Array | All levels | What to log: ['error', 'warn', 'info', 'debug', 'verbose'] |
colorful |
boolean | true |
Fancy colors in terminal? 🌈 |
displayContext |
boolean | true |
Show/hide context tag |
// Production-ready config
const prodLogger = new Logger({
context: 'PaymentGateway',
logLevels: ['error', 'warn'],
colorful: false,
});Logger supports the following log levels:
info- General application messageserror- Errors and exceptionswarn- Warnings about potential issuesdebug- Debugging informationverbose- Detailed logs for deep insights
By default, all levels are enabled. You can customize which levels are allowed using logLevels.
We've got all the pretty colors! Here's how levels look by default:
| Level | Color | Style |
|---|---|---|
| error | Red | Bold |
| warn | Yellow | Bold |
| info | Green | Bold |
| debug | Blue | Dim |
| verbose | Purple | Dim |
Customize colors using the Style constants:
logger.styles.warn = ['BgBlack', 'FgBlack', 'Bold'];If colorful: false is set, all logs appear in plain text.
const logger = new Logger();
logger.info('Server started', { port: 3000 });
logger.error('Something went wrong');const dbLogger = new Logger({ context: 'Database' });
dbLogger.warn('Connection is slow');const plainLogger = new Logger({ colorful: false });
plainLogger.info('No colors in this log');const filteredLogger = new Logger({ logLevels: ['error', 'warn'] });
filteredLogger.info('This will not be logged');
filteredLogger.error('This will be logged');Logs an informational message.
logger.info('User logged in', { userId: 123 });Logs an error message.
logger.error('File not found', 'config.json');Logs a warning.
logger.warn('Low memory warning');Logs debug information.
logger.debug('Variable state', { foo: 'bar' });Logs detailed information for deeper insights.
logger.verbose('Loading configuration', { path: './config.json' });Here's the documentation update for the new child() method, integrated into your existing structure:
Create nested loggers with contextual inheritance using the child() method. Perfect for modular applications!
Creates a new logger instance with combined context while inheriting parent configuration.
Parameters:
context: Sub-context name to append
Returns:
New Logger instance with combined context
// Parent logger
const authLogger = new Logger({ context: 'AuthService' });
// Child logger inherits parent config
const dbLogger = authLogger.child('Database');
const redisLogger = dbLogger.child('Redis');
// Logs will show full context chain
dbLogger.info('Connection established');
// [AuthService::Database] [INFO] Connection established
redisLogger.warn('Cache miss');
// [AuthService::Database::Redis] [WARN] Cache miss[Application] [INFO] Server started { port: 3000 }
[Application] [ERROR] File not found "config.json"
Note: Colors only work in terminals that support ANSI escape codes. No rainbow text in production logs! 🌧️