0

What's the best way to log a dynamic variable?

Example: while running the app, my log changes size and data. I want the log to not change, and simply save data.

This is my code for creating the log file:

const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
const logger = new Console(output, errorOutput);
logger.log(res);

The var res changes during the execution of the app.

qxz
  • 3,697
  • 1
  • 11
  • 29
Santiago D'Antuoni
  • 164
  • 1
  • 1
  • 12
  • 1
    Your logging logs the variable at the moment you call `logger.log()`. There is no automatic way to log every time the variable changes. – jfriend00 Dec 08 '16 at 17:54
  • What's the purpose of the rest of your program? Perhaps we can help with your overall goal. – qxz Dec 08 '16 at 17:59
  • Related: http://stackoverflow.com/questions/1759987/listening-for-variable-changes-in-javascript-or-jquery for reference, I would suggest creating a function instead of the solution in the link though. – Caramiriel Dec 08 '16 at 18:01
  • @jfriend00 my purpose is not log all variable changues in stdout.log and this file not changue never. This file should save all data of this variable. – Santiago D'Antuoni Dec 08 '16 at 18:30
  • Well, Javascript doesn't have that feature. You need to log the value yourself after it changes. Javascript doesn't have a feature to log it automatically after it changes. Just isn't a feature of the language. You could probably try to build one by proxying the object and overriding all properties with getters/setters or using some new ES6 features, but you'd have to do a bunch of coding yourself to capture all the changes to the object and to log those changes yourself. – jfriend00 Dec 08 '16 at 18:32
  • I not explain correctly, what i need is log file not erase for my script. Only log data, but not delete info logged. – Santiago D'Antuoni Dec 08 '16 at 18:37
  • It is not clear at all what you are asking. I'm moving on to other questions where I can tell what the question means. – jfriend00 Dec 08 '16 at 19:05

1 Answers1

0

Looks like there are a couple ways of watching for variables changes: Listening for variable changes in JavaScript or jQuery

Doesn't look like you have shown all your code, since as far as I know there is no 'Console' constructor. There is a 'console' object rather (i.e. console.log()).

I have previously created a 'logger' object in my code so that I could change how I logged throughout my code base.

i.e.

var logger = {
  log: function(msg) {console.log(msg)}
}

(But note that you lose some functionality in this specific example)

Community
  • 1
  • 1
Zach Smith
  • 6,866
  • 8
  • 42
  • 92