echortech-logging
v1.3.6
Published
logging
Downloads
188
Readme
echortech-logging
A robust logging middleware for Express.js applications that provides structured logging with correlation IDs, trace IDs, and standardized log formats.
Installation
npm install echortech-logging
Features
- Structured logging for Express.js applications
- Support for info, warn, and error log levels
- Automatic correlation ID and trace ID tracking
- Request timing and response metrics
- Custom tagging support
Usage
const express = require('express');
const echorLogger = require('echortech-logging');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware to simulate request info
app.use((req, res, next) => {
req.info = { received: Date.now() };
req.auth = { credentials: { sub: 'user123' } };
req.correlationId = 'correlation-id-123';
req.traceId = 'trace-id-123';
next();
});
// Success route
app.get('/success', (req, res) => {
const data = {
statusCode: 200,
message: 'This is a success message',
responseTime: 123,
result: { key: 'value' }
};
echorLogger.info(req, ['SUCCESS_TAG'], data);
res.status(200).send('Success logged');
});
// Warning route
app.get('/warn', (req, res) => {
const data = {
statusCode: 200,
message: 'This is a warning message',
responseTime: 150,
result: { key: 'value' }
};
echorLogger.warn(req, ['WARN_TAG'], data);
res.status(200).send('Warning logged');
});
// Error route
app.get('/error', (req, res) => {
const error = new Error('This is an error message');
const data = {
statusCode: 500,
error: error.message,
message: 'An error occurred',
responseTime: 200
};
echorLogger.error(req, ['ERROR_TAG'], data, error);
res.status(500).send('Error logged');
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
API Reference
echorLogger.info(req, tags, data)
Logs information level messages.
echorLogger.warn(req, tags, data)
Logs warning level messages.
echorLogger.error(req, tags, data, error)
Logs error level messages with error stack traces.
Parameters
req
: Express request object containing:info.received
: Request receipt timestampauth.credentials.sub
: User identifiercorrelationId
: Request correlation IDtraceId
: Request trace ID
tags
: Array of string tags for log categorizationdata
: Object containing log dataerror
: (Optional) Error object for error logging
Example Data Structure
{
statusCode: 200,
message: 'Log message',
responseTime: 123,
result: { key: 'value' }
}