Skip to content

Instantly share code, notes, and snippets.

@hax
Created December 7, 2016 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hax/1ee3daf3c18bb436ad7ec53ea06e2aca to your computer and use it in GitHub Desktop.
Save hax/1ee3daf3c18bb436ad7ec53ea06e2aca to your computer and use it in GitHub Desktop.
log with calling location info in Node.js
const {main} = require('./main')
main()
exports.log = function log(...args) {
const err = {}
const limit = Error.stackTraceLimit
Error.stackTraceLimit = 1
Error.captureStackTrace(err, log)
Error.stackTraceLimit = limit
const l = err.stack.split(/\n/)[1]
console.log(...args, l)
}
const {log} = require('./log')
exports.main = function test() {
log('Hello world!')
}
> node .
Hello world! at test (/path/main.js:4:2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment