最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Winston logging - pretty JSON format in Winston 3.x - Stack Overflow

programmeradmin4浏览0评论

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._pile (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._pile (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:

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?

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._pile (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._pile (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:

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?

Share Improve this question asked Dec 13, 2020 at 16:03 libiklibik 23k10 gold badges51 silver badges92 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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.bine(
            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._pile (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

发布评论

评论列表(0)

  1. 暂无评论