How to log all console.log output to a file?

8,021 views
Skip to first unread message

csplrj

unread,
Apr 22, 2011, 11:14:32 AM4/22/11
to phantomjs
How to log all console.log output to a file?

I want that console.log output to be logged to a file.
Other output like error to be logged in a seperate file

Thanks in advance

CSJakharia

James Roe

unread,
Apr 22, 2011, 11:24:25 AM4/22/11
to phantomjs
Hi, have you tried redirecting stdout/stderr?

I believe you could use a command like so. It worked with me on Linux
as well (then again, Linux != Windows either)
phantomjs.exe script.js > stdout.txt 2> stderr.txt

If you don't see anything in stderr.txt, it could be 1 of 3 reasons.
1) All the output is going to stdout.
2) The program put the error on stdout instead of stderr. (Related to
1) )
3) The command just doesn't work.

You can see a guide here (useful, but not a ton since the examples
aren't that great)
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx

csplrj

unread,
Apr 22, 2011, 12:35:24 PM4/22/11
to phantomjs
Thanks for this. But it does not helps as errors come in the messages
text file
> aren't that great)http://www.microsoft.com/resources/documentation/windows/xp/all/prodd...

James Roe

unread,
Apr 22, 2011, 12:44:41 PM4/22/11
to phantomjs
That would mean that the errors are being printed on stdout instead of
stderr.

That is the case with my PyPhantomJS port, but is easily fixable. The
same is fixable in the PhantomJS C++ version as well.

You see on line 70, it's directing output to cout. All that's needed
is to redirect output to cerr instead.
https://github.com/ariya/phantomjs/blob/master/src/webpage.cpp#L70

Please file a bug for this so that we can fix it later. Thanks!

Ariya Hidayat

unread,
Apr 22, 2011, 2:30:18 PM4/22/11
to phan...@googlegroups.com

Why is this a bug? I believe *all* console.log still needs to go to
stdout instead of stderr.

I believe the issue is not being able to distinguish between your own
console.log and the web page's one. Qt redirects everything to
javaScriptConsoleMessage. This is already filed as
http://code.google.com/p/phantomjs/issues/detail?id=42.

--
Ariya Hidayat
http://www.linkedin.com/in/ariyahidayat

James Roe

unread,
Apr 22, 2011, 11:43:08 PM4/22/11
to phantomjs
I was incorrect. I assumed he was talking about the actual JavaScript
page errors, not errors in his script output using console.log, which,
in such a case, should *all* go to stdout.

So, if that was the case, I take back what I said about filing the bug
report. Although, the redirection trick still works. ;)

James Roe

unread,
Apr 23, 2011, 12:09:04 AM4/23/11
to phantomjs
Oops, failed to correctly read your last message. Ignore the email I
had just sent.

Anyways, you're right. All the console.log output should be to stdout
not stderr.

I had thought he was talking about webpage JavaScript evaluation
errors, and my line of thinking here was to redirect all evaluation
errors into stderr (since they currently go to stdout. And while we're
at it we could probably use qWarning/qCritical or something). The
previous assumption I was operating on was that if sourceID is not
empty, then print it to stderr instead. This quote from the
documentation supports my view.. (unless I misinterpreted what it
says, or have a simplistic view of how it really works).

"For example in case of evaluation errors the source URL may be
provided in sourceID as well as the lineNumber."

Ivan De Marino

unread,
Apr 27, 2011, 5:51:07 AM4/27/11
to phan...@googlegroups.com
Just read the documentation of QWebPage at http://doc.qt.nokia.com/4.7/qwebpage.html.

The "::javascriptControlMessage" protected method retuns a "sourceID" and a "lineNumber" when it's a coding error and NOT a "console.log" call.

The problem is: there is also "console.error". We could filter out (or divert) the Javascript Coding Errors, but to handle "console.error" we need QtWebKit to fix this.
Reply all
Reply to author
Forward
0 new messages