Skip to content

Commit

Permalink
refactor: logging [BREAKING CHANGE] (#749)
Browse files Browse the repository at this point in the history
* feat(option): logger

* refactor(router, path-rewriter): log with debug [BREAKING CHANGE]

* refactor(logger): log request and response with logger plugin

* refactor(logger): replace logLevel and logProvider with logger option [BREAKING CHANGE]
  • Loading branch information
chimurai committed Apr 22, 2022
1 parent 8c7ef76 commit c88d42d
Show file tree
Hide file tree
Showing 21 changed files with 211 additions and 683 deletions.
37 changes: 7 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
- [`pathRewrite` (object/function)](#pathrewrite-objectfunction)
- [`router` (object/function)](#router-objectfunction)
- [`plugins` (Array)](#plugins-array)
- [`logLevel` (string)](#loglevel-string)
- [`logProvider` (function)](#logprovider-function)
- [`logger` (Object)](#logger-object)
- [`http-proxy` events](#http-proxy-events)
- [`http-proxy` options](#http-proxy-options)
- [WebSocket](#websocket)
Expand Down Expand Up @@ -287,38 +286,16 @@ const config = {
};
```
### `logLevel` (string)
### `logger` (Object)
Default: `'info'`
Configure a logger to output information from http-proxy-middleware: ie. `console`, `winston`, `pino`, `bunyan`, `log4js`, etc...
Values: ['debug', 'info', 'warn', 'error', 'silent'].
### `logProvider` (function)
Modify or replace log provider. Default: `console`.
```javascript
// simple replace
function logProvider(provider) {
// replace the default console log provider.
return require('winston');
}
```
See also logger recipes ([recipes/logger.md](https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/logger.md)) for more details.
```javascript
// verbose replacement
function logProvider(provider) {
const logger = new (require('winston').Logger)();

const myCustomProvider = {
log: logger.log,
debug: logger.debug,
info: logger.info,
warn: logger.warn,
error: logger.error,
};
return myCustomProvider;
}
createProxyMiddleware({
logger: console,
});
```
## `http-proxy` events
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"nextjs",
"Nodejitsu",
"ntlm",
"pino",
"proxied",
"proxying",
"rawbody",
Expand Down
2 changes: 1 addition & 1 deletion examples/browser-sync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const jsonPlaceholderProxy = createProxyMiddleware({
target: 'http://jsonplaceholder.typicode.com',
pathFilter: '/users',
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
logLevel: 'debug',
logger: console,
});

/**
Expand Down
1 change: 1 addition & 0 deletions examples/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const jsonPlaceholderProxy = createProxyMiddleware({
target: 'http://jsonplaceholder.typicode.com/users',
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
logLevel: 'debug',
logger: console,
});

const app = express();
Expand Down
2 changes: 1 addition & 1 deletion examples/response-interceptor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const jsonPlaceholderProxy = createProxyMiddleware({
return JSON.stringify(favoriteFoods);
}),
},
logLevel: 'debug',
logger: console,
});

const app = express();
Expand Down
2 changes: 1 addition & 1 deletion examples/websocket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const wsProxy = createProxyMiddleware({
// },
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
ws: true, // enable websocket proxy
logLevel: 'debug',
logger: console,
});

const app = express();
Expand Down
42 changes: 3 additions & 39 deletions recipes/logLevel.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
# Log Level
# [BREAKING CHANGE]

Control the amount of logging of http-proxy-middleware.
This functionality is removed in v3.

Possible values:

- `debug`
- `info`
- `warn` (default)
- `error`
- `silent`

## Level: debug

Log everything.

```javascript
const { createProxyMiddleware } = require('http-proxy-middleware');

const options = {
target: 'http://localhost:3000',
logLevel: 'debug',
};

const apiProxy = createProxyMiddleware(options);
```

## Level: silent

Suppress all logging.

```javascript
const { createProxyMiddleware } = require('http-proxy-middleware');

const options = {
target: 'http://localhost:3000',
logLevel: 'silent',
};

const apiProxy = createProxyMiddleware(options);
```
See [logger.md](logger.md) for logging in v3.
77 changes: 3 additions & 74 deletions recipes/logProvider.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,5 @@
# Log Provider
# [BREAKING CHANGE]

Configure your own logger with the `logProvider` option.
This functionality is removed in v3.

In this example [winston](https://www.npmjs.com/package/winston) is configured to do the actual logging.

```javascript
const winston = require('winston');
const { createProxyMiddleware } = require('http-proxy-middleware');

const options = {
target: 'http://localhost:3000',
logProvider: function (provider) {
return winston;
},
};

const apiProxy = createProxyMiddleware(options);
```

## Winston

Configure your own logger with the `logProvider` option.

In this example [winston](https://www.npmjs.com/package/winston) is configured to do the actual logging. Map the logging api if needed.

```javascript
const winston = require('winston');
const { createProxyMiddleware } = require('http-proxy-middleware');

const logProvider = function (provider) {
return {
log: winston.log,
debug: winston.debug,
info: winston.info,
warn: winston.warn,
error: winston.error,
};
};

const options = {
target: 'http://localhost:3000',
logProvider: logProvider,
};

const apiProxy = createProxyMiddleware(options);
```

# Winston Multi Transport

Configure your own logger with the `logProvider` option.

In this example [winston](https://www.npmjs.com/package/winston) is configured to do the actual logging.

```javascript
const winston = require('winston');
const { createProxyMiddleware } = require('http-proxy-middleware');

const logProvider = function (provider) {
const logger = new winston.Logger({
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'some-file.log' }),
],
});

return logger;
};

const options = {
target: 'http://localhost:3000',
logProvider: logProvider,
};

const apiProxy = createProxyMiddleware(options);
```
See [logger.md](logger.md) for logging in v3.
87 changes: 87 additions & 0 deletions recipes/logger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Logger

Configure a logger to output information from http-proxy-middleware: ie. `console`, `winston`, `pino`, `bunyan`, `log4js`, etc...

## `console`

```javascript
const { createProxyMiddleware } = require('http-proxy-middleware');

const proxy = createProxyMiddleware({
target: 'http://localhost:3000',
logger: console,
});
```

## `winston`

<https://github.com/winstonjs/winston> ![GitHub Repo stars](https://img.shields.io/github/stars/winstonjs/winston?style=social) ![winston downloads](https://img.shields.io/npm/dm/winston)

```javascript
const { createProxyMiddleware } = require('http-proxy-middleware');
const winston = require('winston');
const { format, transports } = require('winston');

// Enable interpolation in log messages
// https://github.com/winstonjs/winston#string-interpolation
const logger = winston.createLogger({
format: format.combine(format.splat(), format.simple()),
transports: [new transports.Console()],
});

const proxy = createProxyMiddleware({
target: 'http://localhost:3000',
logger,
});
```

## `pino`

<https://github.com/pinojs/pino> ![GitHub Repo stars](https://img.shields.io/github/stars/pinojs/pino?style=social) ![winston downloads](https://img.shields.io/npm/dm/pino)

```javascript
const { createProxyMiddleware } = require('http-proxy-middleware');
const pino = require('pino');

const logger = pino();

const proxy = createProxyMiddleware({
target: 'http://localhost:3000',
logger,
});
```

## `log4js`

<https://github.com/log4js-node/log4js-node> ![GitHub Repo stars](https://img.shields.io/github/stars/log4js-node/log4js-node?style=social) ![winston downloads](https://img.shields.io/npm/dm/log4js)

```javascript
const { createProxyMiddleware } = require('http-proxy-middleware');
const log4js = require('log4js');

const logger = log4js.getLogger();
logger.level = 'debug';

const proxy = createProxyMiddleware({
target: 'http://localhost:3000',
logger,
});
```

## `bunyan`

<https://github.com/trentm/node-bunyan> ![GitHub Repo stars](https://img.shields.io/github/stars/trentm/node-bunyan?style=social) ![winston downloads](https://img.shields.io/npm/dm/bunyan)

```javascript
const { createProxyMiddleware } = require('http-proxy-middleware');
const bunyan = require('bunyan');

const logger = bunyan.createLogger({
name: 'my-app',
});

const proxy = createProxyMiddleware({
target: 'http://localhost:3000',
logger,
});
```
15 changes: 0 additions & 15 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
import { ERRORS } from './errors';
import { getInstance } from './logger';
import { Options } from './types';

const logger = getInstance();

export function verifyConfig(options: Options): void {
configureLogger(options);

if (!options.target && !options.router) {
throw new Error(ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING);
}
}

function configureLogger(options: Options): void {
if (options.logLevel) {
logger.setLevel(options.logLevel);
}

if (options.logProvider) {
logger.setProvider(options.logProvider);
}
}
Loading

0 comments on commit c88d42d

Please sign in to comment.