我是否可以配置console.log,以便将日志写入文件,而不是打印在控制台中?
当前回答
如果您正在寻找不修改任何代码的解决方案,这里有一个简单的解决方案。
它需要pm2,只需将它添加到你的节点模块,并启动你的应用程序
pm2 start server.js
你完成了,控制台。日志现在自动注册在home/.pm2/ Logs /server-out.log下。
其他回答
您现在可以使用Caterpillar,这是一个基于流的日志记录系统,允许您登录它,然后将输出输出到不同的转换和位置。
输出到一个文件很简单:
var logger = new (require('./').Logger)();
logger.pipe(require('fs').createWriteStream('./debug.log'));
logger.log('your log message');
卡特彼勒网站上的完整示例
对于未来的用户。@keshavDulal答案不适用于最新版本。而且我无法找到针对最新版本3.3.3中报告的问题的适当修复。
不管怎样,在研究了一下之后,我终于把它修好了。下面是温斯顿3.3.3版本的解决方案
安装winston和winston-daily-rotate-file
npm install winston
npm install winston-daily-rotate-file
创建一个新文件utils/logger.js
const winston = require('winston');
const winstonRotator = require('winston-daily-rotate-file');
var logger = new winston.createLogger({
transports: [
new (winston.transports.DailyRotateFile)({
name: 'access-file',
level: 'info',
filename: './logs/access.log',
json: false,
datePattern: 'yyyy-MM-DD',
prepend: true,
maxFiles: 10
}),
new (winston.transports.DailyRotateFile)({
name: 'error-file',
level: 'error',
filename: './logs/error.log',
json: false,
datePattern: 'yyyy-MM-DD',
prepend: true,
maxFiles: 10
})
]
});
module.exports = {
logger
};
然后在任何你想要使用日志导入模块的文件
const logger = require('./utils/logger').logger;
像下面这样使用记录器:
logger.info('Info service started');
logger.error('Service crashed');
const fs = require("fs");
const {keys} = Object;
const {Console} = console;
/**
* Redirect console to a file. Call without path or with false-y
* value to restore original behavior.
* @param {string} [path]
*/
function file(path) {
const con = path ? new Console(fs.createWriteStream(path)) : null;
keys(Console.prototype).forEach(key => {
if (path) {
this[key] = (...args) => con[key](...args);
} else {
delete this[key];
}
});
};
// patch global console object and export
module.exports = console.file = file;
要使用它,可以这样做:
require("./console-file");
console.file("/path/to.log");
console.log("write to file!");
console.error("also write to file!");
console.file(); // go back to writing to stdout
大多数记录器是过量的,不支持console.log中的正确构建。因此我创建了console-log-to-file:
import { consoleLogToFile } from "console-log-to-file";
// or `const { consoleLogToFile } = require("console-log-to-file/dist/index.cjs.js")`
consoleLogToFile({
logFilePath: "/log/default.log",
});
// all of your console.log/warn/error/info will work as it does and save to file now.
直接来自控制台的nodejs API文档
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// custom simple logger
const logger = new Console(output, errorOutput);
// use it like console
const count = 5;
logger.log('count: %d', count);
// in stdout.log: count 5
推荐文章
- CALL_AND_RETRY_LAST分配失败-进程内存不足
- 在Ubuntu上安装Node.js
- 使用express.js代理
- Python日志记录不输出任何东西
- Node -使用NODE_MODULE_VERSION 51根据不同的Node.js版本编译
- RabbitMQ / AMQP:单队列,同一消息的多个消费者?
- IE9是否支持console.log,它是一个真实的功能吗?
- Node.js同步执行系统命令
- 禁用包的postinstall脚本
- Node.js上的html解析器
- 错误:无法找到模块“webpack”
- 有nginx access_log和error_log日志的STDOUT和STDERR的主进程
- 在node.js中使用async / await文件系统
- NodeJS -用NPM安装错误
- 如何为本地安装npm包设置自定义位置?