Comments (18)
@dguyonvarch feel free to open pr for this
from nestjs-pino.
No sooner said than done !
#441
With this PR we can now do :
// logger.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { Logger, Params, PARAMS_PROVIDER_TOKEN, PinoLogger } from 'nestjs-pino';
@Injectable()
export class LoggerService extends Logger {
private aProperty: string;
constructor(
pinoLogger: PinoLogger,
@Inject(PARAMS_PROVIDER_TOKEN) params: Params,
aProperty: string
) {
super(pinoLogger, params);
this.aProperty = aProperty
}
aMethod(): void {
...
}
}
and
// logger.module.ts
import { Module } from "@nestjs/common";
import { LoggerModule } from "nestjs-pino";
import { LoggerService } from "./logger.service";
@Module({
exports: [LoggerService],
providers: [LoggerService],
imports: [
LoggerModule.forRoot()
],
})
export class LoggerModule {}
from nestjs-pino.
until PR gets merged, @iamolegga, or @dguyonvarch what is the work around for adding a wrapper around the Logger?
from nestjs-pino.
Thanks, I'll try to find time for this in a week
from nestjs-pino.
I arrived at this issue needing to override pino and extend it's functionality.... I have since solved my issue and here is my working override code that makes the root pino give me override access:
TypeScript
import pino from 'pino'
const myPino = pino()
const orgChildMethod = myPino.child
myPino.child = function(this: any, details: any) {
const newPino = orgChildMethod.call(this, details)
newPino.info = function(logMeta: any): void {
logger.info({...logMeta, ...details}) // our custom override
}
newPino.warn = function(logMeta: any): void {
logger.warning({...logMeta, ...details}) // our custom override
}
return newPino
}
JavaScript
const myPino = require('pino')()
const orgChildMethod = myPino.child
myPino.child = function(details) {
const newPino = orgChildMethod.call(this, details)
newPino.info = function(logMeta) {
logger.info({...logMeta, ...details}) // our custom override
}
newPino.warn = function(logMeta) {
logger.warning({...logMeta, ...details}) // our custom override
}
return newPino
}
from nestjs-pino.
@delai I have copy, pasted, and edited the clean solution we have been running on several projects.
Good luck reading out the details, I just quick and dirty got you the bulk of our solution
import { Module, Logger } from '@nestjs/common'
import { LoggerModule } from 'nestjs-pino'
import pino from 'pino'
@Module({
controllers: [],
providers: [ Logger ],
imports: [
LoggerModule.forRoot({
pinoHttp: {
enabled: true,
logger: getOurPino(logger), // see function below for details
}
})
],
})
export default class AppModule {
}
/** Pino logger functionality override to report to us instead of console
* We use pino to ensure request.id in every log.
* It has a child recursive nature to it that makes overrides a bit complex
*/
export function getOurPino(
logger: any // We want Pino to call something else for every log (our internal logging tool)
): pino.Logger {
const myPino = pino()
const orgChildMethod = myPino.child
myPino.child = function(this: any, details: any) {
const newPino = orgChildMethod.call(this, details)
/* customs overrides */
newPino.info = function(logMeta: any): void {
logger.info({...logMeta, ...details})
}
newPino.warn = function(logMeta: any): void {
logger.warning({...logMeta, ...details})
}
newPino.error = function(logMeta: any): void {
logger.error({...logMeta, ...details})
}
newPino.debug = function(logMeta: any): void {
logger.debug({...logMeta, ...details})
}
/* end: custom overrides */
return newPino
}
return myPino
}
from nestjs-pino.
@iamolegga im on v6 and seems that’s a v7 feature
I will look to check it out
from nestjs-pino.
Hi, easiest way is composition here, but if you want extend it please provide minimal repo for that without business logic as in your example above, I could take a look at it
from nestjs-pino.
Thanks @iamolegga , here is the repo where you can find the simple logger module with controller "Hello World" https://github.com/alexabidri/nestjs-pino--test
from nestjs-pino.
any updates regarding this? I'd also like to add some logic to logger.error
(sending errors to Sentry)
from nestjs-pino.
@orzarchi this can be done with current implementation, see docs of this lib and https://github.com/aandrewww/pino-sentry#usage
from nestjs-pino.
This error is due to pino-params
provider which is not exported by the module : https://github.com/iamolegga/nestjs-pino/blob/master/src/LoggerCoreModule.ts#L61
Any way to export it ? I would like to extend Logger
too for separation of concerns logic.
from nestjs-pino.
Look in the comments @jdt6 I posted a working solution in both TypeScript and JavaScript
from nestjs-pino.
closed in #441
from nestjs-pino.
@AckerApple how can i use the code, run the code in every nestjs controller or service? is there any elegant way?
from nestjs-pino.
@AckerApple may I ask you why an extension of PinoLogger
doesn't fit for you?
Pino logger functionality override to report to us instead of console
you can pass transports as a pinoHttp.stream
field of module settings. But I personally prefer to pipe stdout to the transport in that case
from nestjs-pino.
@AckerApple in this part
logger: getOurPino(logger), // see function below for details
where does this logger
variable come from?
from nestjs-pino.
The short: We have our own company logger so we pass that in there.
The long: “getOurPino()” as seen above, essentially takes Pino logs and casts them to fit our needed format. So the argument “logger” is what our company passes in and one main difference is our company logger uses “warning” instead of “warn” but thats the flexibility we get with “getOurPino()”. So if you needed to cast Pino logs to something of your own format, then copy “getOurPino()” and edited it to your needs.
The data typing for logger, instead of any, should be:
logger: {
info: (…args: any[]) => any,
warning: (…args: any[]) => any,
debug: (…args: any[]) => any,
error: (…args: any[]) => any,
}
from nestjs-pino.
Related Issues (20)
- [QUESTION] Have 'context' key outside of 'nestedKey' object HOT 4
- [FEATURE REQUEST] set the loggers scope values for its lifetime HOT 1
- [QUESTION] Some logs are lost through pino-socket transport HOT 1
- [FEATURE REQUEST] Add support for pino-http's customAttributeKeys.err option HOT 3
- How to inject PinoLogger into TestingModule? HOT 2
- [QUESTION] How to assign additional attributes in context of Nest Microservice? HOT 1
- [QUESTION] Setting context on extended Logger HOT 2
- [BUG] Double `request completed` in my logs HOT 3
- Support pino-http 9.x HOT 1
- Pino Multistream / Multi-transports ? HOT 2
- [BUG] nestjs-pino fails to log HTTP requests with global prefix HOT 1
- [BUG] Pino logger unavailable in nestjs shutdown functions HOT 2
- [BUG] Source maps missed in the package
- [QUESTION] How to use the pino option `customLevels` HOT 1
- [BUG] "is not a function" when trying to extend PinoLogger HOT 2
- [BUG] Automatic logs showing 2 times HOT 1
- [BUG] Redacting modifies the body before it hits the resolver
- [QUESTION] How to import LoggerModule from another module HOT 8
- [BUG] - Custom Logs Placement Issue in pino-nestjs Package HOT 1
- [QUESTION] why are logs delayed/slow when using pino-pretty? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs-pino.