sachii-safe-logger
v1.2.0
Published
A lightweight TypeScript library to mask sensitive data (emails, credit cards, phone numbers, API tokens) in logs for security and compliance.
Downloads
11
Maintainers
Readme
sachii-safe-logger
A lightweight TypeScript library to automatically detect and mask sensitive data (emails, credit cards, phone numbers, API tokens, SSN, passwords, etc.) in logs for security and compliance.
Installation
npm install sachii-safe-logger🚀 Quick Start - Auto-Mask All Console Logs
Just add ONE line at the top of your app and all console.log calls will automatically mask sensitive data!
// At the very top of your app entry point (index.js, app.js, server.js)
require('sachii-safe-logger').enableGlobalMasking();
// That's it! Now ALL console.log calls anywhere in your app auto-mask sensitive data!
console.log("User email: [email protected]"); // → "User email: j***@example.com"
console.log("Card: 4111111111111111"); // → "Card: ************1111"
console.log("Call 555-123-4567"); // → "Call *******4567"
console.log("SSN: 123-45-6789"); // → "SSN: ***-**-6789"
console.log({ password: "secret123" }); // → { password: "sec****123" }What Gets Auto-Detected & Masked
| Data Type | Example Input | Masked Output |
|-----------|---------------|---------------|
| Email | [email protected] | j***@example.com |
| Credit Card | 4111111111111111 | ************1111 |
| Phone | 555-123-4567 | *******4567 |
| SSN | 123-45-6789 | ***-**-6789 |
| JWT Tokens | eyJhbGci... | eyJh****... |
| Passwords | password=secret | password=****** |
| IP Addresses | 192.168.1.100 | 192.***.***100 |
| AWS Keys | AKIAIOSFODNN7EXAMPLE | AKIA****MPLE |
| Basic Auth | Basic dXNlcjpwYXNz | Basic [REDACTED] |
| URLs with creds | https://user:[email protected] | https://user:********@host.com |
API Reference
enableGlobalMasking(options?)
Enable auto-masking for all console methods globally. Call once at app startup.
const { enableGlobalMasking } = require('sachii-safe-logger');
// Enable with default options (masks everything)
enableGlobalMasking();
// Or customize what to mask
enableGlobalMasking({
maskEmail: true, // default: true
maskPhone: true, // default: true
maskCreditCard: true, // default: true
maskSSN: true, // default: true
maskIP: true, // default: true
maskJWT: true, // default: true
maskPasswords: true, // default: true
maskMAC: false, // default: false
});disableGlobalMasking()
Restore original console behavior.
const { disableGlobalMasking } = require('sachii-safe-logger');
disableGlobalMasking();autoMask(input, options?)
Mask sensitive data in a single string.
const { autoMask } = require('sachii-safe-logger');
const text = "Contact [email protected] or 555-123-4567";
console.log(autoMask(text));
// Output: "Contact j***@example.com or *******4567"autoMaskObject(obj, options?)
Deep-traverse and mask sensitive data in objects.
const { autoMaskObject } = require('sachii-safe-logger');
const user = {
email: "[email protected]",
password: "secret123",
payment: { cardNumber: "4111111111111111" }
};
console.log(autoMaskObject(user));
// Output: { email: "j***@example.com", password: "sec****123", payment: { cardNumber: "************1111" }}createSafeLogger(logger?, options?)
Wrap any logger (console, winston, pino, bunyan) with auto-masking.
const { createSafeLogger } = require('sachii-safe-logger');
const winston = require('winston');
const safeLogger = createSafeLogger(winston);
safeLogger.info("User email: [email protected]"); // Auto-masked!Individual Maskers
const { maskEmail, maskPhone, maskCreditCard, maskToken } = require('sachii-safe-logger');
maskEmail("[email protected]"); // "j***@example.com"
maskPhone("555-123-4567"); // "*******4567"
maskCreditCard("4111111111111111"); // "************1111"
maskToken("sk_live_abc123xyz789"); // "sk_l****9789"Access Regex Patterns
const { PATTERNS } = require('sachii-safe-logger');
// Use patterns for your own validation/detection
PATTERNS.EMAIL // Email regex
PATTERNS.CREDIT_CARD // Credit card regex
PATTERNS.SSN // SSN regex
PATTERNS.JWT // JWT regex
// ... and moreCustom Patterns
Add your own regex patterns:
enableGlobalMasking({
customPatterns: [
{ name: 'employeeId', pattern: /EMP-\d{6}/g },
{ name: 'internalCode', pattern: /INT-[A-Z]{3}-\d{4}/g }
]
});TypeScript Support
Full TypeScript support with type definitions included.
import { enableGlobalMasking, autoMask, AutoMaskOptions } from 'sachii-safe-logger';
const options: AutoMaskOptions = {
maskEmail: true,
maskPhone: false
};
enableGlobalMasking(options);License
MIT
Author
Sachinandan [email protected]
