From 90004e70e125f64b3b510ff765c711b9a7d04f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Gonz=C3=A1lez?= Date: Mon, 29 Jul 2024 18:16:27 -0400 Subject: [PATCH 1/2] doc: add esm examples to node:console --- doc/api/console.md | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/doc/api/console.md b/doc/api/console.md index c9812f055974dc..64b0da059b0387 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -80,7 +80,11 @@ The `Console` class can be used to create a simple logger with configurable output streams and can be accessed using either `require('node:console').Console` or `console.Console` (or their destructured counterparts): -```js +```mjs +import { Console } from 'node:console'; +``` + +```cjs const { Console } = require('node:console'); ``` @@ -132,7 +136,28 @@ Creates a new `Console` with one or two writable stream instances. `stdout` is a writable stream to print log or info output. `stderr` is used for warning or error output. If `stderr` is not provided, `stdout` is used for `stderr`. -```js +```mjs +import { createWriteStream } from 'node:fs'; +import { Console } from 'node:console'; +// Alternatively +// const { Console } = console; + +const output = createWriteStream('./stdout.log'); +const errorOutput = createWriteStream('./stderr.log'); +// Custom simple logger +const logger = new Console({ stdout: output, stderr: errorOutput }); +// use it like console +const count = 5; +logger.log('count: %d', count); +// In stdout.log: count 5 +``` + +```cjs +const fs = require('fs'); +const { Console } = require('node:console'); +// Alternatively +// const { Console } = console; + const output = fs.createWriteStream('./stdout.log'); const errorOutput = fs.createWriteStream('./stderr.log'); // Custom simple logger From cf2b2977180ef431a7c6befb0a62126542837fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Gonz=C3=A1lez?= Date: Mon, 29 Jul 2024 18:29:21 -0400 Subject: [PATCH 2/2] fix: add node prefix to fix linter error --- doc/api/console.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/console.md b/doc/api/console.md index 64b0da059b0387..147a45eadb1203 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -153,7 +153,7 @@ logger.log('count: %d', count); ``` ```cjs -const fs = require('fs'); +const fs = require('node:fs'); const { Console } = require('node:console'); // Alternatively // const { Console } = console;