import pino from "pino";
import { mask } from "./mask";
import { env } from "@/env";

const serializers: { [key: string]: pino.SerializerFn } = {
	password: (payload) => {
		if (payload) {
			return env.IS_DEV
				? mask(payload)
				: mask(payload, {
						visibleStart: 0,
						visibleEnd: 0,
					});
		}

		return payload;
	},
	email: (payload) => {
		if (payload) {
			return env.IS_DEV ? payload : mask(payload);
		}
		return payload;
	},
};

export const baseLogger = pino({
	level: process.env.LOG_LEVEL || "info",
	timestamp: pino.stdTimeFunctions.isoTime,
	serializers,
});

export const loggerModule = (loggerName: string) => {
	return baseLogger.child({ module: loggerName });
};
