libik
libik

Reputation: 23029

Winston logging - pretty JSON format in Winston 3.x

I was working with Winston few years ago. When we were developing on localhost our winston was configured to output nice formatted JSON which was easy to read.

2.x Winston

npm install [email protected]

const winston = require('winston');
const logger = new winston.Logger({
    transports: [new (winston.transports.Console)({ json: true })],
});

logger.info('please', { iam: 'good' });
try {
    throw new Error('ooh noo');
} catch (err) {
    logger.error('Not good error', err);
}

has this output

{
  "iam": "good",
  "level": "info",
  "message": "please"
}
{
  "message": "Not good error",
  "stack": "Error: ooh noo\n    at Object.<anonymous> (C:\\Users\\libor\\WebstormProjects\\untitled\\usewinston.js:30:11)\n    at Module._compile (internal/modules/cjs/loader.js:1063:30)\n    at Object.Module._extensions..js (internal/
modules/cjs/loader.js:1092:10)\n    at Module.load (internal/modules/cjs/loader.js:928:32)\n    at Function.Module._load (internal/modules/cjs/loader.js:769:14)\n    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_
main.js:72:12)\n    at internal/main/run_main_module.js:17:47",
  "level": "error"
}

3.x Winston

npm install winston (newest version or 3.3.3 when writing this article)

const winston = require('winston');

const logger = winston.createLogger({
    level: 'info',
    format: winston.format.json(),
});


logger.add(new winston.transports.Console({
    format: winston.format.json(),
}));

logger.info('please', { iam: 'good' });
try {
    throw new Error('ooh noo');
} catch (err) {
    logger.error('Not good error', err);
}

has this output

{"iam":"good","level":"info","message":"please"}
{"level":"error","message":"Not good error ooh noo","stack":"Error: ooh noo\n    at Object.<anonymous> (C:\\Users\\libor\\WebstormProjects\\untitled\\usewinston.js:21:11)\n    at Module._compile (internal/modules/cjs/loader.js:1063:30)
\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)\n    at Module.load (internal/modules/cjs/loader.js:928:32)\n    at Function.Module._load (internal/modules/cjs/loader.js:769:14)\n    at Function.executeU
serEntryPoint [as runMain] (internal/modules/run_main.js:72:12)\n    at internal/main/run_main_module.js:17:47"}

Question

Is there some native/easy way to get same output like I had in 2.x version in newest (3.3.3) version? If not, do you know the best way to achieve it?

Bonus question

The Webstorm is not able to detect (in neither of above examples) the files and lines in stacktrace, therefore its not clickable. When I just "console.error" the stack trace looks like this:

webstorm

and I can directly click the file to navigate to it in the line that caused error. Do you know how to achieve it in Winston 3.x?

Upvotes: 3

Views: 6061

Answers (1)

libik
libik

Reputation: 23029

I solved it with custom formatter as following

const winston = require('winston');
const _ = require('lodash');

const logger = winston.createLogger({
    level: 'info',
});

const logStackAndOmitIt = winston.format((info, opts) => {
    if (info.stack){
        console.error(info.stack);
        return _.omit(info, 'stack');
    }
    return info;
});

logger.add(new winston.transports.Console({
        format: winston.format.combine(
            logStackAndOmitIt(),
            winston.format.prettyPrint(),
        ),
    })
);

const arr = Array(50).fill(20)

logger.info('pleases', { iam: 'abc', arr });
try {
    throw new Error('ooh noo');
} catch (err) {
    logger.error('Not good error', err);
}

Which has this output

{
  iam: 'abc',
  arr: [
    20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
    20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
    20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
    20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
    20, 20, 20, 20, 20, 20
  ],
  level: 'info',
  message: 'pleases'
}
Error: ooh noo
    at Object.<anonymous> (C:\Users\libor\WebstormProjects\untitled\usewinston.js:71:11)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
{ level: 'error', message: 'Not good error ooh noo' }

Including clickable link in Webstorm

Upvotes: 6

Related Questions