Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(middleware): need message in request log message #349

Merged
merged 4 commits into from
Jun 18, 2019
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ app.get('/', (req, res) => {
// `req.log` can be used as a winston style log method. All logs generated
// using `req.log` use the current request context. That is, all logs
// corresponding to a specific request will be bundled in the Stackdriver
// UI.
// UI. The log method will accept RFC5424 (syslog) log levels (the same
// levels provided by winston.config.syslog.levels).
req.log.info('this is an info log message');
res.send('hello world');
});
Expand Down
8 changes: 6 additions & 2 deletions src/middleware/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function middleware(options?: MiddlewareOptions) {
const defaultOptions = {
logName: 'winston_log',
level: 'info',
levels: winston.config.npm.levels,
levels: winston.config.syslog.levels,
};
options = Object.assign({}, defaultOptions, options);

Expand Down Expand Up @@ -71,7 +71,11 @@ export async function middleware(options?: MiddlewareOptions) {
transports: [loggingWinstonReq],
});
emitRequestLog = (httpRequest: HttpRequest, trace: string) => {
requestLogger.info({[LOGGING_TRACE_KEY]: trace, httpRequest});
requestLogger.info({
[LOGGING_TRACE_KEY]: trace,
httpRequest,
message: httpRequest.requestUrl || 'http request',
});
};
}

Expand Down
27 changes: 20 additions & 7 deletions system-test/test-middleware-express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,26 @@ describe(__filename, () => {

await delay(WRITE_CONSISTENCY_DELAY_MS);

const log = logging.log(`${LOG_NAME}_applog`);
const entries = (await log.getEntries({pageSize: 1}))[0];
assert.strictEqual(entries.length, 1);
const entry = entries[0];
assert.strictEqual(LOG_MESSAGE, entry.data.message);
assert(entry.metadata.trace, 'should have a trace property');
assert(entry.metadata.trace.match(/projects\/.*\/traces\/.*/));
const appLog = logging.log(`${LOG_NAME}_applog`);
const appLogEntries = (await appLog.getEntries({pageSize: 1}))[0];
assert.strictEqual(appLogEntries.length, 1);
const [appLogEntry] = appLogEntries;
assert.strictEqual(LOG_MESSAGE, appLogEntry.data.message);
assert(appLogEntry.metadata.trace, 'should have a trace property');
assert(appLogEntry.metadata.trace.match(/projects\/.*\/traces\/.*/));
assert.strictEqual(appLogEntry.metadata.severity, 'INFO');

const requestLog = logging.log(LOG_NAME);
const requestLogEntries = (await requestLog.getEntries({
pageSize: 1,
}))[0];
assert.strictEqual(requestLogEntries.length, 1);
const [requestLogEntry] = requestLogEntries;
assert.strictEqual(
requestLogEntry.metadata.trace,
appLogEntry.metadata.trace
);

done();
};

Expand Down