magiclogger - v0.1.0
    Preparing search index...

    Class ContextManager

    ContextManager handles context data for logging.

    Features:

    • Deep merging of context objects
    • Circular reference detection
    • Value sanitization
    • Context validation
    • Snapshot management
    • Performance optimization

    ContextManager

    const contextManager = new ContextManager({
    maxDepth: 5,
    sanitizeMode: 'strict'
    });

    // Set global context
    contextManager.set({
    app: 'my-app',
    version: '1.0.0'
    });

    // Merge additional context
    const merged = contextManager.merge(globalContext, localContext);

    Hierarchy

    • EventEmitter
      • ContextManager
    Index

    Constructors

    Methods

    • Extract specific fields from context.

      Parameters

      • context: Record<string, unknown>

        Source context

      • fields: string[]

        Fields to extract

      Returns Record<string, unknown>

      Extracted context

    • Flatten nested context to dot notation.

      Parameters

      • context: Record<string, unknown>

        Context to flatten

      • Optionalprefix: string = ''

        Key prefix

      Returns Record<string, unknown>

      Flattened context

    • Get global context.

      Returns Record<string, unknown>

      Global context

    • Get context statistics.

      Returns { depth: number; propertyCount: number; size: number; snapshotCount: number }

      Context statistics

    • Merge multiple context objects.

      Parameters

      • ...contexts: (undefined | Record<string, unknown>)[]

        Contexts to merge

      Returns Record<string, unknown>

      Merged context

    • Restore from a snapshot.

      Parameters

      • snapshot: ContextSnapshot

        Snapshot to restore

      Returns void

    • Set global context.

      Parameters

      • context: Record<string, unknown>

        Context to set

      Returns void

    • Sets a schema for context validation.

      Parameters

      • schema: AnySchema

        The schema to use for validation

      • Optionalmode: "warn" | "silent" | "throw"

        Validation mode

      Returns void

      import { object, string, number } from 'magiclogger/validation';

      contextManager.setSchema(
      object({
      userId: string({ format: 'uuid' }),
      sessionId: string(),
      requestCount: number({ min: 0 })
      }),
      'throw' // Strict mode - throw on validation errors
      );
    • Set validation rules.

      Parameters

      • rules: ContextValidationRules

        Validation rules

      Returns void

    • Create a snapshot of current context.

      Parameters

      • Optionalmetadata: Record<string, unknown>

        Optional metadata

      Returns ContextSnapshot

      Created snapshot

    • Unflatten dot notation to nested object. Converts a flat object with dot-notation keys into a nested object structure.

      Parameters

      • flattened: Record<string, unknown>

        Flattened context

      Returns Record<string, unknown>

      Nested context

    • Validate context against rules.

      Parameters

      • context: Record<string, unknown>

        Context to validate

      Returns ContextValidationResult

      Validation result