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

feat: add loggers package #1472

Merged
merged 3 commits into from
May 4, 2020
Merged

feat: add loggers package #1472

merged 3 commits into from
May 4, 2020

Conversation

emmenko
Copy link
Member

@emmenko emmenko commented Apr 28, 2020

Partially part of #1444

This PR open sources our internal loggers package, used by our backend services.

@vercel
Copy link

vercel bot commented Apr 28, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/commercetools/merchant-center-application-kit/q9on1kr7d
✅ Preview: https://merchant-center-application-kit-git-nm-feat-loggers.commercetools.now.sh

@emmenko emmenko added the 🚀 Type: New Feature Something new label Apr 28, 2020
Copy link
Contributor

@tdeekens tdeekens left a comment

Choose a reason for hiding this comment

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

Thanks. I really like that we open source more and more of our internal infrastructure code. Code which is duplicated across services and repositories and quite hard to maintain/keep in sync at time. A problem definitely worthwhile solving. I am just wondering if open sourcing it here right now is the right approach given my level of information. I feel like we are rushing this maybe a bit while having fun on late nite PRs 🤓. I would need us to think maybe a bit more about what we open source and why. What API patterns and guidelines we follow for sets of middlewares here. Especially as they should all feel similar to consumers and what would actually help custom app builders and what verified need we solve. All of which I didn’t have time to think about much but would like to discuss first also with you and stakeholders. As such, at least to my current state of mind, this helps us internally more than anybody else. Lets maybe discuss this soon in person.

packages-backend/loggers/src/custom-formats/json.ts Outdated Show resolved Hide resolved

function createErrorReportLogger(options: LoggerOptions = {}) {
// Sentry
if (options.sentry) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm. Personally I find it rather specific that we use Sentry. Do we know if others using this package would too or should we allow for more reporting targets?

Copy link
Member Author

Choose a reason for hiding this comment

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

In my defense, the package says "Opinionated JSON loggers" 😜

@vercel vercel bot temporarily deployed to Preview April 28, 2020 20:11 Inactive
@emmenko emmenko marked this pull request as draft April 28, 2020 20:11
@emmenko
Copy link
Member Author

emmenko commented Apr 28, 2020

I understand your points, let's have a chat then 🙂

import * as env from './env';
import jsonFormatter from './custom-formats/json';

const shouldLog = !env.isTest || process.env.DEBUG === 'true';
Copy link
Contributor

Choose a reason for hiding this comment

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

This we should maybe extract to a { silent } argument to the createApplicationLogger factory. Others may log under test.


import * as Sentry from '@sentry/node';
import * as env from './env';
import redactInsecureRequestHeaders from './utils/redact-insecure-request-headers';
Copy link
Contributor

Choose a reason for hiding this comment

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

I would not open source this. Maybe we can have a mapHeaders or filterHeaders as a config option (name TBD 😄).

Copy link
Member Author

Choose a reason for hiding this comment

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

PS: one of the intents with this loggers package was to provide preconfigured loggers, meaning that for common cases there should be no need to provide extra configuration.

We could maybe have a default list of "insecure" headers, while still allowing to extend that list.

errorMessageBlacklist?: Array<string | RegExp>;
};

function createErrorReportLogger(options: LoggerOptions = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe calling it createSentryErrorMiddleware is more apt.

delete info.level;

// Replace / rename fields
replaceField(info, 'meta.req.query', 'meta.req.queryJsonString');
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be an option omitMetaPaths: String[] I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, probably we need to provide some kind of fields mapping.

{
  rewriteMetaInfoFields: {
    'meta.req.query': 'meta.req.queryJsonString'
  }
}

An alternative might be to allow to pass a custom logform formatter. We can also expose a bunch of default formatter (e.g. json, kibana, etc). The kibana formatter can also be additionally configured with the fields rewriter.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah. I think sane defaults which can be overwritten would be good. Maybe also by just saying: I want a Kibana logger as a type argument you get those defaults.

return req[propName];
};

const createAccessLogger = (options: LoggerOptions = {}) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a middleware I think. So to have it inline with others maybe it's createAccessLoggerMiddleware(options).

const isProd = env === 'production';
const isTest = env === 'test';

export { isDev, isProd, isTest };
Copy link
Contributor

Choose a reason for hiding this comment

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

I would also have this as an arg to the middlewars. envGetters maybe. Not 100% also just making notes for myself.

@vercel vercel bot temporarily deployed to Preview May 4, 2020 16:09 Inactive
@emmenko
Copy link
Member Author

emmenko commented May 4, 2020

I cleaned things up and removed all unnecessary things. I also removed Sentry, as I think we can keep this internal for now.

@tdeekens can you take another look?

@emmenko emmenko marked this pull request as ready for review May 4, 2020 16:10
@emmenko emmenko requested a review from tdeekens May 4, 2020 16:10
Copy link
Contributor

@tdeekens tdeekens left a comment

Choose a reason for hiding this comment

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

Thanks. Looks very nice now 😄 💯


// Re-export winston for convenience
import * as winston from 'winston';
export { winston };
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: doesn't TS allow export * as winston from 'winston'`?

@vercel vercel bot temporarily deployed to Preview May 4, 2020 17:55 Inactive
@vercel vercel bot temporarily deployed to Preview May 4, 2020 18:33 Inactive
@emmenko emmenko merged commit 493af38 into master May 4, 2020
@emmenko emmenko deleted the nm-feat-loggers branch May 4, 2020 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants