Skip to content

feature: request logging middleware for express#182

Merged
ofrobots merged 2 commits into
googleapis:masterfrom
ofrobots:express-middleware
Feb 13, 2019
Merged

feature: request logging middleware for express#182
ofrobots merged 2 commits into
googleapis:masterfrom
ofrobots:express-middleware

Conversation

@ofrobots
Copy link
Copy Markdown
Contributor

Similar to logging-bunyan, this PR implements a request correlating middleware for express. This is WIP, but opening now to get early feedback.

@ghost ghost assigned ofrobots Sep 28, 2018
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Sep 28, 2018
@ofrobots ofrobots added do not merge Indicates a pull request not ready for merge, due to either quality or timing. and removed cla: yes This human has signed the Contributor License Agreement. labels Sep 28, 2018
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Sep 28, 2018
@ofrobots ofrobots force-pushed the express-middleware branch from 60b028b to 59e95b5 Compare October 4, 2018 01:01
@ofrobots
Copy link
Copy Markdown
Contributor Author

ofrobots commented Oct 4, 2018

Tests are expected to fail until a new version of winston@3 is released that includes the fix winstonjs/winston#1483.

@ofrobots
Copy link
Copy Markdown
Contributor Author

I've extracted much of the logic to googleapis/nodejs-logging#248.

@ofrobots ofrobots changed the title [WIP] feature: request logging middleware for express feature: request logging middleware for express Jan 30, 2019
@ofrobots ofrobots removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Jan 30, 2019
@ofrobots
Copy link
Copy Markdown
Contributor Author

New version of winston is out now. This is ready to review.

JustinBeckwith
JustinBeckwith previously approved these changes Feb 1, 2019
Comment thread package.json Outdated
Comment thread src/common.ts
Comment thread src/middleware/make-middleware.ts Outdated
Comment thread src/middleware/make-middleware.ts Outdated
import * as context from '@opencensus/propagation-stackdriver';
import * as http from 'http';

// TODO: move this code to @google-cloud/logging so that it can be shared
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a bug open somewhere to track that?

Comment thread test/middleware/express.ts Outdated
@JustinBeckwith
Copy link
Copy Markdown
Contributor

👋 @ofrobots is this ready to go?

@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Feb 7, 2019
@ofrobots
Copy link
Copy Markdown
Contributor Author

Actually it turns out I still need to do a bit more work on this.

@ofrobots
Copy link
Copy Markdown
Contributor Author

Currently blocked on googleapis/nodejs-logging#376 being released.

@ofrobots ofrobots added the status: blocked Resolving the issue is dependent on other work. label Feb 12, 2019
@yoshi-automation yoshi-automation removed the 🚨 This issue needs some love. label Feb 12, 2019
@ofrobots ofrobots force-pushed the express-middleware branch 2 times, most recently from 8eb5860 to 4990356 Compare February 12, 2019 01:47
@ofrobots ofrobots added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 12, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 12, 2019
@ofrobots ofrobots added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 12, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 12, 2019
@ofrobots ofrobots removed the status: blocked Resolving the issue is dependent on other work. label Feb 12, 2019
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Feb 12, 2019
@ofrobots ofrobots added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 12, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 13, 2019
@ofrobots
Copy link
Copy Markdown
Contributor Author

Okay, this is ready for review for realz this time.

Copy link
Copy Markdown
Contributor

@kjin kjin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM w/ q

Comment thread src/middleware/express.ts
options = Object.assign({}, defaultOptions, options);

const loggingWinstonApp = new LoggingWinston(
{...options, logName: `${options.logName}_${APP_LOG_SUFFIX}`});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this always replace both the user and default option for logName?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The middleware is opinionated. It may need to create two different log streams: one for the app logs and potentially one for the request logs (when not running on GAE/GCF). The app log will always uniformly have the _applog suffix. We want to 1) distinguish the app log from the request log and 2) have uniform naming regardless of the deployment environment.

It is conceivable that we might want to make ${APP_LOG_SUFFIX} a user-configurable option, but I don't see the need for it yet.

@JustinBeckwith JustinBeckwith added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 13, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 13, 2019
@sduskis sduskis added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 13, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 13, 2019
@ofrobots ofrobots added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 13, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 13, 2019
@ofrobots ofrobots merged commit 0e8345b into googleapis:master Feb 13, 2019
@ofrobots ofrobots deleted the express-middleware branch February 13, 2019 19:49
@shaunc869
Copy link
Copy Markdown

Is there any documentation on how to enable this or does it just magically starting working inside express? Thank you for doing this!

@ofrobots
Copy link
Copy Markdown
Contributor Author

Hi @shaunc869 this is not publicly exposed at this point unfortunately 😞. This is currently blocked by issues related to TypeScript and our transparent support for both winston2 and winston3 out of the same module: #278. I would suggest subscribing to that issue.

@shaunc869
Copy link
Copy Markdown

I just saw the merge come through, looks like we're close on this maybe?

@ofrobots
Copy link
Copy Markdown
Contributor Author

@shaunc869 yes, this should get released around the end of the month. If you wanted to kick the tires, you can install the module from the master branch and play around: npm i googleapis/nodejs-logging-winston. Note that you'll be the first 'real' user of this feature, so this may be rough around the edges. If you have any feedback, that would be most welcome.

@shaunc869
Copy link
Copy Markdown

Sounds great I'll do that tonight any special instructions for this to work with express?

@ofrobots
Copy link
Copy Markdown
Contributor Author

The readme on the master branch has some instructions.

@shaunc869
Copy link
Copy Markdown

Dumb question, now my import is failing and I can't see why, can these not be mixed?

import { LoggingWinston } from '@google-cloud/logging-winston';

@DominicKramer
Copy link
Copy Markdown
Contributor

The way you are importing LoggingWinston should work. What specific error are you seeing?

@shaunc869
Copy link
Copy Markdown

shaunc869 commented Apr 18, 2019

Sorry I rebuilt and now I'm down to:

TypeError: LoggingWinston is not a constructor
    at Object.<anonymous> (/Users/hackintosh/PycharmProjects/spm-appengine/lib/util/logger.js:10:24)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/hackintosh/PycharmProjects/spm-appengine/lib/models/appointment_request.js:5:18)
    at Module._compile (module.js:653:30)
Waiting for the debugger to disconnect...

For the code:

import * as LoggingWinston from '@google-cloud/logging-winston';

const loggingWinston = new LoggingWinston({
	projectId: 'highlevel-backend',
	keyFilename: './lib/highlevel-backend.json',
	serviceContext: {
		service: process.env.GAE_SERVICE,
		version: process.env.GAE_VERSION,
	},
});

@DominicKramer
Copy link
Copy Markdown
Contributor

Either

import * as LoggingWinston from '@google-cloud/logging-winston';

const loggingWinston = new LoggingWinston.LoggingWinston({
	projectId: 'highlevel-backend',
	keyFilename: './lib/highlevel-backend.json',
	serviceContext: {
		service: process.env.GAE_SERVICE,
		version: process.env.GAE_VERSION,
	},
});

or

import {LoggingWinston} from '@google-cloud/logging-winston';

const loggingWinston = new LoggingWinston({
	projectId: 'highlevel-backend',
	keyFilename: './lib/highlevel-backend.json',
	serviceContext: {
		service: process.env.GAE_SERVICE,
		version: process.env.GAE_VERSION,
	},
});

should work. Let me know if it doesn't.

@shaunc869
Copy link
Copy Markdown

Awesome first one totally worked, now I threw in a test method:

export async function logTest(req, res) {
	req.log.info('this is an info log message');
	res.status(200).send('Hello');
}

It runs fine, but nothing prints to the console on my localhost, should it?

@ofrobots
Copy link
Copy Markdown
Contributor Author

@shaunc869 Yeah, that is a problem – the express middleware configures only a stackdriver transport for winston. This is a bug.

However, in order to keep the discussion focussed, I am going to open a new bug rather than continuing to work on this closed issue: #303

@yoshi-automation yoshi-automation removed the 🚨 This issue needs some love. label Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants