All Articles/Logging

Using Child Loggers for Modular Applications

Create scoped loggers with their own service name, metadata, and tags.


What Are Child Loggers?

A child logger inherits the parent's transport and connection but has its own service name, metadata, and tags. Use them to separate logging concerns across modules.

JavaScript
import { init, child } from '@lognitor/node';

const client = init({ apiKey: 'your-key', service: 'main-app' });

const paymentLogger = child({
  service: 'payment-module',
  metadata: { module: 'payment' },
  tags: ['payments'],
});

paymentLogger.info('Processing payment'); // service: "payment-module"

const stripeLogger = paymentLogger.child({ service: 'stripe-adapter' });
stripeLogger.info('Charge created'); // service: "stripe-adapter"

When to Use Child Loggers

  • Microservices in a monorepo — each service module gets its own logger
  • Plugin systems — each plugin logs under its own service name
  • Request-scoped logging — create a child per request with request-specific metadata

Important Behavior

  • Child tags replace the parent's tags (they don't merge)
  • Child metadata merges with the parent's context
  • All children share the same batch buffer and transport
  • Calling flush() on any logger flushes all pending logs