Nodejs - print function name, line number and file name

Viewed 1267

I'm researching various packages in Nodejs and cannot find that would suit my needs. I would like to output the current line number, function name and src file to log, similar to how Bunyan logger does.

I looked into packages such as 'caller-id' but I cannot use it as it does not support 'strict' mode.

Other packages, such as 'capture-stack-trace' only return the file name without the line number & function name.

Is there a package that I'm missing and can suit my need? Or is there an elegant way to generate this info in code?

Thanks in advance.

1 Answers

Determining the call source info is always turned off by default in every nodes logger. It is slow. Never use this option in production.

We use bunyan for our micro services. This is copypaste from documentation:

The source file, line and function of the log call site can be added to log records by using the src: true config option:

var log = bunyan.createLogger({src: true, ...});

This adds the call source info with the 'src' field, like this:

{
  "name": "src-example",
  "hostname": "banana.local",
  "pid": 123,
  "component": "wuzzle",
  "level": 4,
  "msg": "This wuzzle is woosey.",
  "time": "2012-02-06T04:19:35.605Z",
  "src": {
    "file": "/Users/trentm/tm/node-bunyan/examples/src.js",
    "line": 20,
    "func": "Wuzzle.woos"
  },
  "v": 0
}
Related