Created
December 7, 2016 19:32
-
-
Save hax/1ee3daf3c18bb436ad7ec53ea06e2aca to your computer and use it in GitHub Desktop.
log with calling location info in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {main} = require('./main') | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {log} = require('./log') | |
exports.main = function test() { | |
log('Hello world!') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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