Skip to content

How do you actually test a custom logger with this? #22

@slidenerd

Description

@slidenerd
  • The examples simple create a stream and pass that to the pino instance and boom it is done
  • But what if your logger has a lot of stuff configured on it and you want to test it
import { randomUUID } from "node:crypto";
import { type LoggerOptions, pino } from "pino";
import { type Options, pinoHttp } from "pino-http";

const loggerOptions: LoggerOptions = {
	enabled: process.env.LOGGING_ENABLED === "true",
	formatters: {
		level(label) {
			return { level: label };
		},
	},
	level: process.env.LOGGING_LEVEL || "info",
	name: process.env.APP_NAME || "app",
	redact: {
		censor: "**REDACTED**",
		paths:
			process.env.LOGGING_REDACTED_KEYS?.split(",")
				.map((key) => key.trim())
				.filter((key) => key) || [],
	},
	serializers: pino.stdSerializers,
	timestamp: pino.stdTimeFunctions.isoTime,
	transport:
		process.env.NODE_ENV !== "production"
			? {
					target: "pino-pretty",
					options: {
						colorize: true,
						translateTime: "UTC:yyyy-mm-dd HH:MM:ss.l o",
					},
				}
			: undefined,
};

export const logger = pino(loggerOptions);

export const createChildLogger = (context: Record<string, unknown>) => {
	return logger.child(context);
};

const httpLoggerOptions: Options = {
	customLogLevel(req, res, err) {
		if (res.statusCode >= 500) {
			return "error";
		}
		if (res.statusCode >= 400) {
			return "warn";
		}
		return "info";
	},
	genReqId: (req) => {
		return req.headers["x-request-id"] || randomUUID();
	},
	logger,
};

export const httpLogger = pinoHttp(httpLoggerOptions);

  • Take this logger/index.ts file for example that exports both a pino logger and a pino-http logger
  • If i wanted to test this, where do I pass that stream?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions