GeezerVison is showing github.com — logging-winston: metadata serialization · ofrobots/google-cloud-node@87a5ce9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/logging-winston/package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 32 | 32 | "@google-cloud/logging": "^1.0.0", | 33 | 33 | "extend": "^3.0.0", | 34 | 34 | "is": "^3.2.0", | | 35 | + "lodash.mapvalues": "^4.6.0", | 35 | 36 | "winston": "^2.2.0" | 36 | 37 | }, | 37 | 38 | "devDependencies": { | |
Collapse file Copy file name to clipboardExpand all lines: packages/logging-winston/src/index.js +12-11Lines changed: 12 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 | var extend = require('extend'); | 24 | 24 | var is = require('is'); | 25 | 25 | var logging = require('@google-cloud/logging'); | | 26 | +var mapValues = require('lodash.mapvalues'); | 26 | 27 | var util = require('util'); | 27 | 28 | var winston = require('winston'); | 28 | 29 |
| @@ -89,6 +90,9 @@ var STACKDRIVER_LOGGING_LEVEL_CODE_TO_NAME = { | 89 | 90 | * but you may optionally specify a specific monitored resource. For more | 90 | 91 | * information see the | 91 | 92 | * [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 | 92 | 96 | * | 93 | 97 | * @example | 94 | 98 | * var transport = require('@google-cloud/logging-winston'); | @@ -124,6 +128,7 @@ function LoggingWinston(options) { | 124 | 128 | this.levels_ = options.levels || NPM_LEVEL_NAME_TO_CODE; | 125 | 129 | this.log_ = logging(options).log(logName); | 126 | 130 | this.resource_ = options.resource; | | 131 | + this.inspectMetadata_ = options.inspectMetadata || false; | 127 | 132 | } | 128 | 133 |
| 129 | 134 | winston.transports.StackdriverLogging = LoggingWinston; | @@ -157,19 +162,8 @@ LoggingWinston.prototype.log = function(levelName, msg, metadata, callback) { | 157 | 162 | var levelCode = this.levels_[levelName]; | 158 | 163 | var stackdriverLevel = STACKDRIVER_LOGGING_LEVEL_CODE_TO_NAME[levelCode]; | 159 | 164 |
| 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 | | - | 170 | 165 | var entryMetadata = { | 171 | 166 | resource: this.resource_, | 172 | | - labels: labels | 173 | 167 | }; | 174 | 168 |
| 175 | 169 | // Stackdriver Logs Viewer picks up the summary line from the `message` | @@ -194,6 +188,13 @@ LoggingWinston.prototype.log = function(levelName, msg, metadata, callback) { | 194 | 188 | var data = { | 195 | 189 | message: msg | 196 | 190 | }; | | 191 | + if (is.object(metadata)) { | | 192 | + data.metadata = | | 193 | + this.inspectMetadata_ ? | | 194 | + mapValues(metadata, function(v) { return util.inspect(v); }) : | | 195 | + metadata; | | 196 | + } | | 197 | + | 197 | 198 | var entry = this.log_.entry(entryMetadata, data); | 198 | 199 | this.log_[stackdriverLevel](entry, callback); | 199 | 200 | }; | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -42,28 +42,34 @@ describe('LoggingWinston', function() { | 42 | 42 | args: ['first'], | 43 | 43 | level: 'info', | 44 | 44 | verify: function(entry) { | 45 | | - assert.deepStrictEqual(entry.data, {message: 'first'}); | | 45 | + assert.deepStrictEqual(entry.data, { | | 46 | + message: 'first', | | 47 | + metadata: {} | | 48 | + }); | 46 | 49 | } | 47 | 50 | }, | 48 | 51 |
| 49 | 52 | { | 50 | 53 | args: ['second'], | 51 | 54 | level: 'info', | 52 | 55 | verify: function(entry) { | 53 | | - assert.deepStrictEqual(entry.data, {message: 'second'}); | | 56 | + assert.deepStrictEqual(entry.data, { | | 57 | + message: 'second', | | 58 | + metadata: {} | | 59 | + }); | 54 | 60 | } | 55 | 61 | }, | 56 | 62 |
| 57 | 63 | { | 58 | 64 | args: ['third', {testTimestamp: testTimestamp}], | 59 | 65 | level: 'info', | 60 | 66 | 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 | + }); | 67 | 73 | } | 68 | 74 | }, | 69 | 75 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/logging-winston/test/index.js +47-10Lines changed: 47 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -154,6 +154,18 @@ describe('logging-winston', function() { | 154 | 154 | it('should localize the provided resource', function() { | 155 | 155 | assert.strictEqual(loggingWinston.resource_, OPTIONS.resource); | 156 | 156 | }); | | 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 | + }); | 157 | 169 | }); | 158 | 170 |
| 159 | 171 | describe('log', function() { | @@ -182,15 +194,36 @@ describe('logging-winston', function() { | 182 | 194 |
| 183 | 195 | it('should properly create an entry', function(done) { | 184 | 196 | 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 = {}; | 186 | 217 | for (var prop in METADATA) { | 187 | | - expectedLabels[prop] = nodeutil.inspect(METADATA[prop]); | | 218 | + expectedWinstonMetadata[prop] = nodeutil.inspect(METADATA[prop]); | 188 | 219 | } | 189 | 220 | 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 | 192 | 226 | }); | 193 | | - assert.deepStrictEqual(data, { message: MESSAGE }); | 194 | 227 | done(); | 195 | 228 | }; | 196 | 229 |
| @@ -204,7 +237,8 @@ describe('logging-winston', function() { | 204 | 237 |
| 205 | 238 | loggingWinston.log_.entry = function(entryMetadata, data) { | 206 | 239 | assert.deepStrictEqual(data, { | 207 | | - message: MESSAGE + ' ' + error.stack | | 240 | + message: MESSAGE + ' ' + error.stack, | | 241 | + metadata: error | 208 | 242 | }); | 209 | 243 | done(); | 210 | 244 | }; | @@ -219,7 +253,8 @@ describe('logging-winston', function() { | 219 | 253 |
| 220 | 254 | loggingWinston.log_.entry = function(entryMetadata, data) { | 221 | 255 | assert.deepStrictEqual(data, { | 222 | | - message: error.stack | | 256 | + message: error.stack, | | 257 | + metadata: error | 223 | 258 | }); | 224 | 259 | done(); | 225 | 260 | }; | @@ -230,10 +265,12 @@ describe('logging-winston', function() { | 230 | 265 | it('should not require metadata', function(done) { | 231 | 266 | loggingWinston.log_.entry = function(entryMetadata, data) { | 232 | 267 | assert.deepEqual(entryMetadata, { | 233 | | - resource: loggingWinston.resource_, | 234 | | - labels: {} | | 268 | + resource: loggingWinston.resource_ | | 269 | + }); | | 270 | + assert.deepStrictEqual(data, { | | 271 | + message: MESSAGE, | | 272 | + metadata: {} | 235 | 273 | }); | 236 | | - assert.deepStrictEqual(data, { message: MESSAGE }); | 237 | 274 | done(); | 238 | 275 | }; | 239 | 276 |
| |
|