简体   繁体   中英

How to remove unwanted character from log files created by Winston

I am using Winston logger inside NestJS application but when logs are writing in file then some unwanted characters are also get appended to my logs.

Below are the logs from log file:

[92m[MyApp][39m [33mInfo[39m    16/3/2022, 7:03:52 pm [92mHello[39m - {}

Below is my Winston configuration:

import { Module } from '@nestjs/common';
import { WinstonModule,utilities as nestWinstonModuleUtilities } from 'nest-winston';
import * as winston from 'winston';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import * as path from 'path';

@Module({
imports: [ WinstonModule.forRoot({
          format: winston.format.combine(
                   winston.format.timestamp(),
                   winston.format.json(),
                   winston.format.simple(),
                   nestWinstonModuleUtilities.format.nestLike('MyApp', { prettyPrint: true }),
                  ),
          transports: [
                      new winston.transports.File({
                      dirname: path.join(__dirname, '../log/info/'), //path to where save logging result 
                      filename: 'info.txt', //name of file where will be saved logging result
                      level: 'info',
                    }),
                  ],        
       }),],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

How can I remove these characters from my logs?

Add format like this. You can change replace character whatever you want. This code is for example.

new winston.transports.File({
                      dirname: path.join(__dirname, '../log/info/'), //path to where save logging result 
                      filename: 'info.txt', //name of file where will be saved logging result
                      level: 'info',
                      format: format.combine(
                             format.printf((i) => i.message.replace('e', '')),
                        ),
                    }),
             

Or if you want to delete all special character use this code

new winston.transports.File({
                      dirname: path.join(__dirname, '../log/info/'), //path to where save logging result 
                      filename: 'info.txt', //name of file where will be saved logging result
                      level: 'info',
                      format: format.combine(
                             format.printf((i) => i.message.replace(/[e]/g, '')),
                        ),
                    }),
             

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM