New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

gc-json-logger

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gc-json-logger

Log JSON entries to stdout/stderr to be queried in Google Cloud Monitoring

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
721
decreased by-31.79%
Maintainers
1
Weekly downloads
 
Created
Source

Logger for Structured Logging with Asynchronous Context Tracking (Stability: 2 - Stable)

Allows printing logs to stdout, stderr for further action in Google Cloud platform. Given that log agent ingests logs from stdout streams when running in GKE.

NPM Coverage Health License Runkit

Installation

npm install --save gc-json-logger

yarn add gc-json-logger

View in Google Cloud Logging

Google Cloud Logging

Integration

const { Logger } = require('gc-json-logger');
const { createServer } = require('http');

let job = 0;

function scheduleJob() {
  /**
   * 4) Retrieving a context logger
   */
  const logger = Logger.getLogger();

  /**
   * 5) Logs with job id from the parent
   */
  logger.info('scheduling job');

  setTimeout(() => {
    /**
     * 6) Logs with job id from the parent
     */
    logger.info('job done');
  }, 3000);
}

const server = createServer((_, res) => {
  job++;

  /**
   * 1) Setting a context logger using job id
   */
  const logger = new Logger(job);
  Logger.runWith(logger, () => {
    /**
     * 2) Logs with with job id
     */
    logger.info('creating a new job');

    /**
     * 3) Calls another function that uses the context
     */
    scheduleJob();

    res.setHeader('content-type', 'application/json');
    res.end(JSON.stringify({ id: job, status: 'scheduled' }));
  });
});

server.listen(8080);
{
  "severity": "INFO",
  "time": "2022-12-03T10:00:00.000Z",
  "message": "creating a new job",
  "logging.googleapis.com/operation": { "id": 1 } // <- always includes context id
}

{
  "severity": "INFO",
  "time": "2022-12-03T10:00:00.398Z",
  "message": "scheduling job",
  "logging.googleapis.com/operation": { "id": 1 } // <- always includes context id
}

{
  "severity": "INFO",
  "time": "2022-12-03T10:00:01.000Z",
  "message": "job done",
  "logging.googleapis.com/operation": { "id": 1 } // <- always includes context id
}

Usage

The various logging methods (debug, info, notice, etc.) require two parameters. The first parameter is intended for a human-readable string, while the second parameter captures context (such as JSON objects with circular references).

Example of incorrect use: :x:

this.logger.log(Severity.DEBUG, JSON.stringify(someObject));

Example of correct use: :white_check_mark:

this.logger.debug('received tickets', someObject);

Keywords

FAQs

Package last updated on 25 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc