简体   繁体   English

使用express.logger但写压缩后的日志

[英]Using express.logger but write gzipped logs

I'm trying to figure out how to make the express.logger function write its logs to a gzipped file, instead of uncompressed text. 我试图弄清楚如何使express.logger函数将其日志写到压缩文件中,而不是未压缩的文本中。 I tried the following code: 我尝试了以下代码:

var logarchive = zlib.createGzip();
var logfile = fs.createWriteStream('./logs/access.log.gz');
logarchive.pipe(logfile);

app.use(express.logger({
  format: env.get("logformat", "short"),
  stream: logarchive
}));

but that seems to just set up an illegal gzip archive reporting 184Mb of data in it, despite it only getting a few bytes sent during a logging operation. 但这似乎只是建立了一个非法的gzip归档文件,其中报告了184Mb的数据,尽管在记录操作期间它仅发送了几个字节。

What's the correct way to hook up these things? 连接这些东西的正确方法是什么? The Nodejs.org api documentation on zlib and streams doesn't give any concrete examples on how to achieve getting data sent through zlib to another write stream, and the logger only takes streams as targets. 关于zlib和stream的Nodejs.org api文档未提供有关如何实现通过zlib将数据发送到另一个写入流的具体示例,并且记录器仅将流作为目标。

I think your code should work, but zlib is quite good at buffering data so it might take some time before the logs are flushed to file. 我认为您的代码应该可以工作,但是zlib非常擅长缓冲数据,因此将日志刷新到文件可能需要一些时间。

You can periodically flush manually: 您可以定期手动刷新:

setInterval(function() {
  logarchive.flush();
}, 1000); // flush once per second

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM