Use decorators for trace your class methods
npm install nestjs-tracer --save
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
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
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
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
- Hidden secret information in logs
class Dummy {
@PrintLog({ parseResult: value => ({ ...value, token: "*********" }) })
foo(secret) {
return { token: "1234", result: { foo: "bar" } };
}
}
class Dummy {
@PrintLog({ parseArguments: (value: any[]) => ["secret*****"] })
foo(secret) {
return { token: "1234", result: { foo: "bar" } };
}
}
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);
// ...
}