@vuedapt/logger
v1.0.0
Published
A simple, colorful logging package for Node.js
Maintainers
Readme
@vuedapt/logger
A production-ready logging library for Node.js with automatic HTTP request logging, color-coded log levels, and intelligent environment-based configuration. Features zero-configuration Express middleware for seamless HTTP logging and built-in production optimizations.
Changelog
See CHANGELOG.md.
Installation
npm install @vuedapt/loggerUsage
ES6 (ES Modules)
import logger from '@vuedapt/logger';
// Info logs
logger.info('Application started successfully');
// Warn logs
logger.warn('This is a warning message');
// Error logs
logger.error('An error occurred');
// Debug logs
logger.debug('Debug information');
// Express middleware (automatic HTTP logging for all requests)
import express from 'express';
const app = express();
app.use(logger.middleware());ES5 (CommonJS)
const logger = require('@vuedapt/logger');
// Info logs
logger.info('Application started successfully');
// Warn logs
logger.warn('This is a warning message');
// Error logs
logger.error('An error occurred');
// Debug logs
logger.debug('Debug information');
// Express middleware (automatic HTTP logging for all requests)
const express = require('express');
const app = express();
app.use(logger.middleware());Output Format
The default format is: YYYY-MM-DD HH:MM:SS [LEVEL] - Message
Default output (simplified):
2024-01-15 14:30:45 [INFO] - Application started successfully2024-01-15 14:30:46 [WARN] - This is a warning message2024-01-15 14:30:47 [ERROR] - An error occurred2024-01-15 14:30:48 [DEBUG] - Debug information2024-01-15 14:30:49 [HTTP] - GET /api/products HTTP/1.1 200
Customization
The logger can be customized using the configure() method:
ES6 (ES Modules)
import logger from '@vuedapt/logger';
// Enable file name and line number in logs
logger.configure({ showFileInfo: true });
// Disable stack traces for errors
logger.configure({ showStackTrace: false });
// Configure multiple options at once
logger.configure({
showFileInfo: true,
showStackTrace: true
});ES5 (CommonJS)
const logger = require('@vuedapt/logger');
// Enable file name and line number in logs
logger.configure({ showFileInfo: true });
// Disable stack traces for errors
logger.configure({ showStackTrace: false });
// Configure multiple options at once
logger.configure({
showFileInfo: true,
showStackTrace: true
});Configuration Options
showFileInfo(default:false) - Show file name and line number in logs- When enabled:
2024-01-15 14:30:45 [INFO] - Message (app.js:42) - When disabled:
2024-01-15 14:30:45 [INFO] - Message
- When enabled:
showStackTrace(default:!isProduction()) - Show stack traces for ERROR logs- Automatically disabled in production for security
- Can be manually enabled/disabled
Log Levels
- INFO - For informational messages
- WARN - For warning messages
- ERROR - For error messages
- DEBUG - For debug messages
- HTTP - Automatic HTTP request logging via Express middleware (method, URL, protocol, and status code)
HTTP Logging
HTTP requests are automatically logged when using the Express middleware. No configuration needed.
Status codes are color-coded:
- 2xx - Success
- 3xx - Redirect
- 4xx - Client error
- 5xx - Server error
Example output:
2024-01-15 14:30:45 [HTTP] - GET /api/users HTTP/1.1 2002024-01-15 14:30:46 [HTTP] - POST /api/login HTTP/1.1 4012024-01-15 14:30:47 [HTTP] - DELETE /api/resource/123 HTTP/2 500
Automatic extraction from request/response:
- Method: From
req.method - URL: From
req.originalUrlorreq.url - Protocol: From
req.protocolandreq.httpVersion, orreq.httpVersionalone - Status Code: From
res.statusCode
Express Middleware
Use the built-in middleware for automatic logging of all HTTP requests:
ES6 (ES Modules):
import express from 'express';
import logger from '@vuedapt/logger';
const app = express();
// Add logger middleware (logs all requests automatically)
app.use(logger.middleware());
app.get('/api/users', (req, res) => {
res.json({ users: [] });
// Request is automatically logged when response finishes
});ES5 (CommonJS):
const express = require('express');
const logger = require('@vuedapt/logger');
const app = express();
// Add logger middleware (logs all requests automatically)
app.use(logger.middleware());
app.get('/api/users', (req, res) => {
res.json({ users: [] });
// Request is automatically logged when response finishes
});The middleware automatically logs every HTTP request with all details extracted from the request/response objects.
Production Mode
The logger automatically adjusts its behavior based on the NODE_ENV environment variable:
When NODE_ENV=production:
- DEBUG logs are completely hidden - No debug output in production
- Stack traces are hidden - ERROR logs don't show full stack traces (security)
- File names and line numbers are hidden - No source location info (security)
- Cleaner logs - Reduced verbosity for better performance
When NODE_ENV is not production (development/staging):
- All log levels are shown (including DEBUG)
- Full stack traces for ERROR logs
- File names and line numbers included in all logs
Example
Development mode:
2024-01-15 14:30:45 [INFO] - Application started (app.js:42)
2024-01-15 14:30:46 [DEBUG] - Processing request (handler.js:15)
2024-01-15 14:30:47 [ERROR] - Database connection failed (db.js:23)
Error
at Database.connect (db.js:23:10)
at App.start (app.js:45:5)
...Production mode:
2024-01-15 14:30:45 [INFO] - Application started
2024-01-15 14:30:47 [ERROR] - Database connection failedAdditional Production Recommendations
For production environments, consider:
- Log aggregation - Use services like Loggly, Datadog, or CloudWatch
- Structured logging - Consider JSON format for easier parsing (future enhancement)
- Log rotation - Implement log rotation to manage disk space
- Sensitive data - Never log passwords, tokens, or PII
- Rate limiting - Consider rate limiting for high-volume applications
- Error tracking - Integrate with error tracking services (Sentry, Rollbar)
License
MIT
