Gathering detailed insights and metrics for nestjs-pino-stackdriver
Gathering detailed insights and metrics for nestjs-pino-stackdriver
Gathering detailed insights and metrics for nestjs-pino-stackdriver
Gathering detailed insights and metrics for nestjs-pino-stackdriver
npm install nestjs-pino-stackdriver
2.2.1 Automatic remove labels after each log deactivated
Published on 23 Nov 2021
2.2.0 Added custom labels
Published on 23 Nov 2021
1.2.2
Published on 04 Nov 2021
1.2.1
Published on 04 Nov 2021
2.1.3 Bugfix in typing
Published on 17 Aug 2021
Bugfix: system logger stackdriver config
Published on 17 Aug 2021
Module System
Unable to determine the module system for this package.
Min. Node Version
Typescript Support
Node Version
NPM Version
18 Stars
78 Commits
3 Forks
9 Watching
19 Branches
5 Contributors
Updated on 28 Nov 2023
TypeScript (99.04%)
JavaScript (0.73%)
Shell (0.23%)
Cumulative downloads
Total Downloads
Last day
-18.2%
54
Compared to previous day
Last week
70.4%
397
Compared to previous week
Last month
18.5%
1,283
Compared to previous month
Last year
-2.8%
15,328
Compared to previous year
14
2
Pino logger for Nestjs that logs context and execution-context labels using nest-context
The logger exported in this project implements the LoggerService interface of Nest and includes the logger context, as the default logger implementation in Nest. It can be configured to log labels from the execution context of the application.
Furthermore:
Include the module as an import into your main module:
1import { Module } from '@nestjs/common'; 2import { CqrsModule } from '@nestjs/cqrs'; 3import { LoggerModule } from 'nestjs-pino-context'; 4import { ExampleController } from './example.controller'; 5import { ExampleHandler } from './command/handler/example.handler'; 6 7@Module({ 8 imports: [ 9 CqrsModule, 10 LoggerModule, 11 ], 12 controllers: [ExampleController], 13 providers: [ExampleHandler], 14}) 15export class ExampleModule {}
Now you can inject the logger in your providers or controllers and use it:
1import { Controller, Post, Body } from '@nestjs/common'; 2import { CommandBus } from '@nestjs/cqrs'; 3import { Logger } from 'nestjs-pino-context'; 4import { ExampleCommand } from './command/impl/example.command'; 5 6@Controller() 7export class ExampleController { 8 constructor( 9 private readonly commandBus: CommandBus, 10 private readonly logger: Logger, 11 ) { 12 // if you do not call setContext, "ExampleController" will be used as context 13 // logger.setContext('my custom context'); 14 } 15 16 @Post('/example') 17 async example( 18 @Body() 19 command: ExampleCommand, 20 ) { 21 this.logger.verbose('Simple verbose message'); 22 this.logger.debug({ 23 msg: 'Object-like debug message', 24 sample: 'another field', 25 }); 26 this.logger.warn('Warning passing custom context', 'custom-context'); 27 this.logger.setLabel( 28 'my-custom-label', 29 'my-custom-label for my logger.error', 30 ); 31 this.logger.error( 32 'Error', 33 `An error trace`, 34 ); 35 this.logger.log( 36 'An interpolation message: %o correlation-id %s', 37 undefined, 38 { try: 1 }, 39 'xxx', 40 ); 41 42 return this.commandBus.execute(command); 43 } 44}
You can use the logger to log your application logs:
1import { NestFactory } from '@nestjs/core'; 2import { GcloudTraceService } from 'nestjs-gcloud-trace'; 3import { createLoggerTool, createStackdriverLoggerTool } from 'nestjs-pino-context'; 4import { MyModule } from './my.module'; 5 6async function bootstrap() { 7 const app = await NestFactory.create(MyModule); 8 9 // Pass the app and it will instantiate your logger from the DI 10 app.useLogger(createStackdriverLoggerTool(app)); 11 return await app.listen(3000); 12} 13GcloudTraceService.start(); 14bootstrap();
You can use also use the logger to log your application + initialization logs:
1import { NestFactory } from '@nestjs/core'; 2import { GcloudTraceService } from 'nestjs-gcloud-trace'; 3import { createLoggerTool } from 'nestjs-pino-context'; 4import { MyModule } from './my.module'; 5import { myLoggerConfig } from './my-logger.config'; 6 7async function bootstrap() { 8 const app = await NestFactory.create(MyModule, {logger: createStackdriverLoggerTool()}); 9 // You could also use a custom logger createLoggerTool instead of the stackdriver one 10 const app2 = await NestFactory.create(MyModule, {logger: createLoggerTool(myLoggerConfig)}); 11 12 return await app.listen(3000); 13} 14GcloudTraceService.start(); 15bootstrap();
When you register the Logger, you can pass as configuration either a string representing the name of
one of the bundled configurations (Fex: 'stackdriver'
), or an object containing zero or more of:
1import { Module } from '@nestjs/common'; 2import { CqrsModule } from '@nestjs/cqrs'; 3import { LoggerModule } from 'nestjs-pino-context'; 4import { ExampleController } from './example.controller'; 5import { ExampleHandler } from './command/handler/example.handler'; 6 7@Module({ 8 imports: [ 9 CqrsModule, 10 LoggerModule.register({ 11 12 }), 13 ], 14 controllers: [ExampleController], 15 providers: [ExampleHandler], 16}) 17export class ExampleModule {}
The default Logger
uses the stackdriver as default bundled configuration.
If you want an empty configuration by default, you can use PinoContextLogger
instead:
GcloudTraceModule uses internally nest-context to store the trace
url (something like "projects/
Furthermore, it allows to:
Include the module as an import into your main module:
1import { Module } from '@nestjs/common'; 2import { ConfigModule } from '@nestjs/config'; 3import { GcloudTraceModule } from 'nestjs-gcloud-trace'; 4import { ExampleController } from './example.controller'; 5 6@Module({ 7 imports: [ConfigModule.forRoot(), GcloudTraceModule], 8 controllers: [ExampleController], 9 providers: [], 10}) 11export class ExampleModule {}
Call GcloudTraceService::start (static method) before your Nest bootstrap:
1import { NestFactory } from '@nestjs/core'; 2import { GcloudTraceService } from 'nestjs-gcloud-tracer'; 3import { ExampleModule } from './example.module'; 4 5async function bootstrap() { 6 const app = await NestFactory.create(ExampleModule); 7 await app.listen(9191); 8} 9GcloudTraceService.start(); 10bootstrap();
Now now you can the gcloud trace context id from your headers and you can use the context key exported within this module to get the current trace url from the default context:
1import { Controller, Get } from '@nestjs/common'; 2import { Context } from 'nestjs-context'; 3import { 4 CONTEXT_GCLOUD_TRACE, 5 HEADER_GCLOUD_TRACE_CONTEXT_ID, 6} from 'nestjs-gcloud-trace/constants'; 7 8@Controller() 9export class ExampleController { 10 constructor( 11 private readonly context: Context, 12 ) {} 13 14 @Get('/example') 15 async example(@Headers('HEADER_GCLOUD_TRACE_CONTEXT_ID') header: string) { 16 return `Your Gcloud Trace url is ${this.context.get( 17 CONTEXT_GCLOUD_TRACE, 18 )} and your current context id is ${header}`; 19 } 20}
You can also use the gcloud trace agent directly:
1import { Controller, Get } from '@nestjs/common'; 2import { GcloudTraceService } from 'nestjs-gcloud-trace'; 3 4@Controller() 5export class ExampleController { 6 constructor( 7 private readonly gcloudTracerService: GcloudTraceService, 8 ) {} 9 10 @Get('/example') 11 async example() { 12 return `Your Gcloud trace current context id is ${this.gcloudTracerService.get().getCurrentContextId(); 13 } 14}
You may want one, multiple or all requests to be traced by Gcloud Trace: this module includes a middleware that allows to filter requests to force them to be traced:
1import { NestFactory } from '@nestjs/core'; 2import { Request } from 'express'; 3import { GcloudTraceService, forceRequestToBeTracedMiddleware } from 'nestjs-gcloud-trace'; 4import { ExampleModule } from './example.module'; 5 6async function bootstrap() { 7 const app = await NestFactory.create(ExampleModule); 8 // example: force all "GET" requests to be traced 9 app.use( 10 forceRequestToBeTracedMiddleware( 11 app.get(GcloudTraceService), 12 (req: Request) => req.method === 'GET', 13 ), 14 ); 15 await app.listen(9191); 16} 17GcloudTraceService.start(); 18bootstrap();
Create an issue.
There is a full working example in the directory "example" of this project (here!).
Use "yarn start" to execute the example script (from the "example" directory):
yarn install
NODE_ENV=development yarn start
Now you can open another terminal and execute a curl to see the results of a POST:
curl --location --request POST 'http://127.0.0.1:9191/example/param-id' \
--header 'Content-Type: application/json' \
--data-raw '{
"id": "body-id",
"override-id": "override-id",
"deep": {"id": "deep-id"}
}'
Try now with a different NODE_ENV environment variable and a different CURL, for example:
curl --location --request POST 'http://127.0.0.1:9191/example/param-id?fallback-id=ok' \
--header 'Content-Type: application/json' \
--data-raw '{
"id": "body-id",
"override-id": "override-id",
"block-override": 1
}'
If you want to include GCloud Trace:
curl --location --request GET 'http://127.0.0.1:9191/example'
https://console.cloud.google.com/traces/list?project=<project-id>&tid=<trace-id>
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
Found 5/22 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
81 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More