log method
Sanitizes and logs a message with an optional level and tag.
In debug mode with sanitizeInDebug: false (the default), this works
exactly like print() — you see all real values for easy debugging.
In release mode (or when sanitizeInDebug: true), PII is automatically
hidden before printing.
LogShield().log('User email: john@test.com', level: 'INFO');
// Debug output: [INFO] User email: john@test.com (real value!)
// Release output: [INFO] User email: [EMAIL HIDDEN] (sanitized!)
Implementation
void log(String message, {String level = 'INFO', String? tag}) {
if (!_shouldOutput(level)) return;
final shouldSanitize = !kDebugMode || _config.sanitizeInDebug;
String displayMessage;
int redactedCount = 0;
if (shouldSanitize) {
final detector = PIIDetector();
final matches = detector.detect(message);
displayMessage = detector.sanitize(message);
redactedCount = matches.length;
} else {
displayMessage = message;
}
final buffer = StringBuffer();
if (_config.showTimestamp) {
buffer.write('[${DateTime.now().toIso8601String()}] ');
}
buffer.write('[$level]');
if (tag != null) {
buffer.write(' [$tag]');
}
buffer.write(' $displayMessage');
if (_config.showRedactionNotice && redactedCount > 0) {
buffer.write(' [LogShield: $redactedCount items redacted]');
}
_output(buffer.toString(), level);
}