Child Loggers
Create namespaced loggers for different parts of your application.
Overview
Child loggers help you:
- Organize logs — Group logs by component or feature
- Filter logs — Find logs from specific parts of your app
- Inherit configuration — Share settings with parent logger
Creating Child Loggers
Namespace Hierarchy
Child loggers can be nested to create hierarchies:
Configuration Merge Semantics
Understanding how child logger configuration works is important for predictable behavior.
Merge Behavior
Child loggers merge configuration with their parent using object spread:
Config Properties
| Property | Inheritance Behavior |
|---|---|
level |
Child overrides parent if specified |
enabled |
Child overrides parent if specified |
structured |
Child overrides parent if specified |
sanitize |
Child overrides parent if specified |
context |
Merged (parent + child, child wins conflicts) |
namespace |
Concatenated with : separator |
transports |
Not inherited - use same instance |
Override Level
Context Merging
Context is deeply merged, with child values taking precedence:
Transports Are Shared
Child loggers share the same transport instances as their parent:
Caching Behavior
With No Config (Cached)
Child loggers without config options are cached via WeakRef:
This prevents memory bloat when creating child loggers repeatedly (e.g., per-request).
With Config (Not Cached)
Child loggers with config options are never cached:
Memory Management
Cached children use WeakRef, allowing garbage collection when no references remain:
Tip: For request-scoped logging with unique IDs, use context instead of namespace:
Use Cases
By Feature
By Layer
By Component (React)
Pattern: Service Logger
Common pattern for creating a logger per service:
Filtering by Namespace
Use namespace patterns in your log aggregation:
Or configure transports to filter:
Namespace in Sampling
Use namespace sampling for fine-grained control: