Skip to content

Commit 1a0b73a

Browse files
ofrobotsstephenplusplus
authored andcommitted
logging-bunyan: HttpRequest support (#2325)
1 parent 77fd27e commit 1a0b73a

4 files changed

Lines changed: 57 additions & 3 deletions

File tree

packages/logging-bunyan/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,29 @@ var loggingBunyan = require('@google-cloud/logging-bunyan')({
6666
// ...you're good to go!
6767
```
6868

69+
## Formatting Request Logs
70+
71+
To format your request logs you can provide a `httpRequest` property on the bunyan metadata you provide along with the log message. We will treat this as the [`HttpRequest`][http-request-message] message and Stackdriver logging will show this as a request log. Example:
72+
73+
![Request Log Example](/doc/images/request-log.png)
74+
75+
```js
76+
logger.info({
77+
httpRequest: {
78+
status: res.statusCode,
79+
requestUrl: req.url,
80+
requestMethod: req.method,
81+
remoteIp: req.connection.remoteAddress,
82+
// etc.
83+
}
84+
}, req.path);
85+
```
86+
87+
The `httpRequest` proprety must be a properly formatted [`HttpRequest`][http-request-message] message.
88+
89+
6990
[bunyan]: https://github.com/trentm/node-bunyan
7091
[@google-cloud/logging]: https://www.npmjs.com/package/@google-cloud/logging
7192
[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using
93+
[http-request-message]: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#HttpRequest
7294
[dev-console]: https://console.developers.google.com/project
7.08 KB
Loading

packages/logging-bunyan/src/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@ LoggingBunyan.prototype.formatEntry_ = function(record) {
141141
);
142142
}
143143

144+
record = extend({}, record);
145+
144146
// Stackdriver Log Viewer picks up the summary line from the 'message' field
145147
// of the payload. Unless the user has provided a 'message' property also,
146148
// move the 'msg' to 'message'.
147149
if (!record.message) {
148-
// Clone the object before modifying it.
149-
record = extend({}, record);
150-
151150
// If this is an error, report the full stack trace. This allows Stackdriver
152151
// Error Reporting to pick up errors automatically (for severity 'error' or
153152
// higher). In this case we leave the 'msg' property intact.
@@ -171,6 +170,17 @@ LoggingBunyan.prototype.formatEntry_ = function(record) {
171170
severity: BUNYAN_TO_STACKDRIVER[record.level]
172171
};
173172

173+
// If the record contains a httpRequest property, provide it on the entry
174+
// metadata. This allows Stackdriver to use request log formatting.
175+
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#HttpRequest
176+
// Note that the httpRequest field must properly validate as a HttpRequest
177+
// proto message, or the log entry would be rejected by the API. We do no
178+
// validation here.
179+
if (record.httpRequest) {
180+
entryMetadata.httpRequest = record.httpRequest;
181+
delete record.httpRequest;
182+
}
183+
174184
return this.log_.entry(entryMetadata, record);
175185
};
176186

packages/logging-bunyan/test/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@ describe('logging-bunyan', function() {
201201

202202
loggingBunyan.formatEntry_(record);
203203
});
204+
205+
it('should promote the httpRequest property to metadata', function(done) {
206+
var HTTP_REQUEST = {
207+
statusCode: 418
208+
};
209+
var recordWithRequest = extend({
210+
httpRequest: HTTP_REQUEST,
211+
}, RECORD);
212+
213+
loggingBunyan.log_.entry = function(entryMetadata, record) {
214+
assert.deepStrictEqual(entryMetadata, {
215+
resource: loggingBunyan.resource_,
216+
timestamp: RECORD.time,
217+
severity: LoggingBunyan.BUNYAN_TO_STACKDRIVER[RECORD.level],
218+
httpRequest: HTTP_REQUEST
219+
});
220+
assert.deepStrictEqual(record, RECORD);
221+
done();
222+
};
223+
224+
loggingBunyan.formatEntry_(recordWithRequest);
225+
});
204226
});
205227

206228
describe('_write', function() {

0 commit comments

Comments
 (0)