import pino from "pino";

const isDevelopment = process.env.NODE_ENV !== "production";

export const logger = pino({
  level: process.env.LOG_LEVEL || (isDevelopment ? "debug" : "info"),
  redact: {
    paths: [
      // Root level
      "password",
      "passwordHash",
      "token",
      // One level deep (e.g. user.password)
      "*.password",
      "*.passwordHash",
      "*.token",
      // In arrays (for 'args' in decorator)
      "args[*].password",
      "args[*].passwordHash",
      "args[*].token",
    ],
    censor: "[REDACTED]",
  },
  transport: isDevelopment
    ? {
        target: "pino-pretty",
        options: {
          colorize: true,
          translateTime: "SYS:HH:MM:ss",
          ignore: "pid,hostname",
        },
      }
    : undefined,
  base: {
    service: "calchat-server",
  },
});

export function createLogger(module: string) {
  return logger.child({ module });
}

export type Logger = pino.Logger;
