11# Logger
22
3- <!-- introduced_in=v26.0.0 -->
3+ <!-- introduced_in=REPLACEME -->
44
5- > Stability: 1.0 - Early Development
5+ > Stability: 1.1 - Active Development
66
77<!-- source_link=lib/logger.js -->
88
9+ The ` node:logger ` module is available when Node.js is started with the
10+ ` --experimental-logger ` flag.
11+
912The ` node:logger ` module provides high-performance structured logging
1013capabilities for Node.js applications. It uses ` diagnostics_channel ` internally
1114to dispatch log events to consumers, allowing multiple consumers to receive
@@ -100,10 +103,15 @@ const logger = new Logger({
100103added: REPLACEME
101104-->
102105
103- * ` msg ` {string} Log message.
104- * ` obj ` {Object} Object containing ` msg ` property and additional fields.
105- * ` error ` {Error} Error object to log.
106- * ` fields ` {Object} Additional fields to include in the log record.
106+ * ` msg ` {string} The log message. When passed as the first argument, any second
107+ argument is treated as ` fields ` .
108+ * ` obj ` {Object} When passed as the first argument, ` obj.msg ` is used as the
109+ log message and all other properties are merged into the log record as fields.
110+ * ` error ` {Error} When passed as the first argument, the error is serialized
111+ into log fields including ` type ` , ` message ` , and ` stack ` . An optional ` fields `
112+ argument may follow.
113+ * ` fields ` {Object} Additional fields to merge into the log record. Only
114+ applicable alongside ` msg ` or ` error ` , not with ` obj ` .
107115
108116Logs a message at the ` trace ` level.
109117
@@ -123,10 +131,15 @@ logger.trace({ msg: 'Object format', requestId: 'abc123' });
123131added: REPLACEME
124132-->
125133
126- * ` msg ` {string} Log message.
127- * ` obj ` {Object} Object containing ` msg ` property and additional fields.
128- * ` error ` {Error} Error object to log.
129- * ` fields ` {Object} Additional fields to include in the log record.
134+ * ` msg ` {string} The log message. When passed as the first argument, any second
135+ argument is treated as ` fields ` .
136+ * ` obj ` {Object} When passed as the first argument, ` obj.msg ` is used as the
137+ log message and all other properties are merged into the log record as fields.
138+ * ` error ` {Error} When passed as the first argument, the error is serialized
139+ into log fields including ` type ` , ` message ` , and ` stack ` . An optional ` fields `
140+ argument may follow.
141+ * ` fields ` {Object} Additional fields to merge into the log record. Only
142+ applicable alongside ` msg ` or ` error ` , not with ` obj ` .
130143
131144Logs a message at the ` debug ` level.
132145
@@ -145,10 +158,15 @@ logger.debug('Processing request', { requestId: 'abc123' });
145158added: REPLACEME
146159-->
147160
148- * ` msg ` {string} Log message.
149- * ` obj ` {Object} Object containing ` msg ` property and additional fields.
150- * ` error ` {Error} Error object to log.
151- * ` fields ` {Object} Additional fields to include in the log record.
161+ * ` msg ` {string} The log message. When passed as the first argument, any second
162+ argument is treated as ` fields ` .
163+ * ` obj ` {Object} When passed as the first argument, ` obj.msg ` is used as the
164+ log message and all other properties are merged into the log record as fields.
165+ * ` error ` {Error} When passed as the first argument, the error is serialized
166+ into log fields including ` type ` , ` message ` , and ` stack ` . An optional ` fields `
167+ argument may follow.
168+ * ` fields ` {Object} Additional fields to merge into the log record. Only
169+ applicable alongside ` msg ` or ` error ` , not with ` obj ` .
152170
153171Logs a message at the ` info ` level.
154172
@@ -168,10 +186,15 @@ logger.info({ msg: 'User logged in', userId: 123 });
168186added: REPLACEME
169187-->
170188
171- * ` msg ` {string} Log message.
172- * ` obj ` {Object} Object containing ` msg ` property and additional fields.
173- * ` error ` {Error} Error object to log.
174- * ` fields ` {Object} Additional fields to include in the log record.
189+ * ` msg ` {string} The log message. When passed as the first argument, any second
190+ argument is treated as ` fields ` .
191+ * ` obj ` {Object} When passed as the first argument, ` obj.msg ` is used as the
192+ log message and all other properties are merged into the log record as fields.
193+ * ` error ` {Error} When passed as the first argument, the error is serialized
194+ into log fields including ` type ` , ` message ` , and ` stack ` . An optional ` fields `
195+ argument may follow.
196+ * ` fields ` {Object} Additional fields to merge into the log record. Only
197+ applicable alongside ` msg ` or ` error ` , not with ` obj ` .
175198
176199Logs a message at the ` warn ` level.
177200
@@ -190,10 +213,15 @@ logger.warn('High memory usage', { memoryUsage: process.memoryUsage() });
190213added: REPLACEME
191214-->
192215
193- * ` msg ` {string} Log message.
194- * ` obj ` {Object} Object containing ` msg ` property and additional fields.
195- * ` error ` {Error} Error object to log.
196- * ` fields ` {Object} Additional fields to include in the log record.
216+ * ` msg ` {string} The log message. When passed as the first argument, any second
217+ argument is treated as ` fields ` .
218+ * ` obj ` {Object} When passed as the first argument, ` obj.msg ` is used as the
219+ log message and all other properties are merged into the log record as fields.
220+ * ` error ` {Error} When passed as the first argument, the error is serialized
221+ into log fields including ` type ` , ` message ` , and ` stack ` . An optional ` fields `
222+ argument may follow.
223+ * ` fields ` {Object} Additional fields to merge into the log record. Only
224+ applicable alongside ` msg ` or ` error ` , not with ` obj ` .
197225
198226Logs a message at the ` error ` level.
199227
@@ -213,10 +241,15 @@ logger.error(new Error('Request failed'), { requestId: 'abc123' });
213241added: REPLACEME
214242-->
215243
216- * ` msg ` {string} Log message.
217- * ` obj ` {Object} Object containing ` msg ` property and additional fields.
218- * ` error ` {Error} Error object to log.
219- * ` fields ` {Object} Additional fields to include in the log record.
244+ * ` msg ` {string} The log message. When passed as the first argument, any second
245+ argument is treated as ` fields ` .
246+ * ` obj ` {Object} When passed as the first argument, ` obj.msg ` is used as the
247+ log message and all other properties are merged into the log record as fields.
248+ * ` error ` {Error} When passed as the first argument, the error is serialized
249+ into log fields including ` type ` , ` message ` , and ` stack ` . An optional ` fields `
250+ argument may follow.
251+ * ` fields ` {Object} Additional fields to merge into the log record. Only
252+ applicable alongside ` msg ` or ` error ` , not with ` obj ` .
220253
221254Logs a message at the ` fatal ` level.
222255
@@ -231,6 +264,8 @@ logger.fatal(new Error('Unrecoverable error'));
231264added: REPLACEME
232265-->
233266
267+ > Stability: 1.1 - Active Development
268+
234269* ` bindings ` {Object} Additional context fields for the child logger.
235270* ` options ` {Object}
236271 * ` level ` {string} Log level for the child logger.
@@ -613,6 +648,17 @@ added: REPLACEME
613648An object containing ` diagnostics_channel` instances for each log level.
614649Advanced users can subscribe directly to these channels.
615650
651+ The following channel names are used:
652+
653+ | Channel | Log Level |
654+ | ----------- | --------- |
655+ | ` log: trace` | ` trace` |
656+ | ` log: debug` | ` debug` |
657+ | ` log: info` | ` info` |
658+ | ` log: warn` | ` warn` |
659+ | ` log: error` | ` error` |
660+ | ` log: fatal` | ` fatal` |
661+
616662` ` ` js
617663const { channels } = require (' node:logger' );
618664
0 commit comments