Winston: wrong chars inside .log file `` and `[39m` when using colorize()

Created on 23 Jul 2018  路  4Comments  路  Source: winstonjs/winston

  • _winston version?_

    • [ ] winston@2

    • [x] winston@3

  • _node -v outputs:_ v8.11.1
  • _Operating System?_ (Windows, macOS, or Linux): macOS
  • _Language?_ (all | TypeScript X.X | ES6/7 | ES5 | Dart): ES6

What is the problem?

When I add colorize(), my .log file contains  and [39m.
Why is it happening?

This is my code:

'use strict';

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

const logger = createLogger({
    format: format.combine(
        format.colorize({ all: true }),
        format.simple()
    ),
    transports: [
        new transports.File({ filename: 'combined.log' }),
        new transports.Console({ handleExceptions: true })
    ]
});

logger.info('Hello there. How are you?');
logger.info('Hello again distributed logs', {
    pippo: {
        pluto: 'cio',
        ooo: 2.4,
        sasas: [1, 2, 3],
        ole: {
            prova: true
        }
    }
});
logger.warn('some foobar level-ed message');
logger.error('some baz level-ed message');
logger.silly('some bar level-ed message');

This is the result log:

info: Hello there. How are you?
info: Hello again distributed logs {"pippo":{"pluto":"cio","ooo":2.4,"sasas":[1,2,3],"ole":{"prova":true}}}
warn: some foobar level-ed message
error: some baz level-ed message

Removing this line from my code: format.colorize({ all: true })
My log file become correct:

info: Hello there. How are you?
info: Hello again distributed logs {"pippo":{"pluto":"cio","ooo":2.4,"sasas":[1,2,3],"ole":{"prova":true}}}
warn: some foobar level-ed message
error: some baz level-ed message

What do you expect to happen instead?

Log file shouldn't contains [32m and [39m

Most helpful comment

Fixed using colorize ONLY inside transports.Console, instead of globally. I don't know why but it's working.

...
if (process.env.NODE_ENV !== 'production') {
    logger.add(new transports.Console({
        ...
        format: format.combine(
            format.colorize({
                all: true
            }),
           ...
        ),
    }));
}

All 4 comments

Strange! I think those chars are supposed to be what set the colors, not quotes. Do you know if you've done anything special regarding your terminal environment (is this stock os x terminal, some IDE, etc.), any custom settings? Could be an issue with colorize, not totally sure at first glance.

I think those chars are supposed to be what set the colors, not quotes.
Yes, it's possible.

Nothing special about my terminal. I'm using iterm2, but the same issue happens with the stock terminal of macos.

My terminal configuration is created with this repo, but I don't see anything strange: https://github.com/Ks89/Dotfiles_macOS

Fixed using colorize ONLY inside transports.Console, instead of globally. I don't know why but it's working.

...
if (process.env.NODE_ENV !== 'production') {
    logger.add(new transports.Console({
        ...
        format: format.combine(
            format.colorize({
                all: true
            }),
           ...
        ),
    }));
}

Hello @Ks89 ,

I don't know where colorize is so that I can remove it, here is my code:

  new winston.transports.File({
    format: winston.format.combine(
      winston.format.colorize({ // I added this but it's still not helping
        all: false,
        message: false,
        level: false,
      }),
      winston.format.label({ label: 'API' }),
      winston.format.timestamp(),
      winston.format.printf(({ level, message, label, timestamp }) => {
        return `${timestamp} [${label}] ${level}: ${message}`;
      }),
    ),
    filename: environment.logDirectory,
    level: 'http',
    maxsize: 1024 * 1024 * 10,
  }),

In main.ts, I have

import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));

In AppModule.ts, I have the following:

import { WinstonModule } from 'nest-winston';
...
    WinstonModule.forRoot({
      transports,
    }),

I can't find anywhere else that uses colorize() and I don't know how can I disable it.

Was this page helpful?
0 / 5 - 0 ratings