1.2.0 • Published 5 years ago

nestjs-tracer v1.2.0

Weekly downloads
88
License
ISC
Repository
github
Last release
5 years ago

Nestjs tracer

npm package code documentation Maintainability Test Coverage

Use decorators for trace your class methods

Install

npm install nestjs-tracer --save

Usage

PrintLog

import { PrintLog } from "nestjs-tracer";

class Dummy {
  @PrintLog()
  hello(name) {
    return `Hi ${name}`;
  }
}
new Dummy().hello("Foo");
// [Dummy#hello] Call with args: ["Foo"]
// [Dummy#hello] Return: Hi Foo

PrintLog async functions

import { PrintLog } from "nestjs-tracer";

class Dummy {
  @PrintLog()
  async hello(name) {
    return `Hi ${name}`;
  }
}
new Dummy().hello("Foo");
// [Dummy#hello] Call with args: ["Foo"]
// [Dummy#hello] Return: Hi Foo

PrintLogProxy

PrintLog for any instance.

import { PrintLogProxy } from "nestjs-tracer";

import * as fs from "fs";
PrintLogProxy(fs, "existsSync", { className: "Fs" });
fs.existsSync(`./package.json`);
// [Fs#existsSync] Call with args: ["./package.json"]
// [Fs#existsSync] Return: true

Request context

Help to trace called methods in the same request.

Request context, generate one uuid per request.

Install request-context

npm install request-context --save

Example:

  • Configure express app with request-context middleware
// main.ts
import { ContextService, RequestLogger } from "nestjs-tracer/request-context";
async function bootstrap() {
  // ...
  const app = await NestFactory.create(AppModule, {
    logger: false
  });
  app.use(ContextService.middlewareRequest());
  app.use(ContextService.middleware());
  app.useLogger(RequestLogger);
  // ...
}
  • Use PrintLog decorator using the express request context.
import { PrintLog } from "nestjs-tracer/request-context";
class Dummy {
  @PrintLog()
  hello(name) {
    return `Hi ${name}`;
  }
}
new Dummy().hello("Foo");
// f45bg6-56bh-hfc3n-jhu76j [Dummy#hello] Return: Hi Foo

PrintLog Options

  • Hidden secret information in logs

parseResult

class Dummy {
  @PrintLog({ parseResult: value => ({ ...value, token: "*********" }) })
  foo(secret) {
    return { token: "1234", result: { foo: "bar" } };
  }
}

parseArguments

class Dummy {
  @PrintLog({ parseArguments: (value: any[]) => ["secret*****"] })
  foo(secret) {
    return { token: "1234", result: { foo: "bar" } };
  }
}

Advanced

Extend request context

You can create more traces in the middleware

Example:

// main.ts
import { ContextService, RequestLogger } from "nestjs-tracer/request-context";

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { logger: false });
  app.use(ContextService.middlewareRequest());
  app.use(
    ContextService.middleware({
      addTraces(req) {
        this.setTraceByUuid();
        this.set("request:ip", req.ip);
      }
    })
  );
  app.useLogger(RequestLogger);
  // ...
}
1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.4.0

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago