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

@nbn23/gc-logger

v3.0.1

Published

Google Cloud logger library

Downloads

820

Readme

Google Cloud Logger

gc-logger is a library for managing logs from software components (containers, functions and virtual machines) running in Google Cloud. Based on bunyan, it offers additional features as trace support and log labelling for better traceability.

Getting Started

Installation

Install gc-logger using npm.

npm install @nbn23/gc-logger

Note: gc-logger assumes a TypeScript environment

Usage

Basic usage

Create a Google Cloud logger and log to Stackdriver logs with priority higher or equal to info

import { GoogleCloudLogger, SEVERITY_INFO } from "@nbn23/gc-logger;

const logger = new GoogleCloudLogger({ thresholdLevel: SEVERITY_INFO });

logger.debug("This log is NOT being published (severity debug is under default severity threshold)");
logger.info("This log is being published (severity info is equal to the default severity threshold)");
logger.warn("This log is being published (severity warn, ie, warning is above default severity threshold)");
logger.error("This log is being published (severity error is above default severity threshold)");

Logging to both Stackdriver and console

Create a Google Cloud logger and log to Stackdriver logs with priority higher or equal to info, and send to the console logs with priority higher or equal to debug

import { GoogleCloudLogger, SEVERITY_INFO, SEVERITY_DEBUG } from "@nbn23/gc-logger;

const logger = new GoogleCloudLogger({
    thresholdLevel: SEVERITY_INFO,
    consoleThresholdLevel: SEVERITY_DEBUG
});

logger.debug("This log is being published only in the console (severity debug is equal to the default severity threshold)");
logger.info("This log is being published in Stackdriver and the console");

Logging a JSON payload

Create a Google Cloud logger and log to Stackdriver logs a message plus a JSON payload

import { GoogleCloudLogger, SEVERITY_INFO } from "@nbn23/gc-logger;

const logger = new GoogleCloudLogger({
    thresholdLevel: SEVERITY_INFO
});

logger.info(
    "This log is being published in Stackdriver with an extra 'jsonPayload' field containing JSON data", 
    { 
        myStringProperty: "This is the first property of the JSON data being logged", 
        myNumericProperty: 1,
        myBooleanProperty: true,
        myObjectProperty: {
            foo: "bar"
        }
    }
);

Log labels

Use a default list of labels that will be associated to all the logs

import { GoogleCloudLogger, SEVERITY_INFO, SEVERITY_DEBUG } from "@nbn23/gc-logger;

const myLabels = { transactionId = "MyTransactionId", foo = "bar"};
const logger = new GoogleCloudLogger({
    thresholdLevel: SEVERITY_INFO,
    consoleThresholdLevel: SEVERITY_DEBUG,
    labels: myLabels
});

logger.info("This log is being published in Stackdriver and the labels will be set in the log property labels");

Use a default list of labels that will be associated to all the logs, but log some info overriding them

import { GoogleCloudLogger, SEVERITY_INFO, SEVERITY_DEBUG } from "@nbn23/gc-logger;

const myLabels = {
    transactionId : "MyTransactionId",
    foo : "bar"
};
const logger = new GoogleCloudLogger({
    thresholdLevel: SEVERITY_INFO,
    consoleThresholdLevel: SEVERITY_DEBUG,
    labels: myLabels
});

logger.info("This log is being published in Stackdriver with specific entry labels", undefined, { myCustomLabel: "myCustomLabelValue" });

Use a default list of labels that will be associated to all the logs, but log some info expanding them

import { GoogleCloudLogger, SEVERITY_INFO, SEVERITY_DEBUG } from "@nbn23/gc-logger;

const myLabels = {
    transactionId: "MyTransactionId",
    foo: "bar"
};
const logger = new GoogleCloudLogger({
    thresholdLevel: SEVERITY_INFO,
    consoleThresholdLevel: SEVERITY_DEBUG,
    labels: myLabels
});

const myCustomLabels = {
    customLabel : "myCustomLabelValue"
};
logger.info("This log is being published in Stackdriver with specific entry labels", undefined, { ...logger.getLabels(), ... myCustomLabels});

Log traces

Use a custom Strackdriver Trace trace id

At object instance level:

import { GoogleCloudLogger, SEVERITY_INFO } from "@nbn23/gc-logger;

const logger = new GoogleCloudLogger({
    thresholdLevel: SEVERITY_INFO,
    customTraceKey: "ThisIsMyCustomTraceKey"
});

logger.info("This log is being published with traceId set to 'ThisIsMyCustomTraceKey'");

At log level:

import { GoogleCloudLogger, SEVERITY_INFO } from "@nbn23/gc-logger;

const logger = new GoogleCloudLogger({
    thresholdLevel: SEVERITY_INFO
});

logger.info("This log is being published with traceId set to 'ThisIsMyCustomTraceKey'", undefined, undefined, "ThisIsMyCustomTraceKey");

Using both, with log level trace id taking precedence over object instance level trace id:

import { GoogleCloudLogger, SEVERITY_INFO } from "@nbn23/gc-logger;

const logger = new GoogleCloudLogger({
    thresholdLevel: SEVERITY_INFO,
    customTraceKey: "ThisIsMyDEFAULTCustomTraceKey"
});

logger.info("This log is being published with traceId set to 'ThisIsMyDEFAULTCustomTraceKey'...");
logger.info("...whereas this is being published with traceId set to 'ThisIsMyCustomTraceKey'", undefined, undefined, "ThisIsMyCustomTraceKey");