英文:
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('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()');
答案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 > 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
英文:
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("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);
// If we don't call _log.apply() below then we don't get console.log()
// Why do we get the commas ?
_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()');
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论