0

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?

user944513
  • 12,247
  • 49
  • 168
  • 318
  • Process.mainModule is not the current module, it’s the module that was launched. – Joe Jan 08 '20 at 04:19
  • Does this answer your question? [Node.js - getting current filename](https://stackoverflow.com/questions/14201475/node-js-getting-current-filename) – Joe Jan 08 '20 at 04:20

1 Answers1

0

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))
C.Gochev
  • 1,837
  • 11
  • 21