From 7dc63ff29829666e78767c0e0dda21a3516d359f Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 15 Mar 2017 11:37:50 -0700 Subject: [PATCH 1/6] Make removeCircular a property of the Log object --- packages/logging/src/index.js | 7 +++++-- packages/logging/src/log.js | 26 ++++++++++---------------- packages/logging/test/log.js | 29 ++++++++++++++--------------- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/packages/logging/src/index.js b/packages/logging/src/index.js index c2e46b2bedf..8b0e0b1169d 100644 --- a/packages/logging/src/index.js +++ b/packages/logging/src/index.js @@ -452,13 +452,16 @@ Logging.prototype.getSinksStream = common.paginator.streamify('getSinks'); * @resource [Log Overview]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs} * * @param {string} name - Name of the existing log. + * @param {object} options - Configuration object. + * @param {boolean} options.removeCircular - Replace circular references in an + * logged objects with a string value, `[Circular]`. * @return {module:logging/log} * * @example * var log = logging.log('my-log'); */ -Logging.prototype.log = function(name) { - return new Log(this, name); +Logging.prototype.log = function(name, options) { + return new Log(this, name, options); }; /** diff --git a/packages/logging/src/log.js b/packages/logging/src/log.js index b14cb486627..21370b9a977 100644 --- a/packages/logging/src/log.js +++ b/packages/logging/src/log.js @@ -57,13 +57,19 @@ var Metadata = require('./metadata.js'); * @constructor * * @param {string} name - Name of the log. + * @param {object} options - Configuration object. + * @param {boolean} options.removeCircular - Replace circular references in an + * logged objects with a string value, `[Circular]`. * * @example * var log = logging.log('syslog'); */ -function Log(logging, name) { +function Log(logging, name, options) { + options = options || {}; + this.formattedName_ = Log.formatName_(logging.projectId, name); this.name = this.formattedName_.split('/').pop(); + this.removeCircular_ = !!options.removeCircular; this.metadata_ = new Metadata(logging); @@ -520,8 +526,6 @@ Log.prototype.warning = function(entry, options, callback) { * @param {object[]} options.labels - Labels to set on the log. * @param {object} options.resource - A default monitored resource for entries * where one isn't specified. - * @param {boolean} options.removeCircular - Replace circular references in an - * object with a string value, `[Circular]`. * @param {function} callback - The callback function. * @param {?error} callback.err - An error returned while making this request. * @param {object} callback.apiResponse - The full API response. @@ -599,9 +603,7 @@ Log.prototype.write = function(entry, options, callback) { var entries = arrify(entry); - this.decorateEntries_(entries, { - removeCircular: options.removeCircular - }, function(err, decoratedEntries) { + this.decorateEntries_(entries, function(err, decoratedEntries) { // Ignore errors (the API will speak up if it has an issue). reqOpts.entries = decoratedEntries; @@ -619,24 +621,16 @@ Log.prototype.write = function(entry, options, callback) { * @private * * @param {object} entry - An entry object. - * @param {object} options - configuration object - * @param {boolean} options.removeCircular - Replace circular references in an - * object with a string value, `[Circular]`. */ -Log.prototype.decorateEntries_ = function(entries, options, callback) { +Log.prototype.decorateEntries_ = function(entries, callback) { var self = this; - if (is.fn(options)) { - callback = options; - options = {}; - } - async.map(entries, function(entry, callback) { if (!(entry instanceof Entry)) { entry = self.entry(entry); } - var decoratedEntry = entry.toJSON(options); + var decoratedEntry = entry.toJSON({ removeCircular: self.removeCircular_ }); decoratedEntry.logName = self.formattedName_; self.metadata_.assignDefaultResource(decoratedEntry, function(err, entry) { diff --git a/packages/logging/test/log.js b/packages/logging/test/log.js index 100d1d7e512..3fa449279e6 100644 --- a/packages/logging/test/log.js +++ b/packages/logging/test/log.js @@ -139,6 +139,14 @@ describe('Log', function() { } }); }); + + it('should accept and localize options.removeCircular', function() { + assert.strictEqual(log.removeCircular_, false); + + var options = { removeCircular: true }; + log = new Log(LOGGING, LOG_NAME_FORMATTED, options); + assert.strictEqual(log.removeCircular_, true); + }); }); describe('assignSeverityToEntries_', function() { @@ -321,7 +329,7 @@ describe('Log', function() { }; beforeEach(function() { - log.decorateEntries_ = function(entries, options, callback) { + log.decorateEntries_ = function(entries, callback) { callback(null, entries); }; }); @@ -343,7 +351,7 @@ describe('Log', function() { it('should arrify & decorate the entries', function(done) { var decoratedEntries = []; - log.decorateEntries_ = function(entries, options, callback) { + log.decorateEntries_ = function(entries, callback) { assert.strictEqual(entries[0], ENTRY); callback(null, decoratedEntries); }; @@ -380,15 +388,6 @@ describe('Log', function() { log.write(ENTRY, done); }); - - it('should pass options.removeCircular to decorateEntries', function(done) { - log.decorateEntries_ = function(entries, options) { - assert.strictEqual(options.removeCircular, true); - done(); - }; - - log.write(ENTRY, { removeCircular: true }, assert.ifError); - }); }); describe('severity shortcuts', function() { @@ -682,17 +681,17 @@ describe('Log', function() { }); }); - it('should pass options to toJSON', function(done) { - var options = {}; + it('should pass log.removeCircular to toJSON', function(done) { + log.removeCircular_ = true; var entry = new Entry(); entry.toJSON = function(options_) { - assert.strictEqual(options_, options); + assert.deepStrictEqual(options_, { removeCircular: true }); setImmediate(done); return {}; }; - log.decorateEntries_([entry], options, assert.ifError); + log.decorateEntries_([entry], assert.ifError); }); it('should assign the log name', function(done) { From 82ab1c9f2cbe5bfba35afaaf1f484b4536ba0632 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 15 Mar 2017 13:26:53 -0700 Subject: [PATCH 2/6] address comments --- packages/logging/src/log.js | 8 +++++--- packages/logging/test/log.js | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/logging/src/log.js b/packages/logging/src/log.js index 21370b9a977..2a9bf1754db 100644 --- a/packages/logging/src/log.js +++ b/packages/logging/src/log.js @@ -57,9 +57,9 @@ var Metadata = require('./metadata.js'); * @constructor * * @param {string} name - Name of the log. - * @param {object} options - Configuration object. + * @param {object=} options - Configuration object. * @param {boolean} options.removeCircular - Replace circular references in an - * logged objects with a string value, `[Circular]`. + * logged objects with a string value, `[Circular]`. (Default: false) * * @example * var log = logging.log('syslog'); @@ -630,7 +630,9 @@ Log.prototype.decorateEntries_ = function(entries, callback) { entry = self.entry(entry); } - var decoratedEntry = entry.toJSON({ removeCircular: self.removeCircular_ }); + var decoratedEntry = entry.toJSON({ + removeCircular: self.removeCircular_ + }); decoratedEntry.logName = self.formattedName_; self.metadata_.assignDefaultResource(decoratedEntry, function(err, entry) { diff --git a/packages/logging/test/log.js b/packages/logging/test/log.js index 3fa449279e6..d933431946c 100644 --- a/packages/logging/test/log.js +++ b/packages/logging/test/log.js @@ -101,6 +101,10 @@ describe('Log', function() { assert.strictEqual(log.name, LOG_NAME_ENCODED); }); + it('should localize removeCircular_ to default value', function() { + assert.strictEqual(log.removeCircular_, false); + }); + it('should localize the formatted name', function() { var formattedName = 'formatted-name'; @@ -144,7 +148,7 @@ describe('Log', function() { assert.strictEqual(log.removeCircular_, false); var options = { removeCircular: true }; - log = new Log(LOGGING, LOG_NAME_FORMATTED, options); + var log = new Log(LOGGING, LOG_NAME_FORMATTED, options); assert.strictEqual(log.removeCircular_, true); }); }); From fedd677befe52b6b27e4a3cf89ebf1e8d3453613 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 15 Mar 2017 13:32:16 -0700 Subject: [PATCH 3/6] comments 2 --- packages/logging/src/index.js | 4 ++-- packages/logging/src/log.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/logging/src/index.js b/packages/logging/src/index.js index 8b0e0b1169d..a305d6377b6 100644 --- a/packages/logging/src/index.js +++ b/packages/logging/src/index.js @@ -453,8 +453,8 @@ Logging.prototype.getSinksStream = common.paginator.streamify('getSinks'); * * @param {string} name - Name of the existing log. * @param {object} options - Configuration object. - * @param {boolean} options.removeCircular - Replace circular references in an - * logged objects with a string value, `[Circular]`. + * @param {boolean} options.removeCircular - Replace circular references in + * logged objects with a string value, `[Circular]`. (Default: false) * @return {module:logging/log} * * @example diff --git a/packages/logging/src/log.js b/packages/logging/src/log.js index 2a9bf1754db..771a038c3ef 100644 --- a/packages/logging/src/log.js +++ b/packages/logging/src/log.js @@ -58,8 +58,8 @@ var Metadata = require('./metadata.js'); * * @param {string} name - Name of the log. * @param {object=} options - Configuration object. - * @param {boolean} options.removeCircular - Replace circular references in an - * logged objects with a string value, `[Circular]`. (Default: false) + * @param {boolean} options.removeCircular - Replace circular references in + * logged object with a string value, `[Circular]`. (Default: false) * * @example * var log = logging.log('syslog'); From d2e749b9c14aab4b8c291e64df4fe8cbe2e63c69 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 15 Mar 2017 13:33:15 -0700 Subject: [PATCH 4/6] more grammar --- packages/logging/src/log.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logging/src/log.js b/packages/logging/src/log.js index 771a038c3ef..7a59c70919d 100644 --- a/packages/logging/src/log.js +++ b/packages/logging/src/log.js @@ -59,7 +59,7 @@ var Metadata = require('./metadata.js'); * @param {string} name - Name of the log. * @param {object=} options - Configuration object. * @param {boolean} options.removeCircular - Replace circular references in - * logged object with a string value, `[Circular]`. (Default: false) + * logged objects with a string value, `[Circular]`. (Default: false) * * @example * var log = logging.log('syslog'); From 9aa74fa8584567d21458ab7763f95b5823375780 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 15 Mar 2017 13:37:44 -0700 Subject: [PATCH 5/6] object= one more place --- packages/logging/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logging/src/index.js b/packages/logging/src/index.js index a305d6377b6..83bbf2f10ba 100644 --- a/packages/logging/src/index.js +++ b/packages/logging/src/index.js @@ -452,7 +452,7 @@ Logging.prototype.getSinksStream = common.paginator.streamify('getSinks'); * @resource [Log Overview]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs} * * @param {string} name - Name of the existing log. - * @param {object} options - Configuration object. + * @param {object=} options - Configuration object. * @param {boolean} options.removeCircular - Replace circular references in * logged objects with a string value, `[Circular]`. (Default: false) * @return {module:logging/log} From 83bf7652a840b192717462b847265cd9a13635e3 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 15 Mar 2017 14:05:29 -0700 Subject: [PATCH 6/6] ditch the check --- packages/logging/test/log.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/logging/test/log.js b/packages/logging/test/log.js index d933431946c..b187a315404 100644 --- a/packages/logging/test/log.js +++ b/packages/logging/test/log.js @@ -145,8 +145,6 @@ describe('Log', function() { }); it('should accept and localize options.removeCircular', function() { - assert.strictEqual(log.removeCircular_, false); - var options = { removeCircular: true }; var log = new Log(LOGGING, LOG_NAME_FORMATTED, options); assert.strictEqual(log.removeCircular_, true);