0

I'm not sure how I'm supposed to use this package. I've followed the example code from the docs:

var fs = require('fs')
  , Log = require('log')
  , log = new Log('debug', fs.createWriteStream('my.log'));

But then what? How do I send actual log info to the file? I want what normally gets logged with console.log() to go to the file.

edit: here's the context that I am using it in, as a minimal example. log.info() works fine outside of the while. As it is below, the file is created but has nothing in it.

var fs = require('fs')
var Log = require('log')
var log = new Log('info', fs.createWriteStream('my.log'));

while(true) {
    log.info("testing");
}
4
  • Don't use while (true) like that in Node.js, it blocks the event loop preventing any I/O (like writing to a file) from happening. Commented Oct 17, 2017 at 6:55
  • I need the script to run repeatedly and continuously Commented Oct 17, 2017 at 7:11
  • surely there's some code that you want to run in between the calls to the logger? Commented Oct 17, 2017 at 7:12
  • Yeah, there is. I just tried the above in my actual code and it does work fine. Thanks for your help! Commented Oct 17, 2017 at 7:25

3 Answers 3

1
// (taken from readme of package)
log.debug('preparing email');
log.info('sending email');
log.error('failed to send email');

These will each log to the file you specified. The function denotes the log level, and is prepended to the data you provide.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried log.info("...") and nothing was written. The file is created but nothing written to it.
@erv please add a minimal, complete and verifiable example to your question. It works for me.
Ok, give me a little bit of time. My head is immersed in another problem just at the moment.
0

You need to use fs.appendFileSync() method in order to add content synchronously to the log you are creating.

var fs require('fs')
,Log = require('log')
,log = new Log('debug', fs.createWriteStream('test.txt'));

log.debug('test test');
log.debug('test sadasd');
log.debug('test xcvxcv');
log.debug('test ewrewr');
log.debug('test hjgj');
log.debug('test fghfh');
log.debug('test yuiyui');

4 Comments

That's not using the log package, though, is it?
If you need it to sync with your log commands then wrap the fs.appendFileSync in your logging as follows. log = new Log('debug', fs.appendFileSync('log.txt'));
@FernandoGarcia the Log constructor requires a stream, and fs.appendFileSync doesn't return one.
@robertklep correct use createWriteStream method instead. Just call the log.debug method.
0

This package is not designed to log Console.log() statement. As per my knowledge every log file have date/time/(log type) and information related to it. So normal console.log() also have same info stored into it.
Log package which you mentioned is used to create user defined logs of any type (info,debug,warning etc etc.).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.