user8952056
user8952056

Reputation:

Winston log format

i am using Winston ^3.0.0-rc6 as below :

var options = {
    file: {
        level: 'info',
        filename: `${appRoot}/logs/app.log`,
        handleExceptions: true,
        json: true,
        prettyPrint: true,
        maxsize: 5242880, // 5MB
        maxFiles: 5,
        colorize: true,

    }
};

const jsonFormatter = (logEntry) => {
    if (logEntry.type) {
        const base = {
            timestamp: new Date()
        };
        const json = Object.assign(base, logEntry);
        logEntry[MESSAGE] = JSON.stringify(json);
    } else {
        logEntry = "";
    }

    return logEntry;
}

const logger = winston.createLogger({
    format: winston.format(jsonFormatter)(),
    transports: [
        new winston.transports.File(options.file)
    ],
    exceptionHandlers: [
        new winston.transports.File(options.uncaughtExceptions)
    ]
});

my log output :

{"timestamp":"2018-06-10T07:41:03.387Z","type":"Authentication","status":"failed","level":"error","message":"Incorrect password"}

but i want them to be like :

{
    "timestamp": "2018-06-10T07:41:03.387Z",
    "type": "Authentication",
    "status": "failed",
    "level": "error",
    "message": "Incorrect password"
}

i tried to play around with json : true , and prettyPrint but it did not do the trick .

Can any one help please

Thanks.

Upvotes: 3

Views: 16633

Answers (2)

marcusstenbeck
marcusstenbeck

Reputation: 825

I noticed in your code that on the line

logEntry[MESSAGE] = JSON.stringify(json);

you're using JSON.stringify() which takes two more optional arguments

JSON.stringify(value[, replacer[, space]])

If you set space to the amount of spaces you'd like you'll get the output you're looking for. So change the initial line to be:

logEntry[MESSAGE] = JSON.stringify(json, null, 2);  // or 4 ;)

(The replacer argument is null because we don't want to change the default behavior.)

Upvotes: 3

Harshal Yeole
Harshal Yeole

Reputation: 5003

This is deprecated: You can check the link here.

I tried to play around with json: true, and prettyPrint but it did not do the trick.

Simple code like this work for you:

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    //
    // - Write to all logs with level `info` and below to `combined.log` 
    // - Write all logs error (and below) to `error.log`.
    //
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    new winston.transports.File({ filename: 'combined.log' })
  ]
});

If this does not work, let me know so that I can improvise.

Upvotes: 0

Related Questions