Monday, 18 November 2019

Advanced logging with NodeJs: TypeError: X is not iterable

I have created this funcion to log into a file with verbosity level set to info, max 5 files and 5 MB for each and a complete full log (verbosity level debug) in the terminal but the different level should use different colors.

private initialize(): void {

        var winston = require('winston');
        winston.emitErrs = true;

        var logger = new winston.Logger({
            transports: [
                new winston.transports.File({
                    level: 'info',
                    filename: './logs/all-logs.log',
                    handleExceptions: true,
                    json: true,
                    maxsize: 5242880, //5MB
                    maxFiles: 5,
                    colorize: false
                }),
                new winston.transports.Console({
                    level: 'debug',
                    handleExceptions: true,
                    json: false,
                    colorize: true
                })
            ],
            exitOnError: false
        });

        module.exports = logger;
        module.exports.stream = {
            write: function(message : any, encoding : any){
                logger.info(message);
            }
        };

    }

but I have this error when starting the app.

App encountered an unhandled rejection at
Promise: [object Promise]
Reason:
        this.enableTransports is not iterable
        TypeError: this.enableTransports is not iterable
    at LoggingService.isAtLeastOneTransportLevelActive

enableTransports is used here

private createLoggerWithTransports(customLevels: any): void {
        // Create Logger with transports
        const configuration: Configuration = this.configService.configuration;
        this.enableTransports = LoggingService.getEnabledTransports(configuration);
        const opts = {
            levels: customLevels.levels,
            level: LogLevels.TRACE.label,
            transports: LoggingService.createEnabledTransports(this.enableTransports),
            exitOnError: false
        };
        this.logger = winston.createLogger(opts);
    }

and

 private static createEnabledTransports(enabledTransports: any[]): any[] {
        const transports = enabledTransports
            .map((transportConfig: any) => LoggingService.createTransportFromConfig(transportConfig));
        return transports;
    }

and

  private static getEnabledTransports(configuration: Configuration): any[] {
        const transports = configuration.logging.transports
            .filter((c: any) => <any>c.enabled === true);
        return transports;
    }


from Advanced logging with NodeJs: TypeError: X is not iterable

No comments:

Post a Comment