kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
892 stars 235 forks source link

How to disable console logging entirely (express-openapi)? #685

Open TitaniteSlab opened 4 years ago

TitaniteSlab commented 4 years ago

I see #61 but I still don't understand how.

Specifically, when Content-Type is provided but invalid, I'm getting a stack trace in my server console:

failed to parse content-type foo TypeError: invalid media type
    at Object.parse (/foo/node_modules/content-type/index.js:126:11)
    at getSchemaForMediaType (/foo/node_modules/openapi-request-validator/index.ts:385:37)
    at OpenAPIRequestValidator.validateRequest (/foo/node_modules/openapi-request-validator/index.ts:257:30)
    at requestValidatorMiddleware (/foo/node_modules/express-openapi/index.ts:132:48)
    ... more lines

How do I get rid of this? I want to use a noop logger.

TitaniteSlab commented 4 years ago
import { initialize } from 'express-openapi';
initialize({
  logger: {
    error: () => {},
    warn: () => {},
    info: () => {},
    debug: () => {}
  },
  ...
});

Seems to work.

jsdevel commented 4 years ago

it'd be nice to have logger: null support though to just disable it entirely

jsdevel commented 4 years ago

or logger: false

jsau- commented 2 years ago

Could use dummyLogger from ts-log. Already used in openapi-request-validator as a default, and it's installed in openapi-framework.

If null were to be supported at library-level to disable logging entirely it'd probably easiest be done as:

if (args.logger) {
  this.logger = args.logger;
} else if (args.logger !== null) {
  this.logger = new ConsoleDebugAdapterLogger();
} else {
  this.logger = dummyLogger;
}

Can't really see any meaningful way of checking this in the test suite though beyond adding cases that instantiate OpenAPIFramework with null and undefined values for logger to assert it doesn't throw though since the logger member is private and we wouldn't be able to check what it's an instanceof / if it's exactly the dummyLogger.

@jsdevel Can you let me know if you're happy with this solution as-is or if you'd want any extra changes made? e.g. Adding a public getter for the logger to check its type in tests, etc. I'll be happy to put a PR in for this when I get some time this week.