---
title: "Set up the Pino logger or Winston in Node.js and keep every error"
description: "The Pino logger and Winston each drop an error in some calls. Here is the setup for each, the log levels, and the error calls that keep the stack, from runs on Node.js 22."
canonical: "https://logbrew.co/en/guides/node-logging"
last_updated: "2026-11-20"
---

# Set up the Pino logger or Winston in Node.js and keep every error

The Pino logger and Winston each drop an error in some calls. Here is the setup for each, the log levels, and the error calls that keep the stack, from runs on Node.js 22.

Tested on November 20, 2026 with Node.js 22.18.0, Pino 10.4.0, Winston 3.19.0, Express 5.3.0, @logbrew/node 0.1.30, @logbrew/sdk 0.1.47, and @logbrew/express 0.1.14

By [Furkan Erday](https://github.com/furkanerday). Furkan Erday is the founder of LogBrew. He builds LogBrew with Claude Code and Codex every day.

Canonical: https://logbrew.co/en/guides/node-logging

## Which error call keeps the error, on screen and in LogBrew

| Logging call | Printed | In LogBrew |
| --- | --- | --- |
| Pino `error(err, msg)` | Message and stack | Name and message |
| Pino `error(msg, err)` | Lost | Lost |
| Winston `error(err)` | `{"level":"error"}` | Name and message |
| Winston `error(err, msg)` | `"message":{}` | Lost |
| Winston `error(msg, { err })` | `"err":{}` | Name and message |
| Winston `error(msg, err)` | Message and stack | Name and message |

6 calls with `new Error("keg empty")` in Pino 10.4 and in Winston 3.19 with no format.

## Set up a Pino logger

### Create 1 logger and let it print JSON

`pino()` logs at `info` and up and prints 1 JSON line per call, with a numeric `level`, `time`, `pid`, and `hostname`. The `debug` call printed nothing. We replaced the host name in the output.

```bash
import pino from "pino";

const logger = pino();

logger.debug("keg pressure read");
logger.info("tap opened");
logger.warn("keg below 10%");
logger.error("keg empty");
logger.fatal("cooler offline");
```

Output

```text
{"level":30,"time":1795160014210,"pid":27152,"hostname":"taproom","msg":"tap opened"}
{"level":40,"time":1795160014211,"pid":27152,"hostname":"taproom","msg":"keg below 10%"}
{"level":50,"time":1795160014211,"pid":27152,"hostname":"taproom","msg":"keg empty"}
{"level":60,"time":1795160014211,"pid":27152,"hostname":"taproom","msg":"cooler offline"}
```

### Read the level from the environment

Run this with `LOG_LEVEL=debug` and the `debug` line appears, with no code change. An empty `LOG_LEVEL` stops the process. `??` keeps the empty text, and Pino throws `default level: must be included in custom levels`. Use `||` if the variable can be empty.

```bash
import pino from "pino";

const logger = pino({ level: process.env.LOG_LEVEL ?? "info" });

logger.debug("keg pressure read");
logger.info("tap opened");
```

Output

```text
{"level":20,"time":1795160014268,"pid":27166,"hostname":"taproom","msg":"keg pressure read"}
{"level":30,"time":1795160014268,"pid":27166,"hostname":"taproom","msg":"tap opened"}
```

### Pass the error first, then the message

Pino turns an `Error` in the first argument, or in an `err` field, into `err` with its `type`, `message`, and `stack`. An error after the message is lost with no warning. The third line has no `err`. We cut each stack after its first line.

```bash
import pino from "pino";

const logger = pino();
const err = new Error("keg empty");

logger.error(err, "pour failed");
logger.error({ err, orderId: "ord_42" }, "pour failed");
logger.error("pour failed", err);
```

Output

```text
{"level":50,"time":1795160014336,"pid":27169,"hostname":"taproom","err":{"type":"Error","message":"keg empty","stack":"Error: keg empty\n    at …"},"msg":"pour failed"}
{"level":50,"time":1795160014337,"pid":27169,"hostname":"taproom","err":{"type":"Error","message":"keg empty","stack":"Error: keg empty\n    at …"},"orderId":"ord_42","msg":"pour failed"}
{"level":50,"time":1795160014337,"pid":27169,"hostname":"taproom","msg":"pour failed"}
```

## Set up a Winston logger

### Create the logger with a Console transport

With no options, `winston.createLogger()` logs at `info` and up and prints JSON. The `debug` call printed nothing, and the lines have no time until you add `winston.format.timestamp()`.

```bash
import winston from "winston";

const logger = winston.createLogger({
  transports: [new winston.transports.Console()]
});

logger.debug("keg pressure read");
logger.info("tap opened");
logger.warn("keg below 10%");
logger.error("keg empty");
```

Output

```text
{"level":"info","message":"tap opened"}
{"level":"warn","message":"keg below 10%"}
{"level":"error","message":"keg empty"}
```

### Add format.errors() before format.json()

With no format, `logger.error(err)` prints `{"level":"error"}`. The JSON format skips the message and the stack of an `Error`. Put `winston.format.errors({ stack: true })` first, and the same call prints both.

```bash
import winston from "winston";

const err = new Error("keg empty");
const plain = winston.createLogger({
  transports: [new winston.transports.Console()]
});
plain.error(err);

const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.errors({ stack: true }),
    winston.format.json()
  ),
  transports: [new winston.transports.Console()]
});
logger.error(err);
```

Output

```text
{"level":"error"}
{"level":"error","message":"keg empty","stack":"Error: keg empty\n    at …"}
```

### Pass the message first, then the error

`logger.error(err, "pour failed")` loses your message. `logger.error("pour failed", { err })` prints `"err":{}`. `logger.error("pour failed", err)` keeps both and adds the error message to yours, as `pour failed keg empty`.

```bash
import winston from "winston";

const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.errors({ stack: true }),
    winston.format.json()
  ),
  transports: [new winston.transports.Console()]
});
const err = new Error("keg empty");

logger.error(err, "pour failed");
logger.error("pour failed", { err });
logger.error("pour failed", err);
```

Output

```text
{"level":"error","message":"keg empty","stack":"Error: keg empty\n    at …"}
{"err":{},"level":"error","message":"pour failed"}
{"level":"error","message":"pour failed keg empty","stack":"Error: keg empty\n    at …"}
```

## Send Pino and Winston logs to LogBrew

### Install the packages and set the key

`@logbrew/node` captures Pino logs, `@logbrew/sdk` holds the Winston transport, and `@logbrew/express` adds request traces. `createLogBrewNodeClient()` reads the key from `LOGBREW_SERVER_API_KEY`.

```bash
npm install pino winston express @logbrew/node @logbrew/sdk @logbrew/express
```

### Call installLogBrewPinoInstrumentation() once

Call it before the first log. Pino still prints its own lines, and LogBrew gets each record with `pinoLevel`, `pinoLevelNumber`, and your fields. LogBrew keeps flat fields only, so the `card` object stayed out. `RecordingTransport` keeps what the SDK sent so we can print it. In your app, leave out `transport`.

```bash
import pino from "pino";
import { RecordingTransport } from "@logbrew/sdk";
import { createLogBrewNodeClient, installLogBrewPinoInstrumentation } from "@logbrew/node";

const transport = RecordingTransport.alwaysAccept();
const client = createLogBrewNodeClient({ transport });
installLogBrewPinoInstrumentation({ client });

const logger = pino({ redact: ["card.number"] }).child({ service: "taproom" });
logger.info({ orderId: "ord_42", card: { number: "4242424242424242" } }, "order paid");
logger.error(new Error("keg empty"), "pour failed");

await client.shutdown();
for (const { attributes: { level, message, metadata } } of JSON.parse(transport.lastBody()).events) {
  console.log(JSON.stringify({ level, message, metadata }));
}
```

Output

```text
{"level":30,"time":1795160014692,"pid":27178,"hostname":"taproom","service":"taproom","orderId":"ord_42","card":{"number":"[Redacted]"},"msg":"order paid"}
{"level":"info","message":"order paid","metadata":{"pinoLevel":"info","context.service":"taproom","context.orderId":"ord_42","pinoLevelNumber":30}}
{"level":"error","message":"pour failed","metadata":{"pinoLevel":"error","context.service":"taproom","pinoLevelNumber":50,"errorName":"Error","errorMessage":"keg empty"}}
{"level":50,"time":1795160014700,"pid":27178,"hostname":"taproom","service":"taproom","err":{"type":"Error","message":"keg empty","stack":"Error: keg empty\n    at …"},"msg":"pour failed"}
```

### Add createLogBrewWinstonTransport() to Winston

Add the transport next to Console. Each record arrives with `winstonLevel`, your `defaultMeta` and fields, and the `errorName` and `errorMessage` of an error.

```bash
import winston from "winston";
import { RecordingTransport, createLogBrewWinstonTransport } from "@logbrew/sdk";
import { createLogBrewNodeClient } from "@logbrew/node";

const transport = RecordingTransport.alwaysAccept();
const client = createLogBrewNodeClient({ transport });

const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.errors({ stack: true }),
    winston.format.json()
  ),
  defaultMeta: { service: "taproom" },
  transports: [new winston.transports.Console(), createLogBrewWinstonTransport({ client })]
});
logger.info("order paid", { orderId: "ord_42" });
logger.error(new Error("keg empty"));

await client.shutdown();
for (const { attributes: { level, message, metadata } } of JSON.parse(transport.lastBody()).events) {
  console.log(JSON.stringify({ level, message, metadata }));
}
```

Output

```text
{"level":"info","message":"order paid","orderId":"ord_42","service":"taproom"}
{"level":"error","message":"keg empty","service":"taproom","stack":"Error: keg empty\n    at …"}
{"level":"info","message":"order paid","metadata":{"winstonLevel":"info","context.service":"taproom","context.orderId":"ord_42"}}
{"level":"error","message":"keg empty","metadata":{"winstonLevel":"error","context.service":"taproom","errorName":"Error","errorMessage":"keg empty"}}
```

### Link each log to its Express request

`logbrewMiddleware()` opens 1 span per request, and `logbrewErrorHandler()` turns a thrown error into an issue with its stack frames. A Pino log inside the request gets the same trace ID. The last column is the start of that ID.

```bash
import express from "express";
import pino from "pino";
import { RecordingTransport } from "@logbrew/sdk";
import { logbrewErrorHandler, logbrewMiddleware } from "@logbrew/express";
import { createLogBrewNodeClient, installLogBrewPinoInstrumentation } from "@logbrew/node";

const transport = RecordingTransport.alwaysAccept();
const client = createLogBrewNodeClient({ transport });
installLogBrewPinoInstrumentation({ client });
const logger = pino();

const app = express();
app.use(logbrewMiddleware({ client }));
app.get("/taps/:tap", (req, res) => {
  logger.info({ tap: req.params.tap }, "tap opened");
  res.json({ ok: true });
});
app.get("/kegs/:keg", () => {
  throw new Error("keg empty");
});
app.use(logbrewErrorHandler({ client }));
app.use((err, req, res, next) => {
  logger.error(err, "request failed");
  res.status(500).json({ error: "internal" });
});

const server = app.listen(3001, async () => {
  await fetch("http://127.0.0.1:3001/taps/ipa");
  await fetch("http://127.0.0.1:3001/kegs/stout");
  server.close();
  await client.shutdown();
  for (const body of transport.sentBodies) {
    for (const { type, attributes: a } of JSON.parse(body).events) {
      console.log(type, a.title ?? a.name ?? a.message, a.status ?? a.level, (a.traceId ?? a.context.trace.traceId).slice(0, 8));
    }
  }
});
```

Output

```text
{"level":30,"time":1795160015082,"pid":27185,"hostname":"taproom","tap":"ipa","msg":"tap opened"}
{"level":50,"time":1795160015140,"pid":27185,"hostname":"taproom","err":{"type":"Error","message":"keg empty","stack":"Error: keg empty\n    at …"},"msg":"request failed"}
log tap opened info 3dc57c32
span GET /taps/:tap ok 3dc57c32
issue GET /kegs/:keg failed error 49e1920b
log request failed error 49e1920b
span GET /kegs/:keg error 49e1920b
```

## What we saw when we tested it

We ran every script on this page with Node.js 22.18.0, Pino 10.4.0, Winston 3.19.0, and Express 5.3.0. The outputs show each payload the LogBrew SDK built. A second run sent the same logs to a LogBrew server, and we read them back.

The LogBrew SDK reads the error from the log record, not from the printed line. So a call can print no error and still send it. When the logger drops the error before it builds the record, or makes the error its message, no transport can get it back.

|  | Pino | Winston |
| --- | --- | --- |
| `level: ""` | Stops at start with an error | Logs every level, `debug` too |
| A new `logger.level` takes effect at once | Yes | Yes |

## Choose between Pino and Winston

Keep the logger your app already uses. LogBrew takes records from both.

Winston has no built-in redaction, so you write your own format for it.

|  | Pino | Winston |
| --- | --- | --- |
| Argument order | `error(err, msg)` | `error(msg, err)` |
| Time on each line | Yes | With `format.timestamp()` |
| Stack from `error(err)` | Yes | With `format.errors()` |
| Built-in redaction | `redact` | No |

## Node.js log levels in Pino and Winston

Pino numbers its levels from `trace` 10 to `fatal` 60, and a higher number is more severe. Winston numbers its levels from `error` 0 to `silly` 6, and a lower number is more severe.

Both log at `info` and up by default. Set `logger.level = "debug"` to change it while the process runs.

The original level stays in `pinoLevel` or `winstonLevel`.

```bash
import pino from "pino";
import winston from "winston";

console.log(pino().levels.values);
console.log(winston.createLogger().levels);
```

Output

```text
{ trace: 10, debug: 20, info: 30, warn: 40, error: 50, fatal: 60 }
{ error: 0, warn: 1, info: 2, http: 3, verbose: 4, debug: 5, silly: 6 }
```

| LogBrew | Pino | Winston |
| --- | --- | --- |
| `info` | `trace` `debug` `info` | `silly` `debug` `verbose` `http` `info` |
| `warning` | `warn` | `warn` |
| `error` | `error` | `error` |
| `critical` | `fatal` | No |

## What LogBrew records

Each Pino or Winston record becomes 1 log with its level, message, original level, and your fields. Fields from a child logger or `defaultMeta` come along, as `context.service` above.

An error in the record adds `errorName` and `errorMessage`. Set `includeErrorStack: true` on either integration to send the stack as `errorStack` too.

The Pino integration drops fields such as `token`, `password`, and `cookie` before it sends a log. The Winston transport sends them as they are, so drop them in a Winston format first.

With the Express middleware, each log carries the trace ID of its request. The issue, the span, and the logs of 1 failed request share that ID.

- `installLogBrewPinoInstrumentation()`
  - `pinoLevel`: `error`
  - `pinoLevelNumber`: `50`
  - `context.service`: `taproom`
  - `errorName`: `Error`
  - `errorMessage`: `keg empty`
- `createLogBrewWinstonTransport()`
  - `winstonLevel`: `error`
  - `context.service`: `taproom`
  - `errorName`: `Error`
  - `errorMessage`: `keg empty`
- `logbrewErrorHandler()`
  - Issue: `GET /kegs/:keg failed`
  - `exception.type`: `Error`
  - `mechanism`: `express.middleware`
  - `handled`: `false`
  - Stack frames: 10

## Questions people ask

### What is the default log level in Pino?

`info`. `pino()` prints `info`, `warn`, `error`, and `fatal`, and drops `debug` and `trace`. Pass `level` to `pino()` or set `logger.level` to change it.

### Why does Winston log an empty message for an error?

The JSON format skips the `message` and `stack` of an `Error`, because they are not enumerable. Add `winston.format.errors({ stack: true })` before `winston.format.json()`.

### How do I log an error with Pino?

Pass it first, as `logger.error(err, "pour failed")`, or in a field, as `logger.error({ err }, "pour failed")`. Pino drops an error that comes after the message.

### Should I use Pino or Winston?

Keep the one your app uses. Pino prints JSON with no setup and has built-in redaction. Winston builds the line from formats you choose. LogBrew takes logs from both.

### Why does Pino throw default level must be included in custom levels?

The level you passed is not a Pino level, often an empty `LOG_LEVEL`. Write `process.env.LOG_LEVEL || "info"`, so an empty value falls back to `info`.

## See every Node.js error with its request

The LogBrew Node SDK sends each Pino or Winston log with its level and fields, and links it to the request trace and the issue. Start on the generous free tier, no card needed.

[Start Free](https://logbrew.co/en)

## Sources

- [Pino docs, Logging method parameters](https://github.com/pinojs/pino/blob/main/docs/api.md#logging-method-parameters)
- [Pino docs, Redaction](https://github.com/pinojs/pino/blob/main/docs/redaction.md)
- [Winston docs, Logging levels](https://github.com/winstonjs/winston/blob/master/README.md#logging-levels)
- [Winston docs, Formats](https://github.com/winstonjs/winston/blob/master/README.md#formats)
- [Express docs, Error handling](https://expressjs.com/en/guide/error-handling/)
- [winstonjs/winston#1338](https://github.com/winstonjs/winston/issues/1338)

## More guides for agents, Python, Node.js, and React

- [Claude Code MCP and Codex MCP server setup](https://logbrew.co/en/guides/mcp-server-claude-code-codex)
- [Use AGENTS.md in Claude Code and Codex](https://logbrew.co/en/guides/agents-md)
- [Fix the Claude overloaded error 529 and rate limit error 429](https://logbrew.co/en/guides/claude-overloaded-rate-limit-error)
- [Set up Python logging with the right log levels and exception capture](https://logbrew.co/en/guides/python-logging)
- [Structured logging with a log format and log levels you can search by field](https://logbrew.co/en/guides/structured-logging)
- [Set up Django logging and keep the traceback of every 500 error](https://logbrew.co/en/guides/django-logging)
- [Set up FastAPI logging and handle errors without losing the traceback](https://logbrew.co/en/guides/fastapi-logging)
- [Add a React error boundary and log the errors it misses](https://logbrew.co/en/guides/react-error-boundary)
- [Trace ID vs span ID, decoded from a real traceparent header](https://logbrew.co/en/guides/trace-id)
- [How to fix errors and set up logs, traces, metrics & releases](https://logbrew.co/en/guides)
