简体   繁体   中英

how to print file name in log in node js

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?

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM