Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

javascript - Node.js console.log() in txt file

I have another question (last question). At the moment i am working on a Node.js project and in this I have many console.log() functions. This has worked okay so far but I also want everything that's written to the console to also be written in a log-file. Can someone please help me?

For example:

Console.log('The value of array position [5] is '+ array[5]);

In my real code its a bit more but this should give you an idea.

Thank you hopefully.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just run the script in your terminal like this...

node script-file.js > log-file.txt

This tells the shell to write the standard output of the command node script-file.js to your log file instead of the default, which is printing it to the console.

This is called redirection and its very powerful. Say you wanted to write all errors to a separate file...

node script-file.js >log-file.txt 2>error-file.txt

Now all console.log are written to log-file.txt and all console.error are written to error-file.txt


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...