@starbemtech/star-logger
v1.0.3
Published
A TypeScript logging module using Winston and MongoDB, enabling easy integration of logging functionalities into your Node.js microservices. It offers configurable log levels, custom formats, and saves logs to MongoDB, facilitating centralized logging and
Downloads
416
Readme
Star Logger - Logging Module with Winston and MongoDB
Welcome to My Logger, a Node.js module written in TypeScript that simplifies implementing logging in your microservices. It utilizes Winston for log management and MongoDB as the storage destination.
Table of Contents
- Installation
- Usage
- Configuration Options
- Examples
- Capturing Uncaught Exceptions
- Customization
- Testing
- Contribution
- License
Installation
You can install the module via NPM:
$ yarn add star-logger
Usage
Import the module and initialize the logger by passing the MongoDB URI and optionally the desired configurations:
import { getLogger } from 'my-logger'
const logger = getLogger('mongodb://localhost:27017/my-database', {
level: 'info',
collection: 'logs',
})
// Using the logger
logger.info('Server started successfully')
logger.warn('High memory usage detected')
logger.error('Failed to connect to the database')
Configuration Options
The getLogger function accepts two parameters:
1. mongoUri: string - The MongoDB connection URI.
2. options: LoggerOptions (optional) - An object with configuration options.
LoggerOptions
| Property | Type | Default | Description |
| -------------- | -------------------------- | -------- | --------------------------------------------------------- |
| level
| string
| 'info'
| Minimum severity level of logs to be recorded. |
| collection
| string
| 'logs'
| Name of the MongoDB collection where logs will be stored. |
| mongoOptions
| MongoDBConnectionOptions
| {}
| Additional options for the MongoDB connection. |
Examples
Basic Configuration
import { getLogger } from 'my-logger'
const logger = getLogger('mongodb://localhost:27017/my-database')
// Using the logger
logger.info('Server initialized')
logger.warn('Potential issue detected')
logger.error('An error occurred')
Advanced Configuration
import { getLogger } from 'my-logger'
const logger = getLogger('mongodb://localhost:27017/my-database', {
level: 'debug',
collection: 'my-logs',
mongoOptions: {
authSource: 'admin',
ssl: true,
},
})
// Using the logger
logger.debug('Variable X has value:', x)
Capturing Uncaught Exceptions
To capture uncaught exceptions and ensure they are logged:
import { getLogger } from 'my-logger'
import { transports } from 'winston'
const logger = getLogger('mongodb://localhost:27017/my-database')
// Configure to capture exceptions
logger.exceptions.handle(
new transports.Console(),
new transports.MongoDB({
db: 'mongodb://localhost:27017/my-database',
collection: 'exceptions',
})
)
// Application code
process.on('unhandledRejection', (reason) => {
throw reason
})
Customization
You can customize the log format or add new transports as needed.
Customizing the Format
import { getLogger } from 'my-logger'
import { format } from 'winston'
const customFormat = format.printf(({ level, message, timestamp }) => {
return `${timestamp} [${level.toUpperCase()}]: ${message}`
})
const logger = getLogger('mongodb://localhost:27017/my-database', {
level: 'info',
})
logger.format = format.combine(format.timestamp(), customFormat)
// Using the logger
logger.info('Message with custom format')
Adding New Transports
import { getLogger } from 'my-logger'
import { transports } from 'winston'
const logger = getLogger('mongodb://localhost:27017/my-database')
// Adding a new file transport
logger.add(new transports.File({ filename: 'combined.log' }))
// Using the logger
logger.info('This log will be saved in MongoDB and the file')
Testing
The module includes unit tests written with Jest to ensure reliability.
Running Tests
First, install the development dependencies:
$ yarn
Then, run the tests:
$ yarn test
Note on Tests
Ensure that all resources are properly closed after tests to prevent Jest from hanging. This includes:
• Closing Winston logger transports
• Closing MongoDB connections
• Stopping the in-memory MongoDB server used in tests
Contribution
Contributions are welcome! Feel free to open issues or pull requests on the project’s repository.
Steps to Contribute
1. Fork the project.
2. Create a new branch with your feature or fix: git checkout -b my-feature.
3. Commit your changes: git commit -m 'Add new feature'.
4. Push to the branch: git push origin my-feature.
5. Open a pull request on GitHub.
License
This project is licensed under the MIT License - see the LICENSE file for details.