@krainovsd/fastify-logger
Version:
Krainov fastify logger
102 lines (94 loc) • 3.67 kB
TypeScript
import { FastifyBaseLogger, FastifyError, FastifyRequest, FastifyReply, FastifyInstance } from 'fastify';
import { PinoLoggerOptions } from 'fastify/types/logger';
type LoggerServiceConstructorOptions = {
logger: FastifyBaseLogger;
};
type LoggerExecuteOptions = {
start?: boolean;
stop?: boolean;
error?: boolean;
};
type LoggerInfo = {
loggerInfo?: Record<string, unknown>;
};
type LoggerLayerOptions<T> = {
action: () => Promise<T>;
processData?: (data: unknown) => Promise<T>;
loggerExecute?: LoggerExecuteOptions | boolean;
loggerMessage?: string;
} & LoggerInfo;
type LoggerDefaultOptions = {
info?: Record<string, unknown>;
message?: string;
};
type LoggerInfoOptions = LoggerDefaultOptions;
type LoggerWarnOptions = {
error?: unknown;
} & LoggerDefaultOptions;
type LoggerErrorOptions = {
error?: unknown;
} & LoggerDefaultOptions;
type TransportFormat = "logfmt" | "json";
type TransportSettings = {
deniedProperties?: string[];
format?: TransportFormat;
};
type DefineMiddlewareSettings = {
onError?: (error: FastifyError, request: FastifyRequest, reply: FastifyReply) => void;
onRequest?: (request: FastifyRequest, reply: FastifyReply) => void;
onSend?: (request: FastifyRequest, reply: FastifyReply, payload: unknown) => Promise<void>;
onResponse?: (request: FastifyRequest, reply: FastifyReply) => void;
accessLogFilter?: (request: FastifyRequest) => boolean;
errorLogFilter?: (request: FastifyRequest) => boolean;
};
declare class Logger {
private logger;
constructor({ logger }: LoggerServiceConstructorOptions);
loggerLayer<T>({ action, processData, loggerExecute, loggerMessage, loggerInfo, }: LoggerLayerOptions<T>): Promise<T>;
controllerLayer<T = unknown>(action: () => Promise<T>, logger?: boolean): Promise<{
data: T;
status: 200;
success: true;
} | {
data: {
message: string;
};
status: number;
success: false;
}>;
debug({ info, message }: LoggerInfoOptions): void;
info({ info, message }: LoggerInfoOptions): void;
warn({ info, message, error }: LoggerWarnOptions): void;
error({ error, info, message }: LoggerErrorOptions): void;
private isHasLoggerAction;
}
declare function defineTransport(settings?: TransportSettings & {
ext?: string;
}): PinoLoggerOptions["transport"];
declare function defineMiddlewares(fastify: FastifyInstance, settings?: DefineMiddlewareSettings): void;
declare function getTraceId(): string | undefined;
declare function getErrorInfo(err: unknown, stack?: boolean): {
error: {} | undefined;
errorStatus: {} | undefined;
errorDescription: {} | undefined;
errorStack: string | undefined;
traceID: string | undefined;
};
declare function getRequestInfo(req: FastifyRequest): {
host: string;
url: string;
method: string;
traceID: string | undefined;
operationId: string;
};
declare function getResponseInfo(res: FastifyReply): {
status: string | null;
};
declare function getCorrectLog(obj: Record<string, unknown>, deniedProperties?: string[], format?: TransportFormat): void;
declare module "fastify" {
interface FastifyRequest {
operationId?: string;
traceId?: string;
}
}
export { type DefineMiddlewareSettings, Logger, type LoggerErrorOptions, type LoggerExecuteOptions, type LoggerInfoOptions, type LoggerLayerOptions, type LoggerServiceConstructorOptions, type LoggerWarnOptions, type TransportFormat, type TransportSettings, defineMiddlewares, defineTransport, getCorrectLog, getErrorInfo, getRequestInfo, getResponseInfo, getTraceId };