Introduction to Kosher Logger
The kosher-logger is a highly configurable and efficient logging library for Node.js applications. It offers a wide range of APIs to meet all your logging needs, making debugging and monitoring a breeze.
Why Choose Kosher Logger?
Kosher Logger is designed to be easy to use, extendable, and powerful. Whether you are building a small app or an enterprise-level service, this logger provides everything you need.
Installation
npm install kosher-logger
Basic Usage
Here’s a basic example of how to use Kosher Logger in your application.
const KosherLogger = require('kosher-logger');
const logger = new KosherLogger();
logger.info('This is an info message');
logger.error('This is an error message');
logger.debug('This is a debug message');
Configuration
You can customize the logger to suit your requirements.
const logger = new KosherLogger({
level: 'debug',
transport: 'console',
timestamp: true,
format: 'json'
});
Setting Log Levels
Log levels help in filtering the messages based on their severity.
logger.setLevel('warn');
logger.info('This info will not be logged');
logger.warn('This is a warning message');
logger.error('This is an error message');
Custom Transports
You can define custom transports to log messages in different ways (e.g., to a file, over HTTP).
const fileTransport = {
type: 'file',
options: {
filename: 'logs/app.log',
}
};
logger.addTransport(fileTransport);
logger.info('This message will be logged to a file');
Using Context
Context can be added to the logs for better traceability.
logger.withContext({ requestId: '12345' }).info('Request received');
Log Rotation
Log rotation prevents log files from becoming too large.
const rotatedFileTransport = {
type: 'file',
options: {
filename: 'logs/app-%DATE%.log',
datePattern: 'YYYY-MM-DD',
maxSize: '20m',
maxFiles: '14d'
}
};
logger.addTransport(rotatedFileTransport);
logger.info('This message will be logged to a rotated file');
Error Handling
Systematically catch and log errors in your application.
process.on('uncaughtException', (error) => {
logger.error('Uncaught Exception', { error });
});
process.on('unhandledRejection', (reason) => {
logger.error('Unhandled Rejection', { reason });
});
Complete App Example
Below is a complete example of an application utilizing multiple features of the Kosher Logger.
const express = require('express');
const KosherLogger = require('kosher-logger');
const app = express();
const logger = new KosherLogger({
level: 'debug',
transports: [
{ type: 'console' },
{ type: 'file', options: { filename: 'logs/app.log' } }
]
});
app.use((req, res, next) => {
logger.withContext({ method: req.method, url: req.url }).debug('Request received');
next();
});
app.get('/', (req, res) => {
logger.info('Handling root route');
res.send('Hello, world!');
});
app.use((err, req, res, next) => {
logger.error('Error handling request', { err });
res.status(500).send('Something went wrong');
});
app.listen(3000, () => {
logger.info('Server started on port 3000');
});
With these configurations and examples, you now have a powerful and flexible logging setup for your Node.js applications using Kosher Logger!
Hash: 7b126ecbfb099c7ac721d3b4d3aee164b15089289dd773c4788f3dfc6b153ce6