From 4901aaea5df1440a7d63daf600854182d572ff88 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:56:22 +0800 Subject: [PATCH] Import packages/plugin/src/logging.ts --- packages/plugin/src/logging.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 packages/plugin/src/logging.ts diff --git a/packages/plugin/src/logging.ts b/packages/plugin/src/logging.ts new file mode 100644 index 0000000..37eea29 --- /dev/null +++ b/packages/plugin/src/logging.ts @@ -0,0 +1,34 @@ +export type LogLevel = 'debug' | 'info' | 'warn' | 'error' + +export interface LogSink { + debug(message: string): void + info(message: string): void + warn(message: string): void + error(message: string): void +} + +const levels: readonly LogLevel[] = ['debug', 'info', 'warn', 'error'] +const secretKey = /authorization|cookie|token|secret|private|shared|ciphertext|payload|prompt|source|workspace|output|registrationCode|deviceCode/i + +export class SafeLogger { + constructor(private readonly sink: LogSink, private readonly threshold: LogLevel | (() => LogLevel) = 'info') {} + + debug(message: string, fields?: Record): void { this.write('debug', message, fields) } + info(message: string, fields?: Record): void { this.write('info', message, fields) } + warn(message: string, fields?: Record): void { this.write('warn', message, fields) } + error(message: string, fields?: Record): void { this.write('error', message, fields) } + + private write(level: LogLevel, message: string, fields?: Record): void { + const threshold = typeof this.threshold === 'function' ? this.threshold() : this.threshold + if (levels.indexOf(level) < levels.indexOf(threshold)) return + const safeFields = fields === undefined ? '' : ` ${JSON.stringify(redact(fields))}` + this.sink[level](`[ds-harness-remote] ${message}${safeFields}`) + } +} + +function redact(value: unknown, key = ''): unknown { + if (secretKey.test(key)) return '[REDACTED]' + if (Array.isArray(value)) return value.map(item => redact(item)) + if (typeof value !== 'object' || value === null) return value + return Object.fromEntries(Object.entries(value).map(([childKey, child]) => [childKey, redact(child, childKey)])) +}