@betsys-nestjs/logger
v6.0.1
Published
This library is responsible for all logging in this monorepo. It supports HTTP action logging and nodejs cluster logging (using process messaging). In the background it uses [Winston logger](https://www.npmjs.com/package/winston).
Downloads
75
Maintainers
Keywords
Readme
Logger library
This library is responsible for all logging.
It supports HTTP (expres
) and grpc
action logging and nodejs cluster logging (using process messaging).
In the background it uses Winston logger.
Logger level priorities
We are using npm levels from the winston itself (the lower the number higher the priority is):
error: 0,
warn: 1,
info: 2,
http: 3,
verbose: 4,
debug: 5,
silly: 6
Environment variables
This library requires next ENV variables:
| Variable name | Description |
|----------------------------|-------------------------------------------------------------------------------------------------|
| CONFIG_LOG_LEVEL
| Logger levels with lower priority will be skipped |
| CONFIG_LOG_FORMAT=simple
| Logger format logstash
or simple
(value logstash
will format logs in kibana friendly way) |
Dependencies
| Package | Version | | --------------------------- | ------- | | on-headers | ^1.0.2 | | winston | ^3.3.3 | | @betsys-nestjs/config-utils | ^2.0.0 | | @hapi/joi | ^17.1.1 | | @nestjs/common | ^10.0.0 | | @nestjs/config | ^3.0.0 | | @nestjs/core | ^10.0.0 | | express | ^4.17.1 | | reflect-metadata | ^0.1.12 | | rxjs | ^7.1.0 |
Usage
To start using this library simply import LoggerModule
to your main application module.
import {DATA_RETRIEVER, LoggerModule, DataRetrieverService, ContextTypeEnum} from '@betsys-nestjs/logger';
import {ExpressDefaultDataRetrieverService} from "./express-default-data-retriever.service";
@Module({
imports: [
LoggerModule.forRoot('express', {
provide: DATA_RETRIEVER,
useClass: ExpressDefaultDataRetrieverService,
}),
]
})
class AppModule {
}
You can define whether you want to use express
or grpc
in forRoot
method. For all platforms there are default
data retrievers that are responsible for getting data to log from Request and Response objects based on platform. This
can be specified as second parameter of forRoot
method and you can add any providers you need to inject to module.
DATA_RETRIVER
is defined in provider array and creates a provider with this token. This should be a service that
inherits from exposed interface DataRetriever
.
For express
you can use:
{
provide: DATA_RETRIEVER,
useClass: ExpressDefaultDataRetrieverService,
}
For grpc
you can use:
{
provide: DATA_RETRIEVER,
useClass: GrpcDefaultDataRetrieverService,
}
Then inject Logger
provider wherever you want to log something.
import {LoggerModule} from '@betsys/logger';
class AppService {
constructor(private readonly logger: Logger) {
}
}
If you for some reason do not trust dependency resolver without providing annotation, you can use @InjectLogger()
to
provide explicit logger token.
import {InjectLogger, Logger} from '@betsys/logger';
class AppService {
constructor(@InjectLogger() private readonly logger: Logger) {
}
}
Exclude actions from HTTP logging
If you want to exclude any actions from HTTP logging you can mark them using @LoggerExclude()
.
import {LoggerExclude} from '@betsys/logger';
@Controller()
export class CatsController {
@LoggerExclude()
@Get()
public getHello(): string {
}
}
Or you can mark whole controllers.
import {LoggerExclude} from '@betsys/logger';
@LoggerExclude()
@Controller()
export class CatsController {
}