@logtape/redaction
Version:
Redact sensitive data from log messages
126 lines (124 loc) • 3.9 kB
TypeScript
import { ConsoleFormatter, TextFormatter } from "@logtape/logtape";
//#region src/pattern.d.ts
/**
* A redaction pattern, which is a pair of regular expression and replacement
* string or function.
* @since 0.10.0
*/
interface RedactionPattern {
/**
* The regular expression to match against. Note that it must have the
* `g` (global) flag set, otherwise it will throw a `TypeError`.
*/
readonly pattern: RegExp;
/**
* The replacement string or function. If the replacement is a function,
* it will be called with the matched string and any capture groups (the same
* signature as `String.prototype.replaceAll()`).
*/
readonly replacement: string | ((match: string, ...rest: readonly any[]) => string);
}
/**
* A redaction pattern for email addresses.
* @since 0.10.0
*/
declare const EMAIL_ADDRESS_PATTERN: RedactionPattern;
/**
* A redaction pattern for credit card numbers (including American Express).
* @since 0.10.0
*/
declare const CREDIT_CARD_NUMBER_PATTERN: RedactionPattern;
/**
* A redaction pattern for U.S. Social Security numbers.
* @since 0.10.0
*/
declare const US_SSN_PATTERN: RedactionPattern;
/**
* A redaction pattern for South Korean resident registration numbers
* (住民登錄番號).
* @since 0.10.0
*/
declare const KR_RRN_PATTERN: RedactionPattern;
/**
* A redaction pattern for JSON Web Tokens (JWT).
* @since 0.10.0
*/
declare const JWT_PATTERN: RedactionPattern;
/**
* A list of {@link RedactionPattern}s.
* @since 0.10.0
*/
type RedactionPatterns = readonly RedactionPattern[];
/**
* Applies data redaction to a {@link TextFormatter}.
*
* Note that there are some built-in redaction patterns:
*
* - {@link CREDIT_CARD_NUMBER_PATTERN}
* - {@link EMAIL_ADDRESS_PATTERN}
* - {@link JWT_PATTERN}
* - {@link KR_RRN_PATTERN}
* - {@link US_SSN_PATTERN}
*
* @example
* ```ts
* import { getFileSink } from "@logtape/file";
* import { getAnsiColorFormatter } from "@logtape/logtape";
* import {
* CREDIT_CARD_NUMBER_PATTERN,
* EMAIL_ADDRESS_PATTERN,
* JWT_PATTERN,
* redactByPattern,
* } from "@logtape/redaction";
*
* const formatter = redactByPattern(getAnsiConsoleFormatter(), [
* CREDIT_CARD_NUMBER_PATTERN,
* EMAIL_ADDRESS_PATTERN,
* JWT_PATTERN,
* ]);
* const sink = getFileSink("my-app.log", { formatter });
* ```
* @param formatter The text formatter to apply redaction to.
* @param patterns The redaction patterns to apply.
* @returns The redacted text formatter.
* @since 0.10.0
*/
declare function redactByPattern(formatter: TextFormatter, patterns: RedactionPatterns): TextFormatter;
/**
* Applies data redaction to a {@link ConsoleFormatter}.
*
* Note that there are some built-in redaction patterns:
*
* - {@link CREDIT_CARD_NUMBER_PATTERN}
* - {@link EMAIL_ADDRESS_PATTERN}
* - {@link JWT_PATTERN}
* - {@link KR_RRN_PATTERN}
* - {@link US_SSN_PATTERN}
*
* @example
* ```ts
* import { defaultConsoleFormatter, getConsoleSink } from "@logtape/logtape";
* import {
* CREDIT_CARD_NUMBER_PATTERN,
* EMAIL_ADDRESS_PATTERN,
* JWT_PATTERN,
* redactByPattern,
* } from "@logtape/redaction";
*
* const formatter = redactByPattern(defaultConsoleFormatter, [
* CREDIT_CARD_NUMBER_PATTERN,
* EMAIL_ADDRESS_PATTERN,
* JWT_PATTERN,
* ]);
* const sink = getConsoleSink({ formatter });
* ```
* @param formatter The console formatter to apply redaction to.
* @param patterns The redaction patterns to apply.
* @returns The redacted console formatter.
* @since 0.10.0
*/
declare function redactByPattern(formatter: ConsoleFormatter, patterns: RedactionPatterns): ConsoleFormatter;
//# sourceMappingURL=pattern.d.ts.map
//#endregion
export { CREDIT_CARD_NUMBER_PATTERN, EMAIL_ADDRESS_PATTERN, JWT_PATTERN, KR_RRN_PATTERN, RedactionPattern, RedactionPatterns, US_SSN_PATTERN, redactByPattern };
//# sourceMappingURL=pattern.d.ts.map