Skip to content

Commit

Permalink
[PFX-770]: Fix [object Object] logs in Fastify api apps (#940)
Browse files Browse the repository at this point in the history
* disable req logging for fastify apps

* update fastify tests with disableRequestLogging

* add disableRequestLogging as a fastify config option

* update type description
  • Loading branch information
bbetts-godaddy authored Oct 9, 2024
1 parent 3310699 commit dae7433
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/gasket-plugin-fastify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ All the configurations for the plugin are added under `fastify` in the config:
- `compression`: true by default. Can be set to false if applying compression
differently.
- `trustProxy`: Enable trust proxy option, [see Fastify documentation for possible values](https://fastify.dev/docs/latest/Reference/Server/#trustproxy)
- `disableRequestLogging`: Turn off request logging, true by default

#### Example configuration

Expand Down
2 changes: 2 additions & 0 deletions packages/gasket-plugin-fastify/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ declare module '@gasket/core' {
excludedRoutesRegex?: RegExp;
/** Trust proxy configuration */
trustProxy?: FastifyServerOptions['trustProxy'];
/** Fastify request logging per route */
disableRequestLogging?: boolean;
};
/** Middleware configuration */
middleware?: {
Expand Down
4 changes: 2 additions & 2 deletions packages/gasket-plugin-fastify/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const plugin = {
actions: {
getFastifyApp(gasket) {
const { fastify: fastifyConfig = {}, http2, https } = gasket.config;
const { trustProxy = false } = fastifyConfig;
const { trustProxy = false, disableRequestLogging = true } = fastifyConfig;
const fastifyLogger = alignLogger(gasket.logger);

// @ts-ignore
app ??= fastify({ logger: fastifyLogger, trustProxy, https, http2 });
app ??= fastify({ logger: fastifyLogger, trustProxy, https, http2, disableRequestLogging });

return app;
}
Expand Down
42 changes: 38 additions & 4 deletions packages/gasket-plugin-fastify/test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,23 @@ describe('actions', () => {
it('does not enable trust proxy by default', async () => {
const pluginInstance = require('../lib/index');
await pluginInstance.actions.getFastifyApp(gasket);
expect(mockFastify).toHaveBeenCalledWith({ logger: gasket.logger, trustProxy: false });
expect(mockFastify).toHaveBeenCalledWith({
logger: gasket.logger,
trustProxy: false,
disableRequestLogging: true
});
});

it('does enable trust proxy by if set to true', async () => {
gasket.config.fastify = { trustProxy: true };
const pluginInstance = require('../lib/index');
await pluginInstance.actions.getFastifyApp(gasket);

expect(mockFastify).toHaveBeenCalledWith({ logger: gasket.logger, trustProxy: true });
expect(mockFastify).toHaveBeenCalledWith({
logger: gasket.logger,
trustProxy: true,
disableRequestLogging: true
});
});

it('does enable trust proxy by if set to string', async () => {
Expand All @@ -88,15 +96,41 @@ describe('actions', () => {

expect(mockFastify).toHaveBeenCalledWith({
logger: gasket.logger,
trustProxy: '127.0.0.1'
trustProxy: '127.0.0.1',
disableRequestLogging: true
});
});

it('defaults to true for disableRequestLogging', async () => {
const pluginInstance = require('../lib/index');
await pluginInstance.actions.getFastifyApp(gasket);
expect(mockFastify).toHaveBeenCalledWith({
logger: gasket.logger,
trustProxy: false,
disableRequestLogging: true
});
});

it('allows request logging if disableRequestLogging is set to false', async () => {
gasket.config.fastify = { disableRequestLogging: false };
const pluginInstance = require('../lib/index');
await pluginInstance.actions.getFastifyApp(gasket);
expect(mockFastify).toHaveBeenCalledWith({
logger: gasket.logger,
trustProxy: false,
disableRequestLogging: false
});
});

it('adds log plugin as logger to fastify', async function () {
const pluginInstance = require('../lib/index');
await pluginInstance.actions.getFastifyApp(gasket);

expect(mockFastify).toHaveBeenCalledWith({ logger: gasket.logger, trustProxy: false });
expect(mockFastify).toHaveBeenCalledWith({
logger: gasket.logger,
trustProxy: false,
disableRequestLogging: true
});
});
});

Expand Down

0 comments on commit dae7433

Please sign in to comment.