ss-logging-tool
TypeScript icon, indicating that this package has built-in type declarations

2.3.0 • Public • Published

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)
    • 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

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

Readme

Keywords

none

Package Sidebar

Install

npm i ss-logging-tool

Weekly Downloads

50

Version

2.3.0

License

MIT

Unpacked Size

139 kB

Total Files

34

Last publish

Collaborators

  • jimmysetsail
  • setsailkyle
  • derekwan616