how to print file name in log in node js

533 Views Asked by At

could you please tell me how to print file name in log in node js.it always print index.js why ?

here is my code https://codesandbox.io/s/elegant-bassi-ij2e9

[![enter image description here][1]][1]

format: format.combine(
    format.label({ label: path.basename(process.mainModule.filename) }),
    format.timestamp({
      format: "DD-MM-YYYY HH:mm:ss"
    }),

  [1]: https://i.stack.imgur.com/SVmNe.png

expected

08-01-2020 03:29:33 info [test.js]: --dddabcbbc-

why test.js is not printed as I mention log in test.js

any update?

1

There are 1 best solutions below

0
On

You do it like this. The path module has a function basename and you just give it __filename global object that is available in every file as an argument.

const path = require('path')
console.log(path.basename(__filename))