@nodesandbox/logger is a singleton logging utility for Node.js applications built using the winston library. It provides structured logging formats, supports multiple transports, and offers a simple API for logging messages at different levels.
- Singleton Pattern: Ensures only one instance of the logger is created.
- Custom Log Format: Combines timestamps, error stack traces, and other relevant information into structured logs.
- Multiple Transports: Logs to both the console and files (error logs and combined logs).
- Exception Handling: Automatically logs uncaught exceptions to a separate file.
- File Logging: Supports writing specific log messages to individual files.
You can install @nodesandbox/logger via npm:
npm install @nodesandbox/loggerimport { LoggerService } from '@nodesandbox/logger';
const logger = LoggerService.getInstance();
// Logging at different levels
logger.info('Application has started');
logger.warn('This is a warning message');
logger.error('An error occurred', new Error('Sample error'));
// Logging to a specific file
logger.file('custom-log', 'This is a log message for a custom log file');You can use the following log levels:
info: General information messageswarn: Warning messageserror: Error messages with optional stack trace
By default, logs are saved in the logs directory:
logs/error.log: Contains error messageslogs/combined.log: Contains all messageslogs/exceptions.log: Contains uncaught exceptionslogs/browser: Contains custom logs
This project is licensed under the MIT License.