Skip to content

Commit 088a6d7

Browse files
authored
Merge pull request #106 from jaredwray/feat-ability-to-disable-logging
feat: ability to disable logging
2 parents 88b662a + 89e5473 commit 088a6d7

5 files changed

Lines changed: 121 additions & 15 deletions

File tree

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ A simple HTTP server that can be used to mock HTTP responses for testing purpose
2121
* Auto detect the port that is not in use
2222
* Maintained and updated regularly!
2323

24+
# Table of Contents
25+
- [Deploy via Docker](#deploy-via-docker)
26+
- [Deploy via Docker Compose](#deploy-via-docker-compose)
27+
- [Deploy via NodeJS](#deploy-via-nodejs)
28+
- [Response Injection (Tap Feature)](#response-injection-tap-feature)
29+
- [Rate Limiting](#rate-limiting)
30+
- [Logging](#logging)
31+
- [API Reference](#api-reference)
32+
- [About mockhttp.org](#about-mockhttporg)
33+
- [Contributing](#contributing)
34+
- [License](#license)
35+
2436
# Deploy via Docker
2537
```bash
2638
docker run -d -p 3000:3000 jaredwray/mockhttp
@@ -405,6 +417,26 @@ await mock.start(); // Restarts with new settings
405417

406418
For the complete list of options, see the [@fastify/rate-limit documentation](https://github.com/fastify/fastify-rate-limit#options).
407419

420+
# Logging
421+
422+
MockHttp uses [Pino](https://github.com/pinojs/pino) for logging via Fastify's built-in logger. Logging is **enabled by default** but can be disabled when needed.
423+
424+
## Disabling Logging
425+
426+
```javascript
427+
import { MockHttp } from '@jaredwray/mockhttp';
428+
429+
const mock = new MockHttp({ logging: false });
430+
await mock.start();
431+
// Server runs silently without any log output
432+
```
433+
434+
You can also disable logging via the `LOGGING` environment variable:
435+
436+
```bash
437+
LOGGING=false node your-app.js
438+
```
439+
408440
# API Reference
409441

410442
## MockHttp Class
@@ -423,6 +455,7 @@ new MockHttp(options?)
423455
- `helmet?`: boolean - Use Helmet for security headers (default: true)
424456
- `apiDocs?`: boolean - Enable Swagger API documentation (default: true)
425457
- `rateLimit?`: RateLimitPluginOptions - Configure rate limiting (default: 1000 req/min, localhost excluded)
458+
- `logging?`: boolean - Enable logging (default: true)
426459
- `httpBin?`: HttpBinOptions - Configure which httpbin routes to enable
427460
- `httpMethods?`: boolean - Enable HTTP method routes (default: true)
428461
- `redirects?`: boolean - Enable redirect routes (default: true)
@@ -443,6 +476,7 @@ new MockHttp(options?)
443476
- `autoDetectPort`: boolean - Get/set auto-detect port behavior
444477
- `helmet`: boolean - Get/set Helmet security headers
445478
- `apiDocs`: boolean - Get/set API documentation
479+
- `logging`: boolean - Get/set logging enabled state
446480
- `rateLimit`: RateLimitPluginOptions | undefined - Get/set rate limiting options
447481
- `httpBin`: HttpBinOptions - Get/set httpbin route options
448482
- `server`: FastifyInstance - Get/set the Fastify server instance

src/fastify-config.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
export const fastifyConfig = {
2-
logger: {
3-
transport: {
4-
target: "pino-pretty",
5-
options: {
6-
colorize: true,
7-
translateTime: true,
8-
ignore: "pid,hostname",
9-
singleLine: true,
10-
},
11-
},
12-
},
13-
};
1+
export function getFastifyConfig(logging = true) {
2+
return {
3+
logger: logging
4+
? {
5+
transport: {
6+
target: "pino-pretty",
7+
options: {
8+
colorize: true,
9+
translateTime: true,
10+
ignore: "pid,hostname",
11+
singleLine: true,
12+
},
13+
},
14+
}
15+
: false,
16+
};
17+
}

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export const start = async () => {
1515
mockHttp.host = process.env.HOST;
1616
}
1717

18+
/* v8 ignore next -- @preserve */
19+
if (process.env.LOGGING === "false") {
20+
mockHttp.logging = false;
21+
}
22+
1823
await mockHttp.start();
1924

2025
return mockHttp;

src/mock-http.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { fastifySwagger } from "@fastify/swagger";
99
import { detect } from "detect-port";
1010
import Fastify, { type FastifyInstance } from "fastify";
1111
import { Hookified, type HookifiedOptions } from "hookified";
12-
import { fastifyConfig } from "./fastify-config.js";
12+
import { getFastifyConfig } from "./fastify-config.js";
1313
import { anythingRoute } from "./routes/anything/index.js";
1414
import {
1515
basicAuthRoute,
@@ -100,6 +100,10 @@ export type MockHttpOptions = {
100100
* Hookified options.
101101
*/
102102
hookOptions?: HookifiedOptions;
103+
/**
104+
* Whether to enable logging. Defaults to true.
105+
*/
106+
logging?: boolean;
103107
};
104108

105109
export class MockHttp extends Hookified {
@@ -108,6 +112,7 @@ export class MockHttp extends Hookified {
108112
private _autoDetectPort = true;
109113
private _helmet = true;
110114
private _apiDocs = true;
115+
private _logging = true;
111116
private _httpBin: HttpBinOptions = {
112117
httpMethods: true,
113118
redirects: true,
@@ -160,6 +165,10 @@ export class MockHttp extends Hookified {
160165
this._rateLimit = options.rateLimit as RateLimitPluginOptions;
161166
}
162167
}
168+
169+
if (options?.logging !== undefined) {
170+
this._logging = options.logging;
171+
}
163172
}
164173

165174
/**
@@ -244,6 +253,22 @@ export class MockHttp extends Hookified {
244253
this._apiDocs = apiDocs;
245254
}
246255

256+
/**
257+
* Whether to enable logging. Defaults to true.
258+
* @default true
259+
*/
260+
public get logging(): boolean {
261+
return this._logging;
262+
}
263+
264+
/**
265+
* Whether to enable logging. Defaults to true.
266+
* @default true
267+
*/
268+
public set logging(logging: boolean) {
269+
this._logging = logging;
270+
}
271+
247272
/**
248273
* HTTP Bin options. Defaults to all enabled.
249274
*/
@@ -316,7 +341,7 @@ export class MockHttp extends Hookified {
316341
await this._server.close();
317342
}
318343

319-
this._server = Fastify(fastifyConfig);
344+
this._server = Fastify(getFastifyConfig(this._logging));
320345

321346
// Register injection hook to intercept requests
322347
this._server.addHook("onRequest", async (request, reply) => {

test/mock-http.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,44 @@ describe("MockHttp", () => {
434434
});
435435
});
436436

437+
describe("logging", () => {
438+
test("should be enabled by default", () => {
439+
const mock = new MockHttp();
440+
expect(mock.logging).toBe(true);
441+
});
442+
443+
test("should be able to disable logging via options", () => {
444+
const mock = new MockHttp({ logging: false });
445+
expect(mock.logging).toBe(false);
446+
});
447+
448+
test("should be able to enable logging via options", () => {
449+
const mock = new MockHttp({ logging: true });
450+
expect(mock.logging).toBe(true);
451+
});
452+
453+
test("should be able to set logging via setter", () => {
454+
const mock = new MockHttp();
455+
expect(mock.logging).toBe(true);
456+
457+
mock.logging = false;
458+
expect(mock.logging).toBe(false);
459+
460+
mock.logging = true;
461+
expect(mock.logging).toBe(true);
462+
});
463+
464+
test("should start server with logging disabled", async () => {
465+
const mock = new MockHttp({ logging: false });
466+
await mock.start();
467+
468+
// Server should start successfully with logging disabled
469+
expect(mock.server).toBeDefined();
470+
471+
await mock.close();
472+
});
473+
});
474+
437475
describe("rate limiting", () => {
438476
test("should be enabled by default with 1000 requests per minute and localhost excluded", () => {
439477
const mock = new MockHttp();

0 commit comments

Comments
 (0)