Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme with forceConsole info #2493

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]: http://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
Loading