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

@24hr/sentry-logger-node

v1.2.2

Published

This package includes Sentry middleware for Express and Koa and also exports a logger function. This mirrors how Winston logging functions work so replacing would be as simple as possible.

Downloads

87

Readme

Sentry logger node

This package includes Sentry middleware for Express and Koa and also exports a logger function. This mirrors how Winston logging functions work so replacing would be as simple as possible.

How to install

Install the package:

npm install @24hr/sentry-logger-node

Initialize Sentry in your app:

const sentryLogger = require('@24hr/sentry-logger-node');

sentryLogger.init({ dsn: SENTRY_DSN, release: SENTRY_RELEASE, environment: SENTRY_ENVIRONMENT, serviceName: 'my-service' });

// If you dont want it to send to Sentry in development:
if (process.env.NODE_ENV === 'production') {
    sentryLogger.init({ dsn: SENTRY_DSN, release: SENTRY_RELEASE, environment: SENTRY_ENVIRONMENT, serviceName: 'my-service' });
}

Setup with Express

The package exports an Express middleware. To use it, be sure to use as soon as possible:

app.use(sentryLogger.express.middleware());

Setup with Koa

app.use(sentryLogger.koa.middleware());

Middleware options

The middleware function takes an option object where you can pass tags and requestId. This works the same for both Koa and Express.

Tags

To add more tags:

const extraTags = {
    tagKey: 'tagValue';
}
app.use(sentryLogger.koa.middleware({ tags: extraTags }));

Request ID

A Request ID is just a another tag but is a unique identifier for the request. It's super useful if you have a request that has to pass by multiple microservices.

By default the middleware will set the request ID to the value of the x-request-id header, but if that doesn't suit you, you can pass the requestId as an option.

app.use(sentryLogger.koa.middleware({ requestId: 'unique id' }));

Get request Id from other source

If you don't have a x-request-id header you can create a custom middleware to retrieve it.

For Koa:

app.use((ctx, next) => {
    const myRequestId = ctx.headers['x-my-request-id'];
    return sentryLogger.koa.middleware({ requestId: myRequestId })(ctx, next)
});

For Express:

app.use((req, res, next) => {
    const myRequestId = ctx.headers['x-my-request-id'];
    return sentryLogger.koa.middleware({ requestId: myRequestId })(req, res, next)
});

Logging

This package exports a logger object with different logging levels. The current levels are: verbose, info, warning, error, fatal and critical.

The ones that gets sent to Sentry are error, fatal and critical.

I plan to add a LOG_LEVEL env variable feature to exclude unwanted logs, e.g verbose. Right now, verbose and info are the same, but exists for backwards compatibility.

How to use (examples)

const { logger } = require('@24hr/sentry-logger-node');

logger.info('Info text'); // Green color in console
logger.warning('Warning text'); // Yellow color in console
logger.error('Error', new Error('An error occcured!')); // Red color in console and sent to Sentry.