admin管理员组文章数量:1208155
I am working on a game using NodeJs, and realized it would be immensly helpful to have access to old console.log
output. The mechanics and calculations get rather complicated, and it would be nice to have access to them later to search through and do some data analysis on them to find the correct values for my modifiers (battle mechanics). At the same time, I want to see console.log
in the console as well.
I know this isn't possible with regular javascript, (see This), but I was hoping npm had a package of some way to intercept logs and log them to a file on the server. Any thoughts?
I am working on a game using NodeJs, and realized it would be immensly helpful to have access to old console.log
output. The mechanics and calculations get rather complicated, and it would be nice to have access to them later to search through and do some data analysis on them to find the correct values for my modifiers (battle mechanics). At the same time, I want to see console.log
in the console as well.
I know this isn't possible with regular javascript, (see This), but I was hoping npm had a package of some way to intercept logs and log them to a file on the server. Any thoughts?
Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Aug 19, 2016 at 23:27 perennial_perennial_ 1,8462 gold badges28 silver badges41 bronze badges 2 |3 Answers
Reset to default 14You can intercept console.log like so
var cl = console.log
console.log = function(...args){
// your custom logging logic here
cl.apply(console, args)
}
An NPM option to log to stdout and to a file at the same time could be Winston. Winston is a logging library that allows you to define your own loggers, specifying their transports (a transport being what do you do with these log lines). In your case, you could define a logger with two transports, stdout and a log file. Example (inspired by the Winston documentation):
const winston = require('winston');
let logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'somefile.log' })
]
});
logger.info('hello, world!');
If you run this code above, you'll see the logs in the console and in somefile.log
.
The purest form would be by overriding stdout
and stderr
methods, e.g.
process.stdout.write = (data) => {
w(data);
};
process.stderr.write = (data) => {
w(data);
};
Here w
is whatever mechanism that you would like to use to record this data, e.g.
const w = (...args) => {
appendFileSync(
path.resolve(__dirname, "test.json"),
JSON.stringify(args, null, " ")
);
};
Beware that you cannot output anything from this function as that would cause an infinite loop.
The above behaviour would discard any output though. If you want the program to continue producing output, then you need to call the original write
methods, e.g.
const stdoutWrite = process.stdout.write.bind(process.stdout);
const stderrWrite = process.stderr.write.bind(process.stderr);
process.stdout.write = (data, callback) => {
w(data);
return stdoutWrite(data, callback);
};
process.stderr.write = (data, callback) => {
w(data);
return stderrWrite(data, callback);
};
本文标签: javascriptIntercept console logs to file NodejsStack Overflow
版权声明:本文标题:javascript - Intercept console logs to file Nodejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738746161a2110128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
console.log
just make your own log method that writes to a file and stdout. Or, just usetee
when you launch your application if you're in a *nix environment. – dvlsg Commented Aug 19, 2016 at 23:46