Redact and Format a Log Line

Hard ⏱ 18 min 26% acceptance ★★★★☆ 4.4
Write a function format_log(level, message, secrets) that builds a log line "[LEVEL] message", but first replaces every occurrence of any string in the list secrets within message with "***". level should be uppercased in the output regardless of how it was passed in.

Examples

Example 1
Input
format_log("info", "user pass123 logged in", ["pass123"])
Output
'[INFO] user *** logged in'
Explanation

The secret substring is redacted and level is uppercased.

Example 2
Input
format_log("error", "no secrets here", [])
Output
'[ERROR] no secrets here'
Explanation

Empty secrets list leaves message untouched.

Constraints

  • secrets is a list of non-empty strings that may or may not appear in message.
  • Redact all occurrences of every secret, not just the first.

Topics

Input & Outputstring processing

Companies

CiscoOracleIBM

Hints

Hint 1

Loop over secrets and use message.replace(secret, "***") for each.

Hint 2

Uppercase level with level.upper() when building the final string.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.