-
Notifications
You must be signed in to change notification settings - Fork 854
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: logging [BREAKING CHANGE] (#749)
* 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
Showing
21 changed files
with
211 additions
and
683 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
"nextjs", | ||
"Nodejitsu", | ||
"ntlm", | ||
"pino", | ||
"proxied", | ||
"proxying", | ||
"rawbody", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.