Skip to content

Instantly share code, notes, and snippets.

@hax hax/index.js
Created Dec 7, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.