Node, redirect console to a file - node.js

How can I redirect everything what is displayed in console to a file?
I mean for example, i call some function, this function display something on console (no metter if it is console.log or process.stdout.write)?
Thanks for help!

While not strictly a Node.js answer, you could achieve this at the shell level. For example if you are using bash, you could redirect both standard output and error stream to a file using the following
#!/bin/bash
node app.js &> output.log
Check out also tee command for simultaneous output to both file and screen.

Related

Program to capture other's program stderr and stdout

I'd like to run a program as root that can intercept other's program stderr and stdout.
For example, say I start a nodejs server and somehow there's an error (with logs printed to stderr), if my program is running, I would like it to intercept this error.
Is that possible ? How should I do ?
Also, an idea that came to my mind was to replace nodejs binary by another one that starts nodejs and redirect stderr to a custom file. but I think it's too messy and I hope there's better ways to do that.
If you can control how nodejs is called you can redirect stderr to a named pipe and then read the named pipe from another command like this:
mkfifo /tmp/nodejs.stderr
nodejs 2>/tmp/nodejs.stderr
Then in some other shell type:
grep "Error Pattern" </tmp/nodejs.stderr
If you can't control how nodejs is called, then you can create a shell script to wrap those commands and call the shell script wherever nodejs is called.

How to capture the stdout stream of an electron application in windows?

I've got a standard electron app (based off of electron-prebuilt) that ought to be writing to std out with the following command: (in my project's main.js)
process.stdout.write('stdout write test')
when I launch the app from powershell and redirect the output to a txt file, I see the string displayed in the terminal window as expected:
> .\App.exe > log.txt
stdout write test
but when I open log.txt it's a blank file. what am I missing?
edit: to clarify Ansgar Wiechers' correct answer: in the windows command prompt, I needed to run the following command:
set ELECTRON_NO_ATTACH_CONSOLE=true
This results in console.log and process.stdout.write being directed to windows' stdout stream.
If you run .\App.exe > log.txt and you can see the output string in the console it means that the string is not being written to STDOUT (the Success output stream in PowerShell terms) in the first place.
Apparently the Electron developers decided to attach stdout directly to the console instead of actual STDOUT (see issue #4552). If I understand the discussion there correctly you can set the environment variable ELECTRON_NO_ATTACH_CONSOLE to avoid this behavior.

Linux Command redirecting output to file but file is not getting populated

I am issuing a heavy command from bash shell and I have redirected my output to a file as follows
<command> > output.txt
But the file does not show any output even though command is running perfectly and I can see the progress through my other tool.
It is possible that your command isn't writing to STDOUT.
You can use &> to redirect both STDERR and STDOUT to a file.
Also see Advanced Bash-Scripting Guide's IO redirection page.
Try this,
<command> > output.txt 2>&1
It seems like your command fail to redirect the output to STDOUT, there may be a chance of your output went into STDERR. So try to redirect both stdout and stderr to the output file.

Getting STDerr and STDout into a log file inside of a bash script

I was wondering if it is possible to get all of what is outputted from a script I have made to go to a log file if they change one of the variables in the script. Example, in the script a variable createLog=true could be set to enable logging.
I know I can do ./myscript.sh 2>&1 | tee sabs.log
But I would like to be able to simply run ./myscript.sh
and have the whole script logged in a file, as well as output to the console if the var is set to true.
Would I have to change every command in the script to accomplish this or is there a command I can execute at the beginning of the script that will output to both.
If you need more details please let me know.
Thanks!
exec without an argument lets you redirect for the remainder of the current script.
exec >log 2>&1
You can't tee within the redirect but you can display the file with a background job.
tail -f log &

Cannot redirect command output to a file (for postfix status)

I am trying to run the following command:
postfix status > tmp
however the resulting file never has any content written, and instead the output is still sent to the terminal.
I have tried adding the following into the mix, and even piping to echo before redirecting the output, but nothing seems ot have any effect
postfix status 2>&1 > tmp
Other commands work no problem.
script -c 'postfix status' -q tmp
It looks like it writes to the terminal instead to stdout. I don't understand piping to 'echo', did you mean piping to 'cat'?
I think you can always use the 'script' command, that logs everything that you see on the terminal. You would run 'script', then your command, then exit.
Thanks to another SO user, who deleted their answer, so now I can't thank, I was put on the right track. I found the answer here:
http://irbs.net/internet/postfix/0211/2756.html
So for those who want to be able to catch the response of the posfix, I used the following method.
Create a script which causes the output to go to where you wish. I did that like this:
#!/bin/sh
cat <<EOF | expect 2>&1
set timeout -1
spawn postfix status
expect eof
EOF
Then i ran the script (say script.sh) and could pipe/redirect from there. i.e. script.sh > file.txt
I needed this for PHP so I could use exec and actually get a response.

Resources