123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- 'use strict';
- const util = require('util');
- const Writable = require('readable-stream/writable');
- const { LEVEL } = require('triple-beam');
- const TransportStream = module.exports = function TransportStream(options = {}) {
- Writable.call(this, { objectMode: true });
- this.format = options.format;
- this.level = options.level;
- this.handleExceptions = options.handleExceptions;
- this.silent = options.silent;
- if (options.log) this.log = options.log;
- if (options.logv) this.logv = options.logv;
- if (options.close) this.close = options.close;
-
- this.once('pipe', logger => {
-
-
-
-
- this.levels = logger.levels;
- this.parent = logger;
- });
-
- this.once('unpipe', src => {
-
-
-
-
- if (src === this.parent) {
- this.parent = null;
- if (this.close) {
- this.close();
- }
- }
- });
- };
- util.inherits(TransportStream, Writable);
- TransportStream.prototype._write = function _write(info, enc, callback) {
- if (this.silent || (info.exception === true && !this.handleExceptions)) {
- return callback(null);
- }
-
-
-
-
- const level = this.level || (this.parent && this.parent.level);
- if (!level || this.levels[level] >= this.levels[info[LEVEL]]) {
- if (info && !this.format) {
- return this.log(info, callback);
- }
- let errState;
- let transformed;
-
-
- try {
- transformed = this.format.transform(Object.assign({}, info), this.format.options);
- } catch (err) {
- errState = err;
- }
- if (errState || !transformed) {
-
- callback();
- if (errState) throw errState;
- return;
- }
- return this.log(transformed, callback);
- }
- return callback(null);
- };
- TransportStream.prototype._writev = function _writev(chunks, callback) {
- if (this.logv) {
- const infos = chunks.filter(this._accept, this);
- if (!infos.length) {
- return callback(null);
- }
-
-
-
- return this.logv(infos, callback);
- }
- for (let i = 0; i < chunks.length; i++) {
- if (!this._accept(chunks[i])) continue;
- if (chunks[i].chunk && !this.format) {
- this.log(chunks[i].chunk, chunks[i].callback);
- continue;
- }
- let errState;
- let transformed;
-
-
- try {
- transformed = this.format.transform(
- Object.assign({}, chunks[i].chunk),
- this.format.options
- );
- } catch (err) {
- errState = err;
- }
- if (errState || !transformed) {
-
- chunks[i].callback();
- if (errState) {
-
- callback(null);
- throw errState;
- }
- } else {
- this.log(transformed, chunks[i].callback);
- }
- }
- return callback(null);
- };
- TransportStream.prototype._accept = function _accept(write) {
- const info = write.chunk;
- if (this.silent) {
- return false;
- }
-
-
- const level = this.level || (this.parent && this.parent.level);
-
- if (
- info.exception === true ||
- !level ||
- this.levels[level] >= this.levels[info[LEVEL]]
- ) {
-
-
-
- if (this.handleExceptions || info.exception !== true) {
- return true;
- }
- }
- return false;
- };
- TransportStream.prototype._nop = function _nop() {
-
- return void undefined;
- };
|