Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/igrek8/gc-json-logger-nestjs

Enables structured logging for Nest.js application running in Google Cloud platform.
https://github.com/igrek8/gc-json-logger-nestjs

api google-cloud logging

Last synced: about 1 month ago
JSON representation

Enables structured logging for Nest.js application running in Google Cloud platform.

Awesome Lists containing this project

README

        

# Logger for [Structured Logging](https://cloud.google.com/logging/docs/structured-logging) with [Asynchronous Context Tracking](https://nodejs.org/api/async_context.html#class-asynclocalstorage) (Stability: 2 - Stable) to log HTTP traffic in Nest.JS (Express.JS) in GKE

Enables structured logging for Nest.js application running in Google Cloud platform.

[![NPM](https://badgen.net/npm/v/gc-json-logger-nestjs)](https://www.npmjs.com/gc-json-logger-nestjs)
[![Coverage](https://codecov.io/gh/igrek8/gc-json-logger-nestjs/branch/main/graph/badge.svg)](https://codecov.io/gh/igrek8/gc-json-logger-nestjs)
![Release](https://badgen.net/github/checks/igrek8/gc-json-logger-nestjs)
![License](https://badgen.net/github/license/igrek8/gc-json-logger-nestjs)

## Installation

```bash
npm install --save \
gc-json-logger \
gc-json-logger-express \
gc-json-logger-nestjs

yarn add \
gc-json-logger \
gc-json-logger-express \
gc-json-logger-nestjs
```

## View in Google Cloud Monitoring

![Google Cloud Monitoring](./media/google-cloud-logging.png)

## Usage

### [Demo](./demo)

- [./main.ts](./src/demo/main.ts)
- [./demo.module.ts](./src/demo/demo.module.ts)
- [./demo.controller.ts](./src/demo/demo.controller.ts)
- [./demo.controller.test.ts](./src/demo/demo.controller.test.ts)

### Shell

```shell
git clone https://github.com/igrek8/gc-json-logger-nestjs
cd gc-json-logger-nestjs
npm install
npm run dev
```

## Log Structure

```jsonc
{"severity":"INFO","time":"2022-10-08T00:00:00.000Z","message":"Starting Nest application...","logging.googleapis.com/operation":{"id":"app"},"meta":{"context":"NestFactory"}}

{"severity":"INFO","time":"2022-10-08T00:00:00.000Z","message":"LoggerModule dependencies initialized","logging.googleapis.com/operation":{"id":"app"},"meta":{"context":"InstanceLoader"}}

{"severity":"INFO","time":"2022-10-08T00:00:00.000Z","message":"AppModule dependencies initialized","logging.googleapis.com/operation":{"id":"app"},"meta":{"context":"InstanceLoader"}}

{"severity":"INFO","time":"2022-10-08T00:00:00.000Z","message":"AppController {/}:","logging.googleapis.com/operation":{"id":"app"},"meta":{"context":"RoutesResolver"}}

{"severity":"INFO","time":"2022-10-08T00:00:00.000Z","message":"Mapped {/trace, POST} route","logging.googleapis.com/operation":{"id":"app"},"meta":{"context":"RouterExplorer"}}

{"severity":"INFO","time":"2022-10-08T00:00:00.000Z","message":"Nest application successfully started","logging.googleapis.com/operation":{"id":"app"},"meta":{"context":"NestApplication"}}

/* HTTP log and LoggerService use the same UUID in "logging.googleapis.com/operation" */
```

## Use with [`jq`](https://github.com/stedolan/jq)

```bash
node ./server.js | jq -r '."logging.googleapis.com/operation".id as $id | { time, severity, $id, message } | join(" ")'
```

## [Trace Request-Response cycle](https://github.com/igrek8/trace-nestjs#openapi)

[Trace-NestJS](https://github.com/igrek8/trace-nestjs) module adds `X-Request-ID` and `X-Response-ID` headers that match operation id.