npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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

153

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

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.