# @nbn23/gc-logger

> Google Cloud logger library

Latest version **3.0.1** (published 2024-07-23) · ISC license · 179 weekly downloads

## Install

```sh
npm install @nbn23/gc-logger
pnpm add @nbn23/gc-logger
yarn add @nbn23/gc-logger
bun add @nbn23/gc-logger
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2024-07-23 |
| First published | 2020-10-26 |
| Weekly downloads | 179 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 22.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | NBN23 |
| Maintainers | nbn, maraat |
| Keywords | Google Cloud, logging, logger |

## Links

- npm: https://www.npmjs.com/package/@nbn23/gc-logger
- npm.io page: https://npm.io/package/@nbn23/gc-logger

## Dependencies (2)

- [@google-cloud/logging](https://npm.io/package/@google-cloud/logging.md) ^11.0.0
- [@google-cloud/trace-agent](https://npm.io/package/@google-cloud/trace-agent.md) ^8.0.0

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [@luma.gl/experimental](https://npm.io/package/@luma.gl/experimental.md) — 77.1K weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads

## Recent versions

- 3.0.1 (latest) — 2024-07-23
- 3.0.0 — 2024-01-08
- 2.1.4 — 2024-01-03
- 2.1.3 — 2024-01-03
- 2.1.2 — 2024-01-02
- 2.1.1 — 2024-01-02
- 2.1.0 — 2024-01-02
- 2.0.0 — 2022-06-17
- 1.1.4 — 2021-02-25
- 1.1.4-alpha1 — 2021-02-24
- 1.1.4-alpha — 2021-02-24
- 1.1.3 — 2021-02-23
- 1.1.2 — 2021-02-22
- 1.1.1 — 2021-02-22
- 1.1.0 — 2021-02-22
- … 1 more at https://npm.io/package/@nbn23/gc-logger/versions

## 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.

```sh
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

```ts
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

```ts
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

```ts
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

```ts
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

```ts
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

```ts
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:

```ts
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:

```ts
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:

```ts
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");
```

---
_Source: https://npm.io/package/@nbn23/gc-logger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
