import pino, { type DestinationStream, type Logger, type LoggerOptions, type LogFn } from "pino"; export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal" | "silent"; export type HiveGridLogger = Pick; const REDACT_PATHS = [ "authorization", "headers.authorization", "req.headers.authorization", "request.headers.authorization", "cookie", "headers.cookie", "req.headers.cookie", "request.headers.cookie", "password", "*.password", "token", "*.token", "secret", "*.secret", "apiKey", "*.apiKey", "redisUrl", "*.redisUrl", "connectionString", "*.connectionString", ] as const; const REDACTED = "[Redacted]"; const CIRCULAR = "[Circular]"; const MAX_LOG_DEPTH = 8; const MAX_LOG_ENTRIES = 100; const MAX_LOG_STRING_BYTES = 8 * 1024; const SENSITIVE_KEYS = new Set([ "authorization", "body", "connectionstring", "cookie", "email", "emailaddress", "headers", "ip", "ipaddress", "clientip", "password", "passwd", "payload", "phone", "phonenumber", "recipient", "redisurl", "secret", "session", "sessionid", "setcookie", "templatedata", "to", "token", "accesstoken", "refreshtoken", "idtoken", "apikey", "xapikey", "xcsrftoken", ]); const URL_KEYS = new Set(["href", "httpurl", "referer", "referrer", "url", "urlfull"]); const BEARER_PATTERN = /bearer\s+[^\s,;]+/giu; const CONNECTION_URL_PATTERN = /(?:postgres(?:ql)?|redis):\/\/[^\s,;]+/giu; const SECRET_ASSIGNMENT_PATTERN = /(?:token|secret|password|api[_-]?key|connectionstring|redisurl)=([^\s,;]+)/giu; const ABSOLUTE_URL_QUERY_PATTERN = /(https?:\/\/[^\s?#]+)(?:\?[^\s#]*)?(?:#[^\s]*)?/giu; export interface CreateLoggerOptions extends Omit { level?: LogLevel; stream?: DestinationStream; } export function resolveLogLevel(env: NodeJS.ProcessEnv = process.env): LogLevel { return (env.HIVEGRID_LOG_LEVEL ?? env.LOG_LEVEL ?? "info") as LogLevel; } export function createLogger(options: CreateLoggerOptions = {}): HiveGridLogger { const { level = resolveLogLevel(), stream, hooks: callerHooks, ...pinoOptions } = options; return pino( { ...pinoOptions, base: undefined, level, redact: { paths: [...REDACT_PATHS], censor: REDACTED, }, hooks: { ...callerHooks, logMethod(args, method, numericLevel) { const sanitizedArgs = args.map((argument) => sanitizeLogValue(argument)) as Parameters; if (callerHooks?.logMethod) { callerHooks.logMethod.call(this, sanitizedArgs, method, numericLevel); return; } method.apply(this, sanitizedArgs); }, }, }, stream ); } export function createChildLogger(logger: HiveGridLogger, bindings: Record): HiveGridLogger { return logger.child(sanitizeLogValue(bindings) as Record); } export const defaultLogger = createLogger(); function sanitizeLogValue(value: unknown, key?: string, depth = 0, seen = new WeakSet()): unknown { const normalizedKey = key?.trim().toLowerCase().replace(/[-_.]/gu, ""); if (normalizedKey && SENSITIVE_KEYS.has(normalizedKey)) { return REDACTED; } if (depth > MAX_LOG_DEPTH) { return "[MaxDepth]"; } if (typeof value === "string") { const sanitized = normalizedKey && URL_KEYS.has(normalizedKey) ? stripUrlDetails(value) : redactUnsafeText(value); return truncateString(sanitized); } if (value instanceof Error) { return { name: value.name, message: truncateString(redactUnsafeText(value.message)), ...(value.stack ? { stack: truncateString(redactUnsafeText(value.stack)) } : {}), }; } if (value instanceof Date) { return value.toISOString(); } if (Buffer.isBuffer(value)) { return `[Buffer ${value.byteLength} bytes]`; } if (!value || typeof value !== "object") { return value; } if (seen.has(value)) { return CIRCULAR; } seen.add(value); try { if (Array.isArray(value)) { const entries = value.slice(0, MAX_LOG_ENTRIES).map((entry) => sanitizeLogValue(entry, undefined, depth + 1, seen)); if (value.length > MAX_LOG_ENTRIES) entries.push(`[${value.length - MAX_LOG_ENTRIES} entries omitted]`); return entries; } const entries = Object.entries(value).slice(0, MAX_LOG_ENTRIES); const sanitized = Object.fromEntries(entries.map(([entryKey, entryValue]) => [entryKey, sanitizeLogValue(entryValue, entryKey, depth + 1, seen)])); if (Object.keys(value).length > MAX_LOG_ENTRIES) { sanitized.__omittedEntries = Object.keys(value).length - MAX_LOG_ENTRIES; } return sanitized; } finally { seen.delete(value); } } function redactUnsafeText(value: string): string { return value .replace(BEARER_PATTERN, "Bearer [Redacted]") .replace(CONNECTION_URL_PATTERN, REDACTED) .replace(SECRET_ASSIGNMENT_PATTERN, REDACTED) .replace(ABSOLUTE_URL_QUERY_PATTERN, "$1"); } function stripUrlDetails(value: string): string { try { const absolute = /^[A-Za-z][A-Za-z0-9+.-]*:/u.test(value); const parsed = new URL(value, "https://redaction.invalid"); parsed.username = ""; parsed.password = ""; parsed.search = ""; parsed.hash = ""; return absolute ? parsed.toString() : parsed.pathname; } catch { return redactUnsafeText(value); } } function truncateString(value: string): string { const byteLength = Buffer.byteLength(value, "utf8"); return byteLength <= MAX_LOG_STRING_BYTES ? value : ``; }