Logger.ts 907 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import pino from 'pino';
  2. import expressPino, {HttpLogger} from 'pino-http';
  3. import { MessageTypes } from './MessageTypes';
  4. import { StatusCodes } from '../http';
  5. class Logger {
  6. private readonly logger: pino.Logger;
  7. private readonly httpLogger: HttpLogger;
  8. public constructor() {
  9. this.logger = pino({
  10. transport: {
  11. target: 'pino-pretty',
  12. options: {
  13. translateTime: 'yyyy-mm-dd HH:MM:ss.l o',
  14. }
  15. }
  16. });
  17. this.httpLogger = expressPino({
  18. logger: this.logger,
  19. customLogLevel: (req, res, err) => {
  20. if (res.statusCode >= StatusCodes.INTERNAL_SERVER_ERROR || err)
  21. return MessageTypes.ERROR;
  22. if (res.statusCode >= StatusCodes.BAD_REQUEST)
  23. return MessageTypes.WARNING;
  24. return MessageTypes.INFO;
  25. },
  26. });
  27. }
  28. public getLogger(): pino.Logger {
  29. return this.logger;
  30. }
  31. public getHttpLogger(): HttpLogger {
  32. return this.httpLogger;
  33. }
  34. }
  35. export { Logger };