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

ss-logging-tool

v2.3.0

Published

Logging package with built-in error reporting to GCP

Downloads

167

Readme

Features of this package

  1. Uses Pino logging format
  2. Logging level are mapped accordingly to GCP log level
  3. Can be integrated seamlessly as a middleware into node express server.
  4. All logs corresponding to each request will be traceable by a unique requestId assigned to each request.
  5. If an error is encountered during a request, all the log associated to the request is auto-esclated to WARN level
  6. Log level is controlled by the env LOGGING_LEVEL

How to use

const {Logger, expressRequestLoggingMiddleware} = require('ss-logging-tool');

// init GCP errorReporter with service account, this is optional
let credentials = require(`./sa.json`);
let projectId = credentials.project_id;
const errorReporting = Logger.initErrorReporting(projectId, credentials);

const loggerName = 'testingLogger';
const loggerChildOptions = {key: 'child prop'};

const express = require('express');
const app = express();
const port = 3000;

// Provide logger name, option and errorReporter to activate logger middleware
app.use(
    expressRequestLoggingMiddleware({
        loggerName,
        loggerChildOptions,
        errorReporting,
    })
);

app.get('/noError', (req, res) => {
    req.log.debug('logging info level msg', {pageId: 'fb'}); // logged as debug
    res.send('Hello World!');
});

app.get('/Error', (req, res) => {
    req.log.info('logging info level msg', {pageId: 'fb'}); // logged as WARN since the request has a fatal error afterward
    req.log.fatal('theres an error', new Error('error obj')); // logged as CRITICAL on GCP
    res.send('Hello World!');
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});

The INFO message is escalated to WARN upon error. The original level is stored in the data field. Note that all logs of the same request has the same requestId, this allow all logs of the same request to be filtered out easily on the GCP Log Viewer.

CHANGE LOG

1.0.1

  • Log error as object in pino.error/fatal (this prevents requestId from be overridden in the GCP log)

1.0.2

  • Just updated readme

1.0.3

  • Add header in error log

1.0.8

  • Add type declaration file

2.0.2

  • Breaking Change on expressRequestLoggingMiddleware function parameter. Please refer to the updated sample

2.1.0

  • Live UI for logger
    • logger.enableLive('http://localhost',3002,3021)
      • This will enable a live ui on web page http://localhost:3002/live using websocket port 3021 to communicate
    • Logger.middlewareCLS(getRequestContext,true) enable expressRequestLoggingMiddleware to carry request Id for each log

2.1.3

  • Bugs fix (missing export, used "this" to access static variable)

2.1.4

  • Bugs fix ('./liveLogger.html' no found because of the path)

2.2.0

  • Use Typescript for eventBus.js and debugger.js
  • Bug fix
    • The timestamp of the error was using the buffer emit time when using buffer
    • Right now the timestamp will be the time of the error
  • Add ability to change the request log level using url query
    • Example http://localhost:3000/test3?logLevel=silent will turn off log for this request
    • options for logLevel
      • trace
      • debug
      • info
      • error
      • warn
      • fatal
      • silent

2.2.1

  • Fix memory leak issues
  • Add function for logging-middleware for better profiling
    • loggerMapSize(), logMapperSize()
    • cleanMapper() in case something bad happened

2.2.2

  • Fix issue with false alarm error

2.2.3

  • Fix bug:
    • requestInfo was showing more than once when disableRequestContextAfterOnce was set to true
    • Showed multiple requestId
    • request context will work now even after request end (inside setTimeout)
      • it will change bufferLog to false after request end
  • Added test cases
  • Colorise and pretty print for local env
    • when process.env.STAGE === 'local'

2.2.4

  • Fix a bug when using async call in try-catch block will accidentally delete the request context
  • Colorise and pretty print for local env
    • when process.env.STAGE === 'local' or process.env.COLORIZE_LOG === 'true'
  • Annotate class instead of any for live logger

2.2.5

  • Fix memory leak when using with PerformanceObserver

2.3.0

  • getRequestId(isXRequestId:boolean)
    • logger.getRequestId() will return the requestId if exists. It will return "internal" when it is not trigger by any API request
      • Usage Example: global.ss_logger.getRequestId()
    • logger.getRequestId(true) will return the requestId if exists. It will return a new requestId for outgoing API call
      • Usage Example: axios.get('http://localhost:3000/test1', {headers: {'x-request-id': global.ss_logger.getRequestId(true)}})
      • Adding x-request-id will let the requestId pass to the next API service for easiler trace back