Skip to content

Commit 54bb503

Browse files
committed
logging: allow for encoding options
1 parent 79e0e28 commit 54bb503

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

lib/logging/entry.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Entry.fromApiResponse_ = function(entry) {
133133
*
134134
* @private
135135
*/
136-
Entry.prototype.toJSON = function() {
136+
Entry.prototype.toJSON = function(options) {
137137
var entry = extend(true, {}, this);
138138

139139
var whitelist = [
@@ -160,9 +160,9 @@ Entry.prototype.toJSON = function() {
160160
}
161161

162162
if (is.object(this.data)) {
163-
entry.jsonPayload = GrpcService.objToStruct_(this.data, {
163+
entry.jsonPayload = GrpcService.objToStruct_(this.data, extend({
164164
serialize: true
165-
});
165+
}, options));
166166
} else if (is.string(this.data)) {
167167
entry.textPayload = this.data;
168168
}

lib/logging/log.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ Log.prototype.warning = function(entry, options, callback) {
474474
* log.write(entries, options, function(err, apiResponse) {});
475475
*/
476476
Log.prototype.write = function(entry, options, callback) {
477+
var self = this;
478+
477479
if (is.fn(options)) {
478480
callback = options;
479481
options = {};
@@ -486,9 +488,13 @@ Log.prototype.write = function(entry, options, callback) {
486488

487489
var reqOpts = extend({
488490
logName: this.formattedName_,
489-
entries: arrify(entry).map(this.formatEntryForApi_.bind(this))
491+
entries: arrify(entry).map(function(entry) {
492+
return self.formatEntryForApi_(entry, options.encoding);
493+
})
490494
}, options);
491495

496+
delete reqOpts.encoding;
497+
492498
this.request(protoOpts, reqOpts, function(err, resp) {
493499
callback(err, resp);
494500
});
@@ -502,12 +508,12 @@ Log.prototype.write = function(entry, options, callback) {
502508
*
503509
* @param {object} entry - An entry object.
504510
*/
505-
Log.prototype.formatEntryForApi_ = function(entry) {
511+
Log.prototype.formatEntryForApi_ = function(entry, options) {
506512
if (!(entry instanceof Entry)) {
507513
entry = this.entry(entry);
508514
}
509515

510-
var formattedEntry = entry.toJSON();
516+
var formattedEntry = entry.toJSON(options);
511517
formattedEntry.logName = this.formattedName_;
512518
return formattedEntry;
513519
};

system-test/logging.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,18 @@ describe('Logging', function() {
347347
});
348348
});
349349

350-
it('should write an entry with primitive values', function(done) {
350+
it('should stringify unknown value types', function(done) {
351351
var logEntry = log.entry({
352352
when: new Date(),
353353
matchUser: /username: (.+)/,
354354
matchUserError: new Error('No user found.'),
355355
shouldNotBeSaved: undefined
356356
});
357357

358+
options.encoding = {
359+
stringify: true
360+
};
361+
358362
log.write(logEntry, options, function(err) {
359363
assert.ifError(err);
360364

0 commit comments

Comments
 (0)