GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — logging-winston: metadata serialization · ofrobots/google-cloud-node@87a5ce9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/logging-winston
    • package.json
    • src
      • index.js
    • system-test
      • logging-winston.js
    • test
      • index.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/logging-winston/package.json‎

Copy file name to clipboardExpand all lines: packages/logging-winston/package.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@google-cloud/logging": "^1.0.0",
3333
"extend": "^3.0.0",
3434
"is": "^3.2.0",
35+
"lodash.mapvalues": "^4.6.0",
3536
"winston": "^2.2.0"
3637
},
3738
"devDependencies": {
Collapse file

‎packages/logging-winston/src/index.js‎

Copy file name to clipboardExpand all lines: packages/logging-winston/src/index.js
+12-11Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var extend = require('extend');
2424
var is = require('is');
2525
var logging = require('@google-cloud/logging');
26+
var mapValues = require('lodash.mapvalues');
2627
var util = require('util');
2728
var winston = require('winston');
2829

@@ -89,6 +90,9 @@ var STACKDRIVER_LOGGING_LEVEL_CODE_TO_NAME = {
8990
* but you may optionally specify a specific monitored resource. For more
9091
* information see the
9192
* [official documentation]{@link https://cloud.google.com/logging/docs/api/reference/rest/v2/MonitoredResource}.
93+
* @param {boolean=} options.inspectMetadata - whether to serialize winston
94+
* provided log metadata using `util.inspect`. If omitted or set to false,
95+
* we will use the built-in gRPC serialization. Default: false
9296
*
9397
* @example
9498
* var transport = require('@google-cloud/logging-winston');
@@ -124,6 +128,7 @@ function LoggingWinston(options) {
124128
this.levels_ = options.levels || NPM_LEVEL_NAME_TO_CODE;
125129
this.log_ = logging(options).log(logName);
126130
this.resource_ = options.resource;
131+
this.inspectMetadata_ = options.inspectMetadata || false;
127132
}
128133

129134
winston.transports.StackdriverLogging = LoggingWinston;
@@ -157,19 +162,8 @@ LoggingWinston.prototype.log = function(levelName, msg, metadata, callback) {
157162
var levelCode = this.levels_[levelName];
158163
var stackdriverLevel = STACKDRIVER_LOGGING_LEVEL_CODE_TO_NAME[levelCode];
159164

160-
var labels = {};
161-
162-
if (is.object(metadata)) {
163-
// We attach properties as labels on the log entry. Logging proto requires
164-
// that the label values be strings, so we convert using util.inspect.
165-
for (var key in metadata) {
166-
labels[key] = util.inspect(metadata[key]);
167-
}
168-
}
169-
170165
var entryMetadata = {
171166
resource: this.resource_,
172-
labels: labels
173167
};
174168

175169
// Stackdriver Logs Viewer picks up the summary line from the `message`
@@ -194,6 +188,13 @@ LoggingWinston.prototype.log = function(levelName, msg, metadata, callback) {
194188
var data = {
195189
message: msg
196190
};
191+
if (is.object(metadata)) {
192+
data.metadata =
193+
this.inspectMetadata_ ?
194+
mapValues(metadata, function(v) { return util.inspect(v); }) :
195+
metadata;
196+
}
197+
197198
var entry = this.log_.entry(entryMetadata, data);
198199
this.log_[stackdriverLevel](entry, callback);
199200
};
Collapse file

‎packages/logging-winston/system-test/logging-winston.js‎

Copy file name to clipboardExpand all lines: packages/logging-winston/system-test/logging-winston.js
+14-8Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,34 @@ describe('LoggingWinston', function() {
4242
args: ['first'],
4343
level: 'info',
4444
verify: function(entry) {
45-
assert.deepStrictEqual(entry.data, {message: 'first'});
45+
assert.deepStrictEqual(entry.data, {
46+
message: 'first',
47+
metadata: {}
48+
});
4649
}
4750
},
4851

4952
{
5053
args: ['second'],
5154
level: 'info',
5255
verify: function(entry) {
53-
assert.deepStrictEqual(entry.data, {message: 'second'});
56+
assert.deepStrictEqual(entry.data, {
57+
message: 'second',
58+
metadata: {}
59+
});
5460
}
5561
},
5662

5763
{
5864
args: ['third', {testTimestamp: testTimestamp}],
5965
level: 'info',
6066
verify: function(entry) {
61-
assert.deepStrictEqual(entry.data, {message: 'third'});
62-
63-
assert.strictEqual(
64-
entry.metadata.labels.testTimestamp,
65-
util.inspect(testTimestamp)
66-
);
67+
assert.deepStrictEqual(entry.data, {
68+
message: 'third',
69+
metadata: {
70+
testTimestamp: String(testTimestamp)
71+
}
72+
});
6773
}
6874
},
6975

Collapse file

‎packages/logging-winston/test/index.js‎

Copy file name to clipboardExpand all lines: packages/logging-winston/test/index.js
+47-10Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ describe('logging-winston', function() {
154154
it('should localize the provided resource', function() {
155155
assert.strictEqual(loggingWinston.resource_, OPTIONS.resource);
156156
});
157+
158+
it('should localize inspectMetadata to default value', function() {
159+
assert.strictEqual(loggingWinston.inspectMetadata_, false);
160+
});
161+
162+
it('should localize the provided options.inspectMetadata', function() {
163+
var optionsWithInspectMetadata = extend({}, OPTIONS);
164+
optionsWithInspectMetadata.inspectMetadata = true;
165+
166+
var loggingWinston = new LoggingWinston(optionsWithInspectMetadata);
167+
assert.strictEqual(loggingWinston.inspectMetadata_, optionsWithInspectMetadata.inspectMetadata);
168+
});
157169
});
158170

159171
describe('log', function() {
@@ -182,15 +194,36 @@ describe('logging-winston', function() {
182194

183195
it('should properly create an entry', function(done) {
184196
loggingWinston.log_.entry = function(entryMetadata, data) {
185-
var expectedLabels = {};
197+
assert.deepEqual(entryMetadata, {
198+
resource: loggingWinston.resource_
199+
});
200+
assert.deepStrictEqual(data, {
201+
message: MESSAGE,
202+
metadata: METADATA
203+
});
204+
done();
205+
};
206+
207+
loggingWinston.log(LEVEL, MESSAGE, METADATA, assert.ifError);
208+
});
209+
210+
it('should inspect metadata when inspectMetadata is set', function(done) {
211+
var optionsWithInspectMetadata = extend({}, OPTIONS);
212+
optionsWithInspectMetadata.inspectMetadata = true;
213+
214+
var loggingWinston = new LoggingWinston(optionsWithInspectMetadata);
215+
loggingWinston.log_.entry = function(entryMetadata, data) {
216+
var expectedWinstonMetadata = {};
186217
for (var prop in METADATA) {
187-
expectedLabels[prop] = nodeutil.inspect(METADATA[prop]);
218+
expectedWinstonMetadata[prop] = nodeutil.inspect(METADATA[prop]);
188219
}
189220
assert.deepEqual(entryMetadata, {
190-
resource: loggingWinston.resource_,
191-
labels: expectedLabels
221+
resource: loggingWinston.resource_
222+
});
223+
assert.deepStrictEqual(data, {
224+
message: MESSAGE,
225+
metadata: expectedWinstonMetadata
192226
});
193-
assert.deepStrictEqual(data, { message: MESSAGE });
194227
done();
195228
};
196229

@@ -204,7 +237,8 @@ describe('logging-winston', function() {
204237

205238
loggingWinston.log_.entry = function(entryMetadata, data) {
206239
assert.deepStrictEqual(data, {
207-
message: MESSAGE + ' ' + error.stack
240+
message: MESSAGE + ' ' + error.stack,
241+
metadata: error
208242
});
209243
done();
210244
};
@@ -219,7 +253,8 @@ describe('logging-winston', function() {
219253

220254
loggingWinston.log_.entry = function(entryMetadata, data) {
221255
assert.deepStrictEqual(data, {
222-
message: error.stack
256+
message: error.stack,
257+
metadata: error
223258
});
224259
done();
225260
};
@@ -230,10 +265,12 @@ describe('logging-winston', function() {
230265
it('should not require metadata', function(done) {
231266
loggingWinston.log_.entry = function(entryMetadata, data) {
232267
assert.deepEqual(entryMetadata, {
233-
resource: loggingWinston.resource_,
234-
labels: {}
268+
resource: loggingWinston.resource_
269+
});
270+
assert.deepStrictEqual(data, {
271+
message: MESSAGE,
272+
metadata: {}
235273
});
236-
assert.deepStrictEqual(data, { message: MESSAGE });
237274
done();
238275
};
239276

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.