# gcp-nodejs-correlate-logs

> Helper module for nodejs applications to correlate logs in Google Cloud Platform

Latest version **1.0.0** (published 2023-11-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install gcp-nodejs-correlate-logs
pnpm add gcp-nodejs-correlate-logs
yarn add gcp-nodejs-correlate-logs
bun add gcp-nodejs-correlate-logs
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-11-20 |
| First published | 2020-03-19 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 12.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Balachander |
| Maintainers | bala-raj |
| Keywords | gcp, logs, nodejs, grouping logs, correlate logs |

## Links

- npm: https://www.npmjs.com/package/gcp-nodejs-correlate-logs
- Repository: https://github.com/Bala-raj/gcp-nodejs-correlate-logs
- Homepage: https://github.com/Bala-raj/gcp-nodejs-correlate-logs#readme
- Issues: https://github.com/Bala-raj/gcp-nodejs-correlate-logs/issues
- npm.io page: https://npm.io/package/gcp-nodejs-correlate-logs

## Dependencies (4)

- [uuid](https://npm.io/package/uuid.md) ^9.0.1
- [bunyan](https://npm.io/package/bunyan.md) ^1.8.15
- [cls-hooked](https://npm.io/package/cls-hooked.md) ^4.2.2
- [@google-cloud/logging-bunyan](https://npm.io/package/@google-cloud/logging-bunyan.md) ^5.0.1

## 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
- [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
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2023-11-20
- 0.0.2 (beta) — 2020-03-19
- 0.0.6 — 2023-11-20
- 0.0.5 — 2021-10-28
- 0.0.4 — 2021-08-07
- 0.0.3 — 2020-04-07
- 0.0.1 — 2020-03-19

## README

**With Google's solution has limitation which let you use only wherever you have access to the request object**  - https://github.com/googleapis/nodejs-logging-bunyan

But with this you can log anywhere.

![alt text](./src/QAz6L.png)

### Step 1:

You need to enable in GCP and use in your application -> [@google-cloud/trace-agent](https://www.npmjs.com/package/@google-cloud/trace-agent)


### Step 2:

This example shown below will push logs only when it is production or staging server. 

For dev also using the bunyan logger but this way it saves a cost instead pushing unnecessary local logs to cloud.

[![Check and do needful for gcp-nodejs-correlate-logs](https://github.com/Bala-raj/gcp-nodejs-correlate-logs/actions/workflows/nodejs.yml/badge.svg)](https://github.com/Bala-raj/gcp-nodejs-correlate-logs/actions/workflows/nodejs.yml)

config/logger.js
```javascript

import { LoggingBunyan } from '@google-cloud/logging-bunyan';
import createLogger from 'gcp-nodejs-correlate-logs';

import { getProjectId, ifDev } from './config';

// Creates a Bunyan Stackdriver Logging client
const loggingBunyan = new LoggingBunyan();

let loggerOption;

if (ifDev()) {
  const bunyanDebugStream = require('bunyan-debug-stream'); // eslint-disable-line
  loggerOption = {
    name: 'rocket-science',
    streams: [{
      level: 'info',
      type: 'raw',
      stream: bunyanDebugStream({
        forceColor: true,
      }),
    }],
    serializers: bunyanDebugStream.serializers,
  };
} else {
  loggerOption = {
    name: 'rocket-science',
    level: 'info',
    streams: [loggingBunyan.stream('info')],
  };
}


const { loggerContextMiddleware, log } = createLogger(getProjectId() || 'dev', loggerOption);

export { loggerContextMiddleware, log };

```


server.js

```javascript
import express from 'express';

// Activate Google Cloud Trace before logging
if (ifLive() || ifStaging())
  require('@google-cloud/trace-agent').start(); 

import config, { APPLICATION_MODE } from './config/config';
import { loggerContextMiddleware, log } from './config/logger';

const app = express();

app.use(loggerContextMiddleware); // Should add this middleware for correlating the logs.

app.listen(config.PORT, () => log.info(`Server listening on port ${config.PORT}... Application Mode: ${APPLICATION_MODE}`));

```

Import the log and use it wherever you want. 

api.js

```javascript
import { Router }  from 'express';

import { log } from './config/logger';

const route = Router();

route.use('/', ( req, res) => { 
  log.info('Seems working');
  res.send('Hello!')
});
```

---
_Source: https://npm.io/package/gcp-nodejs-correlate-logs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
