-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added TransportStream option levelOnly as per request… #103
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ const { LEVEL } = require('triple-beam'); | |
* that all `winston >= 3` transports should inherit from. | ||
* @param {Object} options - Options for this TransportStream instance | ||
* @param {String} options.level - Highest level according to RFC5424. | ||
* @param {String} options.levelOnly - If true, will ONLY log info | ||
* with a log level equal to this level, nothing less or more than. default = false | ||
* @param {Boolean} options.handleExceptions - If true, info with | ||
* { exception: true } will be written. | ||
* @param {Function} options.log - Custom log function for simple Transport | ||
|
@@ -20,6 +22,7 @@ const TransportStream = module.exports = function TransportStream(options = {}) | |
|
||
this.format = options.format; | ||
this.level = options.level; | ||
this.levelOnly = (options.levelOnly === true); // prevoius behaviour = not defined ~= false | ||
this.handleExceptions = options.handleExceptions; | ||
this.handleRejections = options.handleRejections; | ||
this.silent = options.silent; | ||
|
@@ -58,6 +61,55 @@ const TransportStream = module.exports = function TransportStream(options = {}) | |
*/ | ||
util.inherits(TransportStream, Writable); | ||
|
||
/** | ||
* Returns true if the info log level means it should be logged | ||
* @param {mixed} info - TODO: add param description. | ||
* @returns {boolean} - TODO: add returns description. | ||
Comment on lines
+66
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR leaves some TODOs open here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These TODOs are inherited from the original method that I extracted the code from. I just split the method out to fix the worst eslint issues that were breaking the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had looked in the diff for that but didn't see a corresponding line being moved from elsewhere. Where was it extracted from? |
||
* @private | ||
*/ | ||
TransportStream.prototype._infoLevelLoggable = function _infoLevelLoggable(info) { | ||
// Remark: This has to be handled in the base transport now because we | ||
// cannot conditionally write to our pipe targets as stream. We always | ||
// prefer any explicit level set on the Transport itself falling back to | ||
// any level set on the parent. | ||
// We always prefer any explicit level set on the Transport itself | ||
// falling back to any level set on the parent. | ||
const level = this.level || (this.parent && this.parent.level); | ||
|
||
return ( | ||
!level || | ||
(!this.levelOnly && this.levels[level] >= this.levels[info[LEVEL]]) || | ||
(this.levelOnly && this.levels[level] === this.levels[info[LEVEL]]) | ||
); | ||
}; | ||
|
||
/** | ||
* trap any errors generated by the user-provided format | ||
* @param {mixed} info - TODO: add param description. | ||
* @returns {object} - TODO: add returns description. | ||
* @private | ||
*/ | ||
TransportStream.prototype._transformCatchErrorState = function _transformCatchErrorState(info) { | ||
let errState; | ||
let transformed; | ||
|
||
// We trap(and re-throw) any errors generated by the user-provided format, but also | ||
// guarantee that the streams callback is invoked so that we can continue flowing. | ||
try { | ||
transformed = this.format.transform( | ||
Object.assign({}, info), | ||
this.format.options | ||
); | ||
} catch (err) { | ||
errState = err; | ||
} | ||
|
||
return { | ||
transformed, | ||
errState | ||
}; | ||
}; | ||
|
||
/** | ||
* Writes the info object to our transport instance. | ||
* @param {mixed} info - TODO: add param description. | ||
|
@@ -71,27 +123,14 @@ TransportStream.prototype._write = function _write(info, enc, callback) { | |
return callback(null); | ||
} | ||
|
||
// Remark: This has to be handled in the base transport now because we | ||
// cannot conditionally write to our pipe targets as stream. We always | ||
// prefer any explicit level set on the Transport itself falling back to | ||
// any level set on the parent. | ||
const level = this.level || (this.parent && this.parent.level); | ||
|
||
if (!level || this.levels[level] >= this.levels[info[LEVEL]]) { | ||
if (this._infoLevelLoggable(info)) { | ||
if (info && !this.format) { | ||
return this.log(info, callback); | ||
} | ||
|
||
let errState; | ||
let transformed; | ||
|
||
// We trap(and re-throw) any errors generated by the user-provided format, but also | ||
// trap(and re-throw) any errors generated by the user-provided format, but also | ||
// guarantee that the streams callback is invoked so that we can continue flowing. | ||
try { | ||
transformed = this.format.transform(Object.assign({}, info), this.format.options); | ||
} catch (err) { | ||
errState = err; | ||
} | ||
const { transformed, errState } = this._transformCatchErrorState(info); | ||
|
||
if (errState || !transformed) { | ||
// eslint-disable-next-line callback-return | ||
|
@@ -135,19 +174,9 @@ TransportStream.prototype._writev = function _writev(chunks, callback) { | |
continue; | ||
} | ||
|
||
let errState; | ||
let transformed; | ||
|
||
// We trap(and re-throw) any errors generated by the user-provided format, but also | ||
// trap(and re-throw) any errors generated by the user-provided format, but also | ||
// guarantee that the streams callback is invoked so that we can continue flowing. | ||
try { | ||
transformed = this.format.transform( | ||
Object.assign({}, chunks[i].chunk), | ||
this.format.options | ||
); | ||
} catch (err) { | ||
errState = err; | ||
} | ||
const { transformed, errState } = this._transformCatchErrorState(chunks[i].chunk); | ||
|
||
if (errState || !transformed) { | ||
// eslint-disable-next-line callback-return | ||
|
@@ -180,16 +209,8 @@ TransportStream.prototype._accept = function _accept(write) { | |
return false; | ||
} | ||
|
||
// We always prefer any explicit level set on the Transport itself | ||
// falling back to any level set on the parent. | ||
const level = this.level || (this.parent && this.parent.level); | ||
|
||
// Immediately check the average case: log level filtering. | ||
if ( | ||
info.exception === true || | ||
!level || | ||
this.levels[level] >= this.levels[info[LEVEL]] | ||
) { | ||
if (info.exception === true || this._infoLevelLoggable(info)) { | ||
// Ensure the info object is valid based on `{ exception }`: | ||
// 1. { handleExceptions: true }: all `info` objects are valid | ||
// 2. { exception: false }: accepted by all transports. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.