wush978
wush978

Reputation: 3174

How to correctly combine gzip and filestream object?

I am using winston to log some messages. Because the message is big, I want to write in gzipped format.

The constructor of logger object has an argument of write stream. It works if I pass a file stream to it:

var winston = require('winston');
var out = require('fs').createWriteStream("test.log");
var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.File)({
      stream: out,
      json: false
    })
  ]
});
logger.info("test");

will produce test.log with

2013-09-12T07:53:14.795Z - info: test

I have read the document of zlib which shows that:

This provides bindings to Gzip/Gunzip, Deflate/Inflate, and DeflateRaw/InflateRaw classes. Each class takes the same options, and is a readable/writable Stream.

I tried to use pipe to combine Gzip and fs as follow:

var winston = require('winston');
var out = require('fs').createWriteStream("test.log");
var gzip = require('zlib').createGzip();
gzip.pipe(out, {end : true});
var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.File)({
      stream: gzip,
      json: false
    })
  ]
});
logger.info("test");

However, the test.log will be empty.

How to correctly combine gzip and filestream object?

Upvotes: 0

Views: 476

Answers (1)

Nitzan Shaked
Nitzan Shaked

Reputation: 13598

What you're doing is generally OK. However gzip will not flush its output buffer until you've written quite a bit of data, so the file appears to be empty if you only write one line.

You can:

1) Write more data. (You need, iirc 16KB of compressed data).

2) Invoke .flush() on the gzip stream.

Upvotes: 1

Related Questions