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.
- Installation
- Usage
- Configuration Options
- Examples
- Capturing Uncaught Exceptions
- Customization
- Testing
- Contribution
- License
You can install the module via NPM:
$ yarn add @starbemtech/star-loggerImport 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')The getLogger function accepts two parameters:
1. mongoUri: string - The MongoDB connection URI.
2. options: LoggerOptions (optional) - An object with configuration options.
| 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. |
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')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)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
})You can customize the log format or add new transports as needed.
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')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')The module includes unit tests written with Jest to ensure reliability.
First, install the development dependencies:
$ yarnThen, run the tests:
$ yarn testEnsure 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
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.
This project is licensed under the MIT License - see the LICENSE file for details.