dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.2k stars 794 forks source link

Console color no more working #1570

Closed smasilamani-cfins closed 2 years ago

smasilamani-cfins commented 2 years ago

Bug Report

We recently migrated the serverless framework to the latest version from v8.8.1 and stared noticing that none of our log statements are printed in color. With versions upto 8.8.1, we used to see info in green, warn in yellow and error in red color. We use the following logging library https://github.com/baryon/tracer

We confirm that this started happening from V9.0.0 to the latest version.

Current Behavior

Console log colors are no more working.

Sample Code


import { EOL } from 'os';
import { Tracer, colorConsole } from 'tracer';

import { Injectable } from '@nestjs/common';
import { DSConfig } from '@shared/config/app.config';

@Injectable()
export class DSLogger {
    private logger: Tracer.Logger<any> = null;

    constructor(private readonly dsConfig: DSConfig) {}

    /**
     * Function to configure Tracer. Note : This has to be called before start using log methods.
     *
     * This function also logs messages into a log file inaddition to the console.
     *
     * The log file configured to be rotated every day.
     *
     * @memberof DSLogger
     */
    config(): void {
        this.logger = colorConsole({
            level: this.dsConfig.logger.level,
            stackIndex: 2,
            format: [
                '{{timestamp}} [{{title}}] - {{message}} (in {{file}}:{{line}})', // default format
                {
                    error: '{{timestamp}} [{{title}}] - {{message}} (in {{file}}:{{line}})' + EOL + 'Call Stack:' + EOL + '{{stack}}' // error format
                }
            ],
            dateformat: 'yyyy-mm-dd HH:MM:ss.l'
        });
    }

    /**
     * Log message at trace level
     *
     * @param {any} message
     * @memberof DSLogger
     */
    trace(message: any): void {
        this.logger.trace(message);
    }

    /**
     * Log message at debug level
     *
     * @param {any} message
     * @memberof DSLogger
     */
    debug(message: any): void {
        this.logger.debug(message);
    }

    /**
     * Log message at info level
     *
     * @param {any} message
     * @memberof DSLogger
     */
    info(message: any): void {
        this.logger.info(message);
    }

    /**
     * Log message at warn level
     *
     * @param {any} message
     * @memberof DSLogger
     */
    warn(message: any): void {
        this.logger.warn(message);
    }

    /**
     * Log message at error level
     *
     * @param {any} message
     * @memberof DSLogger
     */
    error(message: any): void {
        this.logger.error(message);
    }

    /**
     * Log message at fatal level
     *
     * @param {any} message
     * @memberof DSLogger
     */
    fatal(message: any): void {
        this.logger.fatal(message);
    }
}

Expected behavior/code

Color console should work as expected.

Environment

Additional context/Screenshots

With V8.8.1

image

With V10.0.0 image

dnalborczyk commented 2 years ago

thank you for filing the issue @saachinsiva

the reason you don't see any colors in v9+ anymore is that the lambda handlers now run in a worker thread. it seems the color library tracer you are not using has a bug and can't detect color support in worker threads. I just tried chalk and it seems to have a similar problem. it appears to be a wide spread problem which should probably be fixed.

if you run this code snippet in your handler you will likely see colors:

console.log('\x1b[36m%s\x1b[0m', 'I am cyan')

I'm suggesting to file a bug against the color library not supporting worker threads. in the interim, if you absolutely need colors, you can use the useInProcess flag, which unfortunately comes with a bunch of disadvantages compared to worker threads.