Skip to content

Structured Logging โ€‹

FLASH DB includes a structured JSON logger for production observability.

Quick Start โ€‹

js
import { logger } from 'flash-zk';

// Automatic structured JSON output to stderr
logger.info('server', 'Flash DB started', { port: 3000, version: '2.1.0' });
// {"timestamp":"2025-01-15T10:30:00.000Z","level":"info","module":"server","message":"Flash DB started","port":3000,"version":"2.1.0"}

logger.warn('engine', 'Corrupt SSTable detected', { file: 'data.arc', reason: 'truncated' });
logger.error('client', 'Encryption failed', { error: 'Invalid key', collection: 'users' });
logger.debug('query', 'Evaluating filter', { filter: { status: 'active' }, results: 42 });

Configuration โ€‹

Set log level via environment variable:

bash
# Default: info (shows info, warn, error)
FLASH_LOG_LEVEL=debug node app.js

# Available levels: debug, info, warn, error

API โ€‹

logger.info(module, message, context?) โ€‹

Logs an informational event.

logger.warn(module, message, context?) โ€‹

Logs a warning (recoverable issue).

logger.error(module, message, context?) โ€‹

Logs an error (failure that needs attention).

logger.debug(module, message, context?) โ€‹

Logs detailed debug information (verbose).

logger.setLevel(level) โ€‹

Dynamically change the log level at runtime.

Sensitive Key Redaction โ€‹

The logger automatically redacts sensitive fields:

  • secretKey, masterKey, password, token, authKey
js
logger.info('auth', 'Login attempt', { password: 'mysecret', username: 'alice' });
// {"level":"info","module":"auth","message":"Login attempt","password":"[REDACTED]","username":"alice"}

Output Format โ€‹

All log lines are single-line JSON (NDJSON), making them easy to parse with tools like:

  • jq (command line)
  • ELK Stack (Elasticsearch, Logstash, Kibana)
  • Datadog, Grafana Loki, CloudWatch Logs
bash
# Pipe to jq for pretty printing
node app.js 2>&1 | jq .

# Filter by level
node app.js 2>&1 | jq 'select(.level == "error")'

# Filter by module
node app.js 2>&1 | jq 'select(.module == "engine")'

Integration with Prometheus โ€‹

For metrics alongside logs, use the FlashMetrics class:

js
import { logger, FlashMetrics } from 'flash-zk';

const metrics = new FlashMetrics();
logger.info('app', 'Request processed', { duration: 42 });
metrics.recordOp('find', 42);

Zero-knowledge encrypted intelligence ยท Apache 2.0