Skip to content

Redact-fields ​

Create a RecordPatch that rewrites every field whose key appears in a provided key list. Plain values become placeholder. Secret spans detected by sujy123456/lunasieve are replaced in place, leaving the rest of the field value intact.

Interface ​

moonbit
pub fn redact_fields(keys : Array[String], placeholder~ : String = "***") -> RecordPatch {}

input ​

  • keys : Array[String] - Field keys whose values should be redacted.
  • placeholder : String - Replacement value written into each matching field.

output ​

  • RecordPatch - Patch that returns a record with matching field values redacted.

Explanation ​

Detailed rules explaining key parameters and behaviors

  • The patch maps over the record field list and rewrites any field whose key is contained in keys.
  • Values with no detected secret are replaced entirely by placeholder.
  • Detected secret spans are redacted through lunasieve while surrounding text in the same field is kept.
  • This helper is useful for masking bundles such as token, password, and secret with one patch.

How to Use ​

Here are some specific examples provided.

When Mask Several Sensitive Keys ​

When one logger must protect multiple credentials:

moonbit
let logger = Logger::new(console_sink())
  .with_patch(redact_fields(["token", "password", "secret"]))

In this example, every matching field value is masked before the sink sees it.

When Share One Custom Mask ​

When the output should show one explicit policy marker:

moonbit
let patch = redact_fields([
  "access_key",
  "session_key",
], placeholder="[hidden]")

In this example, all matching fields use the same replacement text.

Error Case ​

e.g.:

  • If keys is empty, the patch behaves like a pass-through field mapper.

  • If the same key appears multiple times in keys, matching behavior is unchanged because membership is what matters.

Notes ​

  1. This helper is better than stacking many single-key patches when all keys share the same placeholder policy.

  2. It only rewrites structured fields, not text inside the message body.

Published from the repository docs folder with VitePress.