Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nestjs-pino

Package Overview
Dependencies
Maintainers
1
Versions
1334
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-pino - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

logo.jpg

18

dist/index.d.ts

@@ -1,4 +0,7 @@

import { LoggerService } from "@nestjs/common";
import express from "express";
import { LoggerService, DynamicModule, NestModule, MiddlewareConsumer } from "@nestjs/common";
import pino, { LoggerOptions, DestinationStream } from "pino";
declare type PassedLogger = {
logger: pino.Logger;
};
declare type Params = [] | [PassedLogger] | [LoggerOptions | DestinationStream] | [LoggerOptions, DestinationStream];
export declare class Logger implements LoggerService {

@@ -16,9 +19,6 @@ verbose(msg: string, ...args: any[]): void;

}
declare type PassedLogger = {
logger: pino.Logger;
};
export declare function createLoggerMiddlewares(): express.Handler[];
export declare function createLoggerMiddlewares(options: PassedLogger): express.Handler[];
export declare function createLoggerMiddlewares(optionsOrStream: LoggerOptions | DestinationStream): express.Handler[];
export declare function createLoggerMiddlewares(options: LoggerOptions, stream: DestinationStream): express.Handler[];
export declare class LoggerModule implements NestModule {
static forRoot(...params: Params): DynamicModule;
configure(consumer: MiddlewareConsumer): void;
}
declare global {

@@ -25,0 +25,0 @@ namespace Express {

@@ -11,2 +11,3 @@ "use strict";

};
var LoggerModule_1;
Object.defineProperty(exports, "__esModule", { value: true });

@@ -19,2 +20,3 @@ const common_1 = require("@nestjs/common");

const loggerKey = "logger";
let rootParams;
let Logger = class Logger {

@@ -41,12 +43,35 @@ verbose(...params) {

exports.Logger = Logger;
function createLoggerMiddlewares(...params) {
if (hasLoggerParamsPassedLogger(params)) {
outOfContextLogger = params[0].logger;
let LoggerModule = LoggerModule_1 = class LoggerModule {
static forRoot(...params) {
rootParams = params;
if (hasLoggerParamsPassedLogger(rootParams)) {
outOfContextLogger = rootParams[0].logger;
}
else {
outOfContextLogger = pino_1.default(...rootParams);
}
return {
module: LoggerModule_1,
providers: [Logger],
exports: [Logger]
};
}
else {
outOfContextLogger = pino_1.default(...params);
configure(consumer) {
consumer
.apply(...createLoggerMiddlewares())
.forRoutes({ path: "*", method: common_1.RequestMethod.ALL });
}
};
LoggerModule = LoggerModule_1 = __decorate([
common_1.Global(),
common_1.Module({
providers: [Logger],
exports: [Logger]
})
], LoggerModule);
exports.LoggerModule = LoggerModule;
function createLoggerMiddlewares() {
return [
express_ctx_1.middleware,
express_pino_logger_1.default(...params),
express_pino_logger_1.default(...rootParams),
(req, res, next) => {

@@ -58,3 +83,2 @@ express_ctx_1.setValue(loggerKey, req.log);

}
exports.createLoggerMiddlewares = createLoggerMiddlewares;
function hasLoggerParamsPassedLogger(params) {

@@ -61,0 +85,0 @@ return params[0] && "logger" in params[0];

{
"name": "nestjs-pino",
"version": "0.1.1",
"version": "0.2.0",
"description": "Pino logger for NestJS",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -1,13 +0,31 @@

# nestjs-pino
<p align="center">
<img alt="NestJS-Pino logo" src="./logo.jpg" width="300" height="300" />
</p>
![npm](https://img.shields.io/npm/v/nestjs-pino)
![Travis (.org)](https://img.shields.io/travis/iamolegga/nestjs-pino)
[![Coverage Status](https://coveralls.io/repos/github/iamolegga/nestjs-pino/badge.svg?branch=master)](https://coveralls.io/github/iamolegga/nestjs-pino?branch=master)
<h1 align="center">NestJS-Pino</h1>
Logging in NestJS with automatic tracing on every layer
<p align="center">
<a href="https://www.npmjs.com/package/nestjs-pino">
<img alt="npm" src="https://img.shields.io/npm/v/nestjs-pino" />
</a>
<a href="https://travis-ci.org/iamolegga/nestjs-pino">
<img alt="Travis (.org)" src="https://img.shields.io/travis/iamolegga/nestjs-pino" />
</a>
<a href="https://coveralls.io/github/iamolegga/nestjs-pino?branch=master">
<img alt="Coverage Status" src="https://coveralls.io/repos/github/iamolegga/nestjs-pino/badge.svg?branch=master" />
</a>
<a href="https://snyk.io/test/github/iamolegga/nestjs-pino">
<img alt="Snyk Vulnerabilities for npm package" src="https://img.shields.io/snyk/vulnerabilities/npm/nestjs-pino" />
</a>
</p>
<p align="center">✨✨✨ Logging in NestJS via Pino with <b>REQUEST CONTEXT IN ANY PLACE</b> ✨✨✨</p>
## Example
In controller:
```ts
// app.controller.ts
import { Logger } from 'nestjs-pino';
@Controller()

@@ -26,4 +44,7 @@ export class AppController {

}
```
// my.service.ts
In service:
```ts
import { Logger } from 'nestjs-pino';

@@ -42,9 +63,7 @@

output:
Output (every log has request context):
```json
{"level":30,"time":1568720266616,"pid":25566,"hostname":"my-host","req":{"id":1,"method":"GET","url":"/","headers":{...},"remoteAddress":"::1","remotePort":53753},"msg":"calling AppController.getHello","v":1}
{"level":20,"time":1568720266616,"pid":25566,"hostname":"my-host","req":{"id":1,"method":"GET","url":"/","headers":{...},"remoteAddress":"::1","remotePort":53753},"msg":"calling MyService.getWorld","v":1}
{"level":30,"time":1568720266623,"pid":25566,"hostname":"my-host","req":{"id":1,"method":"GET","url":"/","headers":{...},"remoteAddress":"::1","remotePort":53753},"res":{"statusCode":200,"headers":{...}},"responseTime":9,"msg":"request completed","v":1}

@@ -59,36 +78,59 @@ ```

## Using
## Register module
### Setup middlewares
### Default params
First of all you shoud setup middlewares, that allows to do:
Just import `LoggerModule` to your module:
- automatic logging of every request/response
- automatic binding each log to it's request context when using `Logger` service
```ts
import { createLoggerMiddlewares } from 'nestjs-pino';
import { LoggerModule } from 'nestjs-pino';
const app = await NestFactory.create(MyModule);
@Module({
imports: [LoggerModule.forRoot()],
...
})
class MyModule {}
```
// basic example
app.use(...createLoggerMiddlewares());
### Configure
// if you want to configure logger somehow depending on your ConfigService
// you can do something like that:
const config = app.get(ConfigService);
app.use(...createLoggerMiddlewares({ level: config.logLevel }));
Also, you can configure it. `forRoot` function has the same API as [express-pino-logger](https://github.com/pinojs/express-pino-logger#api) has (it's the same as [pino itself](https://github.com/pinojs/pino/blob/master/docs/api.md#options) and can take existing logger via `{ logger: pino(...) }`):
```ts
import { LoggerModule } from 'nestjs-pino';
@Module({
imports: [
LoggerModule.forRoot(
{
name: 'add some name to every JSON line',
level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
prettyPrint: process.env.NODE_ENV !== 'production',
useLevelLabels: true,
// and all the others...
},
someWritableStream
)
],
...
})
class MyModule {}
```
`createLoggerMiddlewares` API is the same as [express-pino-logger](https://github.com/pinojs/express-pino-logger#api)
### Extreme mode
### Providing Logger service
If you want to enable `extreme` mode you should read [pino extreme mode docs](https://github.com/pinojs/pino/blob/master/docs/extreme.md#extreme-mode).
Just add `Logger` as provider to your module:
If you are ok with that, so you can configure module like this:
```ts
import { Logger } from 'nestjs-pino';
import * as pino from 'pino';
import { LoggerModule } from 'nestjs-pino';
const dest = pino.extreme();
const logger = pino(dest);
@Module({
providers: [Logger]
imports: [LoggerModule.forRoot({ logger })],
...
})

@@ -98,11 +140,29 @@ class MyModule {}

### Usage as Logger service
Also you can read more about [Log loss prevention](https://github.com/pinojs/pino/blob/master/docs/extreme.md#log-loss-prevention).
## Usage as Logger service
`Logger` implements standard NestJS `LoggerService` interface. So if you are familiar with [built in NestJS logger](https://docs.nestjs.com/techniques/logger) you are good to go.
### Usage as NestJS app logger
```ts
// my.service.ts
import { Logger } from 'nestjs-pino';
@Injectable()
export class MyService {
constructor(private readonly logger: Logger) {}
getWorld() {
this.logger.debug("calling MyService.getWorld");
return "World!";
}
}
```
## Usage as NestJS app logger
```ts
const app = await NestFactory.create(MyModule, { logger: false });
app.useLogger(app.get(Logger));
import { Logger } from 'nestjs-pino';
const app = await NestFactory.create(MyModule, { logger: new Logger() });
```

@@ -109,0 +169,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc