Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Winston prints one-line JSON, or no colour, instead of readable logs

Error{"level":"info","message":"server started"}

Short answer

Winston 3 formats every log as one line of JSON unless you choose another format. For readable console output, set a format on the logger or on the Console transport: colorize first, then simple or printf, or prettyPrint for a multi-line object. Add splat if you use %s placeholders.

  • Applies to winston 3, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the format you really have. If you created the logger without a format option, it is the default JSON one.

    node -e "const w=require('winston');const l=w.createLogger({transports:[new w.transports.Console()]});l.info('hello',{a:1})"
  2. 2Match what you see, then jump to its fix

    1. 1Every line is a single JSON object such as {"level":"info","message":"hello"}No format was set, so the default JSON format is usedFix
    2. 2The level is not coloured, or the output has no colour at allcolorize is placed after the format that builds the textFix
    3. 3Output contains the text %s or %d instead of the value you passedPlaceholders like %s are printed as they areFix

What does the error look like?

  • {"level":"info","message":"server started"}
  • info: test message %s
  • Colours missing, or escape codes printed as text, in the console

Cause 1 of 3

No format was set, so the default JSON format is used

1Check

The logger options table lists format with the default winston.format.json. Search your code for createLogger and check whether it passes a format.

grep -rn "createLogger" src/ --include=*.js --include=*.ts

2Fix

Choose a readable format. Use prettyPrint for a multi-line object, or colorize plus simple for short coloured lines, or printf for your own layout.

const { createLogger, format, transports } = require('winston');
const { combine, colorize, simple } = format;

const logger = createLogger({
  format: combine(colorize(), simple()),
  transports: [new transports.Console()],
});
// info: hello {"a":1}

3Verify

Log a message with metadata and read it in the terminal.

logger.info('hello', { a: 1 });

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

colorize is placed after the format that builds the text

1Check

Formats run in the order given to combine. The README says colorize must come before any formatter that adds the text you want coloured.

grep -rn -B2 -A4 "combine(" src/

2Fix

Move colorize to the front of the combine list. To colour the whole line of a JSON format, pass all: true.

format.combine(
  format.colorize(),
  format.timestamp(),
  format.printf(({ timestamp, level, message }) => `${timestamp} ${level}: ${message}`)
)

3Verify

Run the app in a terminal and check that the level word has a colour.

logger.warn('colour check');

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

Placeholders like %s are printed as they are

1Check

Winston does not run util.format on your message by itself. The README says string interpolation must be switched on with format.splat().

grep -rn "splat" src/

2Fix

Add splat() before the format that writes the text.

const logger = createLogger({
  format: format.combine(format.splat(), format.simple()),
  transports: [new transports.Console()],
});
logger.log('info', 'test message %s', 'my string');

3Verify

The output shows test message my string without the placeholder.

logger.log('info', 'user %s has %d items', 'ali', 3);

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Define one logger module with an explicit format, and import it everywhere, so no file creates a logger with the default.
  • Use pretty console output only in development. The logform docs warn that prettyPrint should not be used in production because of its performance cost; keep the JSON format there, where log tools parse it.
  • Keep the order in combine in mind: colorize first, then timestamp, then the format that writes the string.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.