Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.


1 Answers

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.)

like image 162
marcusstenbeck Avatar answered Apr 19 '26 09:04

marcusstenbeck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!