///
import { NextFunction, Request, Response } from 'express-serve-static-core';
import pino from 'pino';
export { errSerializer, reqSerializer, resSerializer } from './serializers';
declare module 'express-serve-static-core' {
interface Request {
id: string;
start: Date;
log: pino.Logger;
responseTime: number;
}
interface Response {
body: string;
}
}
declare module 'pino' {
function extreme(): any;
}
interface Error {
status: number;
message: string;
}
/** Function Signature for the optional Request ID Generation Function override. */
export declare type RequestIdFunction = () => string;
/** A set of configuration options used to configure a new instance of ExpressReqLogger. */
export interface ExpressReqLoggerOptions {
/** When set to true, this option will force the log severity to be error for all non 2** response statuses. */
alwaysError?: boolean;
/**
* Allows you to override the default request id generation function.
* By default this is set to the uuidv4 function from the uuid node module.
*/
uuidFunction?: RequestIdFunction;
/** If set to true, this option will stop the X-Request-ID header being added to responses. */
disableIdHeader?: boolean;
/** If set to true, this option will stop the Date header being added to responses. */
disableDateHeader?: boolean;
/** If set to true, this option will stop the X-Response-Time header being added to responses. */
disableResponseTimeHeader?: boolean;
/**
* These options will be passed to pino.
* If no serializers for req, res or err are specified the defaults will be used.
*/
pinoOptions?: pino.LoggerOptions;
/** Configures pino the use extreme mode for added performance. */
extreme?: boolean;
/**
* Allows you to pass your own instance of Pino preconfigured to use in the logging middleware.
* This option is not compatible with pinoOptions or extreme,
* as they don't make sense if you pass a preconfigured pino object.
* If a preconfigured instance is used requests, responses and errors will not be serialized.
*/
pinoInstance?: pino.Logger;
/**
* Log request raw body with debug level
*/
logDebugRequestBody?: boolean;
/**
* Log response raw body with debug level
*/
logDebugResponseBody?: boolean;
}
export declare class ExpressReqLogger {
/** Set to true if the X-Request-ID header should be included in responses. */
private idHeader;
/** Set to true if the Date header should be included in responses. */
private startHeader;
/** The function used to generate the ids used in the X-Request-ID header. */
private uuidFunction;
/** If set to true, the error severity will be used for all non 2xx status responses. */
private alwaysError;
/** The pino logger instance used internally by the module. */
private logger;
private logDebugRequestBody;
private logDebugResponseBody;
/**
* Create a new instance of ExpressReqLogger to use in a Express App.
* @param options - ExpressReqLoggerOptions to configure the middleware.
* @param options.alwaysError - When set to true, the log severity will be error for all non 2xx status responses.
* @param options.uuidFunction - Allows you to override the default X-Request-ID generation function.
* @param options.disableIdHeader - If set to true, the X-Request-ID header is not included in responses.
* @param options.disableDateHeader - If set to true, the Date header is not included in responses.
* @param options.disableResponseTimeHeader - If set to true, the X-Response-Time header isn't included in responses
* @param options.pinoOptions - This object will be passed to pino to configure the logger instance.
* @param options.extreme - If set to true, the extreme mode of pino will be used for extra performance.
* @param options.pinoInstance - Pass in your own preconfigured instance of the pino logger.
* @param options.logDebugRequestBody - Log request raw body with debug level
* @param options.logDebugResponseBody - Log response raw body with debug level
*/
constructor(options?: ExpressReqLoggerOptions);
/**
* This function returns the middleware function for use in express.
*/
getErrorHandlingMiddleware(): (e: Error, req: Request, res: Response, next: NextFunction) => void;
/**
* This function returns the middleware function for use in express.
*/
getMiddleware(): (req: Request, res: Response, next: NextFunction) => Promise;
/**
* This function logs the end of a request and the error that has been caught,
* it also calls another function to calculate and set the X-Response-Time header.
* @param e - Error object
* @param req - The current express request
* @param res - The current express response.
*/
endRequestError(e: Error, req: Request, res: Response): void;
/**
* This function takes a request context and assigns a request id.
* This will either be a newly generated uuid v4 or the X-Request-ID header passed into the request.
* The resulting request id is then set as the X-Request-ID header on the response.
* @param req - The current express request
* @param res - The current express response
*/
private setRequestId;
/**
* This function calcuates the response time and sets the X-Response-Time header.
* @param req - The current express request
* @param res - The current express response.
*/
private setResponseTime;
/**
* This function logs the end of a request,
* it also calls another function to calculate and set the X-Response-Time header.
* @param req - The current express request
* @param res - The current express response.
*/
private endRequest;
private getResponseEndSuffix;
/**
* This function handles all requests and is used as a express middleware.
* @param req - The current express request
* @param res - The current express response.
* @param next - The next function in the middleware stack.
*/
private middleware;
private errorHandlingMiddleware;
}
export default ExpressReqLogger;