12345678910111213141516171819202122232425262728293031323334353637383940 |
- import pino from 'pino';
- import expressPino, {HttpLogger} from 'pino-http';
- import { MessageTypes } from './MessageTypes';
- import { StatusCodes } from '../http';
- class Logger {
- private readonly logger: pino.Logger;
- private readonly httpLogger: HttpLogger;
- public constructor() {
- this.logger = pino({
- transport: {
- target: 'pino-pretty',
- options: {
- translateTime: 'yyyy-mm-dd HH:MM:ss.l o',
- }
- }
- });
- this.httpLogger = expressPino({
- logger: this.logger,
- customLogLevel: (req, res, err) => {
- if (res.statusCode >= StatusCodes.INTERNAL_SERVER_ERROR || err)
- return MessageTypes.ERROR;
- if (res.statusCode >= StatusCodes.BAD_REQUEST)
- return MessageTypes.WARNING;
- return MessageTypes.INFO;
- },
- });
- }
- public getLogger(): pino.Logger {
- return this.logger;
- }
- public getHttpLogger(): HttpLogger {
- return this.httpLogger;
- }
- }
- export { Logger };
|