@@ -13,6 +13,43 @@ capabilities for Node.js applications. It uses `diagnostics_channel` internally
1313to dispatch log events to consumers, allowing multiple consumers to receive
1414logs independently.
1515
16+ ## Why ` node:logger ` instead of ` console `
17+
18+ The built-in ` console.log() ` and related ` console ` APIs are designed for
19+ simple text output and debugging during development. They write directly to
20+ stdout/stderr as unstructured text, provide no log levels beyond
21+ ` log ` /` warn ` /` error ` /` debug ` , and offer no mechanism for routing logs to
22+ different destinations or filtering by severity in production environments.
23+
24+ ` node:logger ` addresses these limitations with capabilities that production
25+ applications typically require:
26+
27+ * ** Structured output** : Log records are emitted as structured objects (e.g.,
28+ JSON) rather than plain text, making them machine-parseable and compatible
29+ with log aggregation systems like Elasticsearch, Datadog, and Splunk.
30+ * ** Log levels with filtering** : Six severity levels (` trace ` through ` fatal ` )
31+ with numeric ordering allow fine-grained control over which logs are emitted.
32+ Both loggers and consumers can set independent minimum levels, so debug logs
33+ can be written to a file without appearing on the console.
34+ * ** Producer-consumer separation via ` diagnostics_channel ` ** : Loggers (producers)
35+ and consumers are decoupled through ` diagnostics_channel ` . Application code
36+ logs without knowing where logs go; consumers decide the destination (stdout,
37+ files, network). Multiple consumers can process the same log records
38+ independently.
39+ * ** Child loggers with context propagation** : ` logger.child() ` creates loggers
40+ that automatically include inherited context fields (e.g., ` requestId ` ,
41+ ` service ` ) in every log record, eliminating the need to manually pass context
42+ through call chains.
43+ * ** Serializers and the ` serialize ` symbol** : Custom serializers and the
44+ ` [serialize]() ` symbol ensure sensitive data (passwords, tokens) is excluded
45+ from logs and complex objects are reduced to loggable representations without
46+ manual transformation at each call site.
47+ * ** Zero-cost level checks** : ` logger.debug.enabled ` allows skipping expensive
48+ computation when a level is disabled, something ` console ` does not support.
49+ * ** No third-party dependency required** : ` node:logger ` provides structured
50+ logging out of the box, removing the need for userland loggers like pino
51+ or winston for common use cases.
52+
1653``` mjs
1754import { Logger , JSONConsumer } from ' node:logger' ;
1855
0 commit comments