Skip to content

Commit

Permalink
Merge branch 'master' into add-winston-newrelic-agent-transport
Browse files Browse the repository at this point in the history
  • Loading branch information
DABH committed Feb 4, 2024
2 parents 2f1e16d + f077f30 commit f258c3f
Show file tree
Hide file tree
Showing 13 changed files with 1,440 additions and 2,852 deletions.
18 changes: 0 additions & 18 deletions docs/transports.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ there are additional transports written by
* [Logsene](#logsene-transport) (including Log-Alerts and Anomaly Detection)
* [Logz.io](#logzio-transport)
* [Mail](#mail-transport)
* [Newrelic](#newrelic-transport) (errors only)
* [New Relic](#new-relic-agent-transport)
* [Papertrail](#papertrail-transport)
* [PostgresQL](#postgresql-transport)
Expand Down Expand Up @@ -641,22 +640,6 @@ The Mail transport uses [node-mail][17] behind the scenes. Options are the foll

*Metadata:* Stringified as JSON in email.

### Newrelic Transport

[newrelic-winston][23] is a Newrelic transport:

``` js
const winston = require('winston');
const Newrelic = require('newrelic-winston');
logger.add(new Newrelic(options));
```

The Newrelic transport will send your errors to newrelic and accepts the follwing optins:

* __env__: the current evironment. Defatuls to `process.env.NODE_ENV`

If `env` is either 'dev' or 'test' the lib will _not_ load the included newrelic module saving devs from anoying errors ;)

### New Relic Agent Transport

[winston-newrelic-agent-transport][47] is a New Relic transport that leverages the New Relic agent:
Expand Down Expand Up @@ -1008,7 +991,6 @@ That's why we say it's a logger for just about everything
[20]: https://github.com/jorgebay/winston-cassandra
[21]: https://github.com/jpoon/winston-azuretable
[22]: https://github.com/rickcraig/winston-airbrake2
[23]: https://github.com/namshi/winston-newrelic
[24]: https://github.com/sematext/winston-logsene
[25]: https://github.com/timdp/winston-aws-cloudwatch
[26]: https://github.com/lazywithclass/winston-cloudwatch
Expand Down
11 changes: 11 additions & 0 deletions lib/winston.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ Object.defineProperty(exports, 'exceptions', {
}
});

/**
* Define getter for `rejections` which replaces `handleRejections` and
* `unhandleRejections`.
* @type {Object}
*/
Object.defineProperty(exports, 'rejections', {
get() {
return defaultLogger.rejections;
}
});

/**
* Define getters / setters for appropriate properties of the default logger
* which need to be exposed by winston.
Expand Down
6 changes: 3 additions & 3 deletions lib/winston/rejection-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const asyncForEach = require('async/forEach');
const debug = require('@dabh/diagnostics')('winston:rejection');
const once = require('one-time');
const stackTrace = require('stack-trace');
const ExceptionStream = require('./exception-stream');
const RejectionStream = require('./rejection-stream');

/**
* Object for handling unhandledRejection events.
Expand Down Expand Up @@ -88,7 +88,7 @@ module.exports = class RejectionHandler {
err && err.stack || ' No stack trace'
].join('\n'),
stack: err && err.stack,
exception: true,
rejection: true,
date: new Date().toString(),
process: this.getProcessInfo(),
os: this.getOsInfo(),
Expand Down Expand Up @@ -151,7 +151,7 @@ module.exports = class RejectionHandler {
_addHandler(handler) {
if (!this.handlers.has(handler)) {
handler.handleRejections = true;
const wrapper = new ExceptionStream(handler);
const wrapper = new RejectionStream(handler);
this.handlers.set(handler, wrapper);
this.logger.pipe(wrapper);
}
Expand Down
52 changes: 52 additions & 0 deletions lib/winston/rejection-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* rejection-stream.js: TODO: add file header handler.
*
* (C) 2010 Charlie Robbins
* MIT LICENCE
*/

'use strict';

const { Writable } = require('readable-stream');

/**
* TODO: add class description.
* @type {RejectionStream}
* @extends {Writable}
*/
module.exports = class RejectionStream extends Writable {
/**
* Constructor function for the RejectionStream responsible for wrapping a
* TransportStream; only allowing writes of `info` objects with
* `info.rejection` set to true.
* @param {!TransportStream} transport - Stream to filter to rejections
*/
constructor(transport) {
super({ objectMode: true });

if (!transport) {
throw new Error('RejectionStream requires a TransportStream instance.');
}

this.handleRejections = true;
this.transport = transport;
}

/**
* Writes the info object to our transport instance if (and only if) the
* `rejection` property is set on the info.
* @param {mixed} info - TODO: add param description.
* @param {mixed} enc - TODO: add param description.
* @param {mixed} callback - TODO: add param description.
* @returns {mixed} - TODO: add return description.
* @private
*/
_write(info, enc, callback) {
if (info.rejection) {
return this.transport.log(info, callback);
}

callback();
return true;
}
};
69 changes: 51 additions & 18 deletions lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ module.exports = class File extends TransportStream {
return;
}
if (this.lazy) {
this._endStream(() => {this.emit('fileclosed')});
this._endStream(() => {this.emit('fileclosed');});
return;
}

Expand Down Expand Up @@ -603,12 +603,6 @@ module.exports = class File extends TransportStream {
});

debug('create stream ok', fullpath);
if (this.zippedArchive) {
const gzip = zlib.createGzip();
gzip.pipe(dest);
return gzip;
}

return dest;
}

Expand All @@ -621,13 +615,33 @@ module.exports = class File extends TransportStream {
debug('_incFile', this.filename);
const ext = path.extname(this._basename);
const basename = path.basename(this._basename, ext);
const tasks = [];

if (!this.tailable) {
this._created += 1;
this._checkMaxFilesIncrementing(ext, basename, callback);
} else {
this._checkMaxFilesTailable(ext, basename, callback);
if (this.zippedArchive) {
tasks.push(
function (cb) {
const num = this._created > 0 && !this.tailable ? this._created : '';
this._compressFile(
path.join(this.dirname, `${basename}${num}${ext}`),
path.join(this.dirname, `${basename}${num}${ext}.gz`),
cb
);
}.bind(this)
);
}

tasks.push(
function (cb) {
if (!this.tailable) {
this._created += 1;
this._checkMaxFilesIncrementing(ext, basename, cb);
} else {
this._checkMaxFilesTailable(ext, basename, cb);
}
}.bind(this)
);

asyncSeries(tasks, callback);
}

/**
Expand All @@ -646,13 +660,9 @@ module.exports = class File extends TransportStream {
// Caveat emptor (indexzero): rotationFormat() was broken by design When
// combined with max files because the set of files to unlink is never
// stored.
const target = !this.tailable && this._created
return !this.tailable && this._created
? `${basename}${isRotation}${ext}`
: `${basename}${ext}`;

return this.zippedArchive && !this.tailable
? `${target}.gz`
: target;
}

/**
Expand Down Expand Up @@ -715,13 +725,36 @@ module.exports = class File extends TransportStream {

asyncSeries(tasks, () => {
fs.rename(
path.join(this.dirname, `${basename}${ext}`),
path.join(this.dirname, `${basename}${ext}${isZipped}`),
path.join(this.dirname, `${basename}1${ext}${isZipped}`),
callback
);
});
}

/**
* Compresses src to dest with gzip and unlinks src
* @param {string} src - path to source file.
* @param {string} dest - path to zipped destination file.
* @param {Function} callback - callback called after file has been compressed.
* @returns {undefined}
* @private
*/
_compressFile(src, dest, callback) {
fs.access(src, fs.F_OK, (err) => {
if (err) {
return callback();
}
var gzip = zlib.createGzip();
var inp = fs.createReadStream(src);
var out = fs.createWriteStream(dest);
out.on('finish', () => {
fs.unlink(src, callback);
});
inp.pipe(gzip).pipe(out);
});
}

_createLogDirIfNotExist(dirPath) {
/* eslint-disable no-sync */
if (!fs.existsSync(dirPath)) {
Expand Down
Loading

0 comments on commit f258c3f

Please sign in to comment.