Save console.log files to a txt

So i’m working on a thing that will ask for your name, style and life (just get hang of how to do stuff) and i want to save that data to a txt file. I have so it prints what the user has entered in to the prompt in the console log. now I want to save that data to a txt file. is there anyway i can do that?

Welcome @pajsen

Maybe you can try the library log4js

You can check some examples there

Simply redirect the output to a file. You don’t need a library for it either.

$ node server.js > 2020-03-27.txt
1 Like

Nice I didn’t know that

Can you explain more how this work? I only can see the first console.log not the next ones

Each time the server is run, it overwrites the same file. Better to append …

$ node server.js >> 2020-03-27.txt

But its better to keep the user data separate from console logs. To write to a file use file system methods

But you’ll find that for serving multiple users at the same time, writing to a database is better again. There’s heaps of database examples on glitch, here is one using ne-db https://glitch.com/~ne-db

Hey @pajsen,

You can use a custom logging package like Winston, which allows you to save logs to a file. See this starter project for Winston by @jenn: https://glitch.com/~starter-winston

Hi,

If you just want to write some data to a txt file, you can do it with the fs nodejs package:

var fs = require('fs');

let name = 'TestName';
let otherData = 'TestOtherData';

fs.writeFile('./data.txt',
   `Name: ${name}\nOtherData: ${otherData}`
,()=>{
  console.log('Successfully saved');
})

This will create a file like this:

Name: TestName
OtherData: TestOtherData

Hi am using this
node server.js >> 2020-03-27.txt

But i want to log console.log to txt file and also to Logs (Tools - Logs)
Is this possible?

Thanks fo reply

I tryid
node server.js >> console.txt & node server.js
But my bot is duplicating messages :confused:

That just runs the server.js file twice

Somebody asked a similar question here: How to log console to files & console with timestamps?

This is a package I wrote a while ago, it actually uses Console class.

Here is an example on how to use it:

const {
    Logger,
    LogConsoleTransport,
    LogFileTransport
} = require("ihacks-log");

const logger = new Logger();

logger.addTransport(
    new LogConsoleTransport(data => Logger.getLevelName(data.level) + " " + data.message),
    new LogFileTransport("test.log", data => Logger.getLevelName(data.level) + " " + data.message)
);

logger.log("Hello World", logger);

You’ll have all the features console.log has:

  • logger.log("Hello %s", "John")
  • logger.log("Hello", "John")

It is a really advanced logger, but it works very simply as well. Here is all the available methods:

  • <Logger>.debug
  • <Logger>.log
  • <Logger>.info
  • <Logger>.alert
  • <Logger>.warn
  • <Logger>.error
  • <Logger>.success
4 Likes

@ihack2712, woah, nice package!

https://www.npmjs.com/package/js-logger ?

There’s a package called Winston.

Is it possible to log console.log to console + file without any npm package and without rewriting code?

Hi, i had the same problem and the solution is very simple, you can check the answer for more details on stackoverflow : javascript - Node.js console.log() in txt file with date and hour - Stack Overflow

you can do node script.js >> log-error.txt 2>&1 and it generates one file, where both logs and errors are transcribed into this file.

I was reading the comments where someone recommended this, why not try it out?

The repo website doesn’t work for me: ihacks.dev refused to connect.

Is it still available?

No it is not, I can recommend porting https://deno.land/x/powerlog to Node.js. I might work on an official port if wanted.