diff --git a/packages/logging/src/index.js b/packages/logging/src/index.js index c2e46b2bedf..83bbf2f10ba 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 + * logged objects with a string value, `[Circular]`. (Default: false) * @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..7a59c70919d 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 + * logged objects with a string value, `[Circular]`. (Default: false) * * @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,18 @@ 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..b187a315404 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'; @@ -139,6 +143,12 @@ describe('Log', function() { } }); }); + + it('should accept and localize options.removeCircular', function() { + var options = { removeCircular: true }; + var log = new Log(LOGGING, LOG_NAME_FORMATTED, options); + assert.strictEqual(log.removeCircular_, true); + }); }); describe('assignSeverityToEntries_', function() { @@ -321,7 +331,7 @@ describe('Log', function() { }; beforeEach(function() { - log.decorateEntries_ = function(entries, options, callback) { + log.decorateEntries_ = function(entries, callback) { callback(null, entries); }; }); @@ -343,7 +353,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 +390,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 +683,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) {