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

Not possible to disable or override default colorizing #209

Closed
ghost opened this issue Jan 22, 2019 · 6 comments
Closed

Not possible to disable or override default colorizing #209

ghost opened this issue Jan 22, 2019 · 6 comments

Comments

@ghost
Copy link

ghost commented Jan 22, 2019

Hi everybody,

Thank you for making Winston even more powerful and easy to use.

I encountered a problem with the colorize feature. It seems to be enabled by default and it's not possible to disable it, or override it by passing a custom format. My logger below :

"dependencies": {
    "winston": "^3.1.0",
    "winston-daily-rotate-file": "^3.5.2"
}
import * as winston from 'winston';
import * as fileRotation from 'winston-daily-rotate-file';

const colorizer = winston.format.colorize();

export const logger = winston.createLogger({
    format: winston.format.combine(
        winston.format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
        winston.format.printf(info => {
            const prefix = `[${info.level}] ${info.timestamp}`;
            return `${colorizer.colorize(info.level, prefix)} ${info.message}`;
        })
    ),
    transports: [
        new fileRotation({
            filename: 'logs/%DATE%.log',
            datePattern: 'YYYY-MM-DD'
        }), 
        new winston.transports.Console()
    ]
});

Result in my logs files :

�[32m[info] 2019-01-22 02:26:32�[39m Application started and listening to port 3000

As you see, I have a custom format which colorizes only the prefix and leaves the message without colors. It's work as expected with the Console transport, but in my logs files the colorize feature seems to be called twice. The whole content is in first colorized by the fileRotation transport (I guess) and the prefix is then colorized by my custom format (visibles characters surrounding the prefix).

Any advices ?

@mattberther
Copy link
Member

I'm not sure I understand, @twaan. The [39m is what terminates the colorization, so your line in the log file (and in my tests) appears to be the expected result. Am I missing something?

@ghost
Copy link
Author

ghost commented Jan 25, 2019

I mean, logs files are colorized even when you don't specify it in the custom format. Like if it was enabled by default. Instead of colorizing the text as expected, my custom format is adding color delimiters around the old ones (thoses added by winston-daily-rotate-file I guess).

As I can't add colors the comment section, imagine that [info] 2019-01-22 02:26:32 is in green. Additional delimiters are my wanted color.

Just test my code, it should reproduce the same result.

Thank you for your response.

@mattberther
Copy link
Member

I've ran your code and I get the same result you do in your log file. Could you put a snippet of what you expect the log file to look like?

Meaning, are you expecting your code to output a log file that looks like:

[info] 2019-01-22 02:26:32 Application started and listening to port 3000

@ghost
Copy link
Author

ghost commented Jan 25, 2019

Because a picture is worth a thousand words, there you have.

This is the expected result (screenshot from the console where it's work). Notice that only the level and the timestamp are colored in (the same) green :

expected

This is the output in the log file. The whole content is already colored, notice the port number colored in blue. The color delimiters are added by my custom format around the level and the timestamp as expected :

actual

@mattberther
Copy link
Member

Ok. I'm not clear as to what's happening here. When I run the code below (which, except for the import vs require, is essentially identical to what you've provided), I do not see the situation you describe.

Test Code

var winston = require('winston');
var fileRotation = require('./index');

const colorizer = winston.format.colorize();

const logger = winston.createLogger({
    format: winston.format.combine(
        winston.format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
        winston.format.printf(info => {
            const prefix = `[${info.level}] ${info.timestamp}`;
            return `${colorizer.colorize(info.level, prefix)} ${info.message}`;
        })
    ),
    transports: [
        new fileRotation({
            filename: 'logs/%DATE%.log',
            datePattern: 'YYYY-MM-DD'
        }),
        new winston.transports.Console()
    ]
});

logger.info('Testing the colorization');

Result

screen shot 2019-01-25 at 5 13 04 pm

Can you try that small sample in a standalone application and see if you still see the issue?

@ghost
Copy link
Author

ghost commented Jan 26, 2019

Okay I just found the reason. I'm using VSCode and it has a default colorization for logs files which causes conflict. That's why I have the expected result by using the command cat in the console, like you did. I would never have suspected such behavior from VSCode.

Anyway, thank you for having took the time to help me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant