JavaScript的console.log()可以重定向到文件吗?

huangapple go评论101阅读模式
英文:

Can Javascript's console.log() be redirected to a file?

问题

Javascript的console.log()能重定向到文件吗?
我目前正在开发一个非常简单的Node.js应用,托管在Namecheap上。
我无法看到console.log()的输出,但即使能够将它重定向到一个文本文件,我可以稍后通过FTP来查看,这会很有帮助。

我尝试使用我的Web浏览器开发工具,但无法看到运行在Namecheap共享托管上的Node.js应用的console.log()的输出。但在本地开发机上执行时,我是可以看到的。

const http = require('http');
console.log('消息 A: 在 createServer() 之前');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/html'});
    response.end("<h1>Hello from Namecheap Node.js App</h1>");
    console.log('消息 B: 从浏览器请求页面。');
}).listen(5000);

console.log('消息 C: 在 listen() 之后');

不包含代码部分的翻译已完成。

英文:

Can Javascript's console.log() be redirected to a file ?
I am currently working on very simple node.js app that is hosted on Namecheap .
I can not see output from console.log(), but even being able to redirect it to a text file that I can examine by FTPing into my account later would be a help.

I tried using my web browser developer tools, but I can not see output of console.log() from the node.js app running on shared hosting by Namecheap. I can see it when doing so on own local developer machine.

const http = require(&#39;http&#39;);
console.log(&#39;Msg A: before createServer()&#39;);

http.createServer(
   function(request, response) 
   {
       response.writeHead(200, {&#39;Content-Type&#39;: &#39;text/html&#39;});
       response.end(&quot;&lt;h1&gt;Hello from Namecheap Node.js App&lt;/h1&gt;&quot;);
       console.log(&#39;Msg B: requesting page from browser.&#39;);
   }
).listen(5000);

console.log(&#39;Msg C: after listen()&#39;);

答案1

得分: 1

你可以这样调用你的应用程序,既然你在Node上:

node foo.js > file.log

而不是:

node foo.js
英文:

Given that you're on Node, you can just call your application this way:

node foo.js &gt; file.log

Instead of:

node foo.js

答案2

得分: 0

@Carcigenicate的评论让我找到了这个GitHub上的解决方案: https://gist.github.com/mksoni88/03f69c9c40bb65aa8bbb

这让我能够修改以下代码,从而在短期内得到我需要的内容:

var fs = require("fs");

_log = console.log;
global.console.log = function() 
{
    var traceobj = new Error("deen").stack.split("\n")[2].split(":");
    //var file = traceobj[0].split(process.env.PWD + '/')[1];
    var line = traceobj[1];
    var new_args = [":" + line + " >> "];
    new_args.push.apply(new_args, arguments);

    // 如果我们不在下面调用_log.apply(),则我们不会得到console.log()
    // 为什么会出现逗号?
    _log.apply(new_args); 

    fs.appendFile("myconsole.log", new_args + "\n", 
    function (err) 
    {
        if (err) { return console.error(err); } }
    );

};

const http = require('http');
console.log('Msg A: before createServer()');

http.createServer(
  function(request, response) 
  {
    response.writeHead(200, {'Content-Type': 'text/html'});
    response.end("<h1>Hello from Namecheap Node.js App</h1>");
    console.log('Msg B: requesting page from browser.');
  }
).listen(5000);

console.log('Msg C: after listen()');

此外,我还能够SSH到托管此代码的Namecheap站点,并使用tail -f命令交互地观看覆盖的console.log函数的输出:

tail -f myconsole.log

JavaScript的console.log()可以重定向到文件吗?

英文:

The comment by @Carcigenicate put me on the road to find this solution on gitHub : https://gist.github.com/mksoni88/03f69c9c40bb65aa8bbb

which led me to hack the following code, thus giving me exactly what I need in the short term:

var fs = require(&quot;fs&quot;);
  
_log = console.log;
global.console.log = function() 
{
	var traceobj = new Error(&quot;deen&quot;).stack.split(&quot;\n&quot;)[2].split(&quot;:&quot;);
	//var file = traceobj[0].split(process.env.PWD + &#39;/&#39;)[1];
	var line = traceobj[1];
	var new_args = [&quot;:&quot; + line + &quot; &gt;&gt; &quot;];
	new_args.push.apply(new_args, arguments);
	
	// If we don&#39;t call _log.apply() below then we don&#39;t get console.log()
	// Why do we get the commas ?
	_log.apply( new_args); 
	
	fs.appendFile(&quot;myconsole.log&quot;, new_args + &quot;\n&quot;, 
	function (err) 
	{
		if (err) { return console.error(err); } }
	);
	
};

const http = require(&#39;http&#39;);
console.log(&#39;Msg A: before createServer()&#39;);

http.createServer(
  function(request, response) 
  {
    response.writeHead(200, {&#39;Content-Type&#39;: &#39;text/html&#39;});
    response.end(&quot;&lt;h1&gt;Hello from Namecheap Node.js App&lt;/h1&gt;&quot;);
    console.log(&#39;Msg B: requesting page from browser.&#39;);
  }
).listen(5000);

console.log(&#39;Msg C: after listen()&#39;);

Also, I was able to SSH into the Namecheap site where this code lives, and use the tail -f command to interactively watch the output from the overridden console.log function:
<code><pre>tail -f myconsole.log </code></pre>

JavaScript的console.log()可以重定向到文件吗?

huangapple
  • 本文由 发表于 2023年2月19日 09:04:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定