All files / middlewares performance-logger.ts

100% Statements 34/34
60% Branches 3/5
100% Functions 2/2
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 351x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x  
import { createPinoLogger, logContext } from '../logger-pino'
import { Request, Response, NextFunction } from 'express'
 
export const performanceLoggerMiddleware = (
  microservice: string,
  operation: string,
  environment: 'development' | 'staging' | 'production' | 'local' | 'test'
) => {
  const logger = createPinoLogger({
    serviceName: microservice,
    environment: environment || 'development',
    logLevel: 'info',
  })
 
  return (req: Request, res: Response, next: NextFunction) => {
    const startTime = Date.now()
 
    const originalSend = res.send
    res.send = function (data: unknown) {
      const duration = Date.now() - startTime
 
      logger.info(`⏱️ ${operation} concluída`, {
        ...logContext.request(req),
        ...logContext.performance(operation, duration),
        statusCode: res.statusCode,
        responseSize: (data as string)?.length || 0,
      } as any)
 
      return originalSend.call(this, data)
    }
 
    next()
  }
}