Skip to content

Commit

Permalink
Update readme with forceConsole info (#2493)
Browse files Browse the repository at this point in the history
* Update readme with forceConsole info

* Fix TOC rewrite. Oops.

* Argh. Fix TOC again.

* Address PR feedback
  • Loading branch information
neilenns authored Aug 8, 2024
1 parent c4a15ec commit b2b79af
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ transports may produce a high memory usage issue.
* [Using the default logger](#using-the-default-logger)
* [Awaiting logs to be written in `winston`](#awaiting-logs-to-be-written-in-winston)
* [Working with multiple Loggers in `winston`](#working-with-multiple-loggers-in-winston)
* [Routing Console transport messages to the console instead of stdout and stderr](#routing-console-transport-messages-to-the-console-instead-of-stdout-and-stderr)
* [Installation](#installation)
* [Run Tests](#run-tests)

Expand Down Expand Up @@ -148,7 +149,7 @@ const logger = winston.createLogger({
});
```

A logger accepts the following parameters:
A logger accepts the following parameters:

| Name | Default | Description |
| ------------- | --------------------------- | --------------- |
Expand Down Expand Up @@ -260,7 +261,7 @@ Several of the formats in `logform` itself add additional properties:
| `label` | `label()` | Custom label associated with each message. |
| `ms` | `ms()` | Number of milliseconds since the previous log message. |

As a consumer you may add whatever properties you wish – _internal state is
As a consumer you may add whatever properties you wish – _internal state is
maintained by `Symbol` properties:_

- `Symbol.for('level')` _**(READ-ONLY)**:_ equal to `level` property.
Expand Down Expand Up @@ -290,7 +291,7 @@ console.log(SPLAT === Symbol.for('splat'));
// true
```

> **NOTE:** any `{ message }` property in a `meta` object provided will
> **NOTE:** any `{ message }` property in a `meta` object provided will
> automatically be concatenated to any `msg` already provided: For
> example the below will concatenate 'world' onto 'hello':
>
Expand Down Expand Up @@ -380,10 +381,10 @@ const logger = createLogger({
transports: [new transports.Console()]
});

// info: test message my string {}
// info: test message my string {}
logger.log('info', 'test message %s', 'my string');

// info: test message 123 {}
// info: test message 123 {}
logger.log('info', 'test message %d', 123);

// info: test message first second {number: 123}
Expand Down Expand Up @@ -450,7 +451,7 @@ method: `transform(info, opts)` and return the mutated `info`:
They are expected to return one of two things:

- **An `info` Object** representing the modified `info` argument. Object
references need not be preserved if immutability is preferred. All current
references need not be preserved if immutability is preferred. All current
built-in formats consider `info` mutable, but [immutablejs] is being
considered for future releases.
- **A falsey value** indicating that the `info` argument should be ignored by the
Expand Down Expand Up @@ -1198,6 +1199,25 @@ const category1 = container.get('category1');
category1.info('logging to file and console transports');
```

### Routing Console transport messages to the console instead of stdout and stderr

By default the `winston.transports.Console` transport sends messages to `stdout` and `stderr`. This
is fine in most situations; however, there are some cases where this isn't desirable, including:

- Debugging using VSCode and attaching to, rather than launching, a Node.js process
- Writing JSON format messages in AWS Lambda
- Logging during Jest tests with the `--silent` option

To make the transport log use `console.log()`, `console.warn()` and `console.error()`
instead, set the `forceConsole` option to `true`:

```js
const logger = winston.createLogger({
level: 'info',
transports: [new winston.transports.Console({ forceConsole: true })]
});
```

## Installation

``` bash
Expand Down Expand Up @@ -1244,4 +1264,4 @@ npm test
[Charlie Robbins]: https://github.com/indexzero
[Jarrett Cruger]: https://github.com/jcrugzz
[David Hyde]: https://github.com/dabh
[Chris Alderson]: https://github.com/chrisalderson
[Chris Alderson]: https://github.com/chrisalderson

0 comments on commit b2b79af

Please sign in to comment.