Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/logging/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down
28 changes: 12 additions & 16 deletions packages/logging/src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 comment was marked as spam.

This comment was marked as spam.


this.metadata_ = new Metadata(logging);

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
31 changes: 16 additions & 15 deletions packages/logging/test/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -321,7 +331,7 @@ describe('Log', function() {
};

beforeEach(function() {
log.decorateEntries_ = function(entries, options, callback) {
log.decorateEntries_ = function(entries, callback) {
callback(null, entries);
};
});
Expand All @@ -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);
};
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down