Skip to content

Commit

Permalink
remove support for winston2 and methods supported by winston2 (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattberther authored Sep 4, 2019
1 parent 4f6cfaa commit c1d1a3a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 297 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ node_js:
- "10"

env:
- WINSTON_VER=winston@2
- WINSTON_VER=winston@3

before_install:
Expand Down
164 changes: 7 additions & 157 deletions daily-rotate-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ var fs = require('fs');
var os = require('os');
var path = require('path');
var util = require('util');
var semver = require('semver');
var zlib = require('zlib');
var hash = require('object-hash');
var winston = require('winston');
var compat = require('winston-compat');
var MESSAGE = require('triple-beam').MESSAGE;
var PassThrough = require('stream').PassThrough;
var Transport = semver.major(winston.version) === 2 ? compat.Transport : require('winston-transport');
var Transport = require('winston-transport');

var loggerDefaults = {
json: false,
Expand Down Expand Up @@ -127,29 +124,13 @@ util.inherits(DailyRotateFile, Transport);
DailyRotateFile.prototype.name = 'dailyRotateFile';

var noop = function () {};
if (semver.major(winston.version) === 2) {
DailyRotateFile.prototype.log = function (level, msg, meta, callback) {
callback = callback || noop;
var options = Object.assign({}, this.options, {
level: level,
message: msg,
meta: meta
});

var output = compat.log(options) + options.eol;
this.logStream.write(output);
callback(null, true);
};
} else {
DailyRotateFile.prototype.normalizeQuery = compat.Transport.prototype.normalizeQuery;
DailyRotateFile.prototype.log = function (info, callback) {
callback = callback || noop;
DailyRotateFile.prototype.log = function (info, callback) {
callback = callback || noop;

this.logStream.write(info[MESSAGE] + this.options.eol);
this.emit('logged', info);
callback(null, true);
};
}
this.logStream.write(info[MESSAGE] + this.options.eol);
this.emit('logged', info);
callback(null, true);
};

DailyRotateFile.prototype.close = function () {
var self = this;
Expand All @@ -159,134 +140,3 @@ DailyRotateFile.prototype.close = function () {
});
}
};

DailyRotateFile.prototype.query = function (options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}

if (!this.options.json) {
throw new Error('query() may not be used without the json option being set to true');
}

if (!this.filename) {
throw new Error('query() may not be used when initializing with a stream');
}

var self = this;
var results = [];
options = self.normalizeQuery(options);

var logFiles = (function () {
var fileRegex = new RegExp(self.filename.replace('%DATE%', '.*'), 'i');
return fs.readdirSync(self.dirname).filter(function (file) {
return path.basename(file).match(fileRegex);
});
})();

if (logFiles.length === 0 && callback) {
callback(null, results);
}

(function processLogFile(file) {
if (!file) {
return;
}

var logFile = path.join(self.dirname, file);
var buff = '';

var stream;

if (file.endsWith('.gz')) {
stream = new PassThrough();
fs.createReadStream(logFile).pipe(zlib.createGunzip()).pipe(stream);
} else {
stream = fs.createReadStream(logFile, {
encoding: 'utf8'
});
}

stream.on('error', function (err) {
if (stream.readable) {
stream.destroy();
}

if (!callback) {
return;
}

return err.code === 'ENOENT' ? callback(null, results) : callback(err);
});

stream.on('data', function (data) {
data = (buff + data).split(/\n+/);
var l = data.length - 1;

for (var i = 0; i < l; i++) {
add(data[i]);
}

buff = data[l];
});

stream.on('end', function () {
if (buff) {
add(buff, true);
}

if (logFiles.length) {
processLogFile(logFiles.shift());
} else if (callback) {
results.sort(function (a, b) {
var d1 = new Date(a.timestamp).getTime();
var d2 = new Date(b.timestamp).getTime();

return d1 > d2 ? 1 : d1 < d2 ? -1 : 0;
});

if (options.order === 'desc') {
results = results.reverse();
}

var start = options.start || 0;
var limit = options.limit || results.length;

results = results.slice(start, start + limit);

if (options.fields) {
results = results.map(function (log) {
var obj = {};
options.fields.forEach(function (key) {
obj[key] = log[key];
});
return obj;
});
}

callback(null, results);
}
});

function add(buff, attempt) {
try {
var log = JSON.parse(buff);
if (!log || typeof log !== 'object') {
return;
}

var time = new Date(log.timestamp);
if ((options.from && time < options.from) || (options.until && time > options.until)) {
return;
}

results.push(log);
} catch (e) {
if (!attempt) {
stream.emit('error', e);
}
}
}
})(logFiles.shift());
};
54 changes: 0 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"homepage": "https://github.com/winstonjs/winston-daily-rotate-file#readme",
"peerDependencies": {
"winston": "^2 || ^3"
"winston": "^3"
},
"devDependencies": {
"chai": "4.2.0",
Expand All @@ -42,9 +42,7 @@
"dependencies": {
"file-stream-rotator": "^0.4.1",
"object-hash": "^1.3.0",
"semver": "^6.2.0",
"triple-beam": "^1.3.0",
"winston-compat": "^0.1.4",
"winston-transport": "^4.2.0"
}
}
Loading

0 comments on commit c1d1a3a

Please sign in to comment.