Home > OS >  Logging everything - Javascript
Logging everything - Javascript

Time:03-27

I have lots of microservices which I want to log. My goal is creating a npm package that will be installed in each of the services. What I aim for is easy integration for example:

const myLogger = require('mylogger')

//.... Express stuff
myLogger(app) 

What I would like to happen in the npm module is that it will log everything from functions executing to variable assignments. Is there a tool to do it with just wrapping the app? Or do I have to log everything explicitly by writing log.info.. log.warn.. Etc

CodePudding user response:

There is no such capability in Javascript that will automatically log every function being called or every variable being assigned in a module. Only the interpreter itself would know how to do that and it doesn't offer a feature for that.

It would be possible to wrap the Express app and then write specific code in the wrapper to log when specific methods on the Express app were called. You could do that by replacing the Express methods with some sort of proxy stub that logs and then calls the original. But, even then, you'd have to decide which methods and which arguments to those methods you want to log and write code in the wrapper to achieve that.

  • Related