3.0.0 • Published 3 months ago

@nbn23/gc-logger v3.0.0

Weekly downloads
170
License
ISC
Repository
-
Last release
3 months ago

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");
3.0.0

3 months ago

2.1.2

4 months ago

2.1.1

4 months ago

2.1.4

3 months ago

2.1.3

3 months ago

2.1.0

4 months ago

2.0.0

2 years ago

1.1.4

3 years ago

1.1.4-alpha1

3 years ago

1.1.4-alpha

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

1.0.1

3 years ago