# @ovotech/axios-logger

> Log responses with sanitization

Latest version **2.0.1** (published 2019-06-06) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @ovotech/axios-logger
pnpm add @ovotech/axios-logger
yarn add @ovotech/axios-logger
bun add @ovotech/axios-logger
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2019-06-06 |
| First published | 2019-04-16 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ivan Kerin |
| Maintainers | adamfielding, agaovo, annedroiid, asalvadore, asummers-ovo, ben.plommer, boost-smile, cb372, clarencedglee, dimitarg, elbird, emilyrigby, ericaovo, felipesales, fgkramer, filose, freddybushboy, grug, hayfield-ovo, hentielouw, hersheyovo, ikerin, iuna4e, joelthornhill, jonevans4, jvmovo, kelveden, kenneth-gray, kevrowe, kieran.allen, kostasovo, lcatallo, marcu.iulian13, mbayoumy, mkohlmyr, mwidurek, nabilboag, nicolasov, odunka, oli-boyle, olmesm, orex-team, orion-migration-team, ovo.cms.devs, ovocms, ovotech-boost, ovotech-identity, ovotech-live, ovotech-payg, ovotech-paym, ovotech-payments, ovotech-qs, ovotech-sa, ovotech-sg, ovotech-sme-team, pdpi_ovo, philip-ovo, potsec, props, radek_tomasek, rosario-ovo, samjacobclift, sarahlikeshiny, shaikhfaisal, sketchingdev, smart-heat-prod, smart-heat-uat, takac, taras_tymovskyi, tech.international, teodora.danciu, tomshawovo, tomverran-ovo, tozzy, viciouskitten, yesdaveovo, yuliya-makarchuk |

## Links

- npm: https://www.npmjs.com/package/@ovotech/axios-logger
- npm.io page: https://npm.io/package/@ovotech/axios-logger

## Recent versions

- 2.0.1 (latest) — 2019-06-06
- 2.0.0 — 2019-05-31
- 1.1.0 — 2019-04-19
- 1.0.0 — 2019-04-16

## README

# Axios Logger Interceptor

Log request and response, redact all possible PII sources by default, but allow to add custom redact paths so more info can be logged.

### Using

```bash
yarn add @ovotech/axios-logger
```

```typescript
import { axiosLogger, redactHeader } from '@ovotech/axios-logger';
import axios from 'axios';

const logger = axiosLogger((level, meta, config) => console.log(level, meta, config.url));

const api = axios.create();
api.interceptors.request.use(logger.request.onFullfilled);
api.interceptors.response.use(logger.response.onFullfilled, logger.response.onRejected);

// ...

api.get('/my/path');

const body = { user: { cards: [{ id: '111' }, { id: '222' }] } };
api.post('/update/path', body, { headers: { [redactHeader]: 'requestBody.user.cards.*.id' } });
```

You have 3 interceptors. `logger.request.onFullfilled`, `logger.response.onFullfilled` and `logger.response.onRejected`.

- `logger.request.onFullfilled` used to setup the initial execution time. If omitted request time would not be logged.
- `logger.response.onFullfilled` logs a successful response
- `logger.response.onRejected` logs an error

Each one can be omitted if you don't want or need that feature.

The log function will receive 3 arguments - level, meta and axios request config. The first one indicates what type of log level to use - "info" for success and "error" for error. The second contains an object of data to log.

```js
{
  uri: '/my/path',
  method: 'get',
  params: { id: '10' }
  requestBody: { id: '10' },
  responseBody: { user: 'Name' },
  status: 200,
  responseTime: 21,
}
```

By default `uri`, `params`, `requestBody` and `responseBody` will be "redacted", since they can contain personally identifiable information. You can control that with the `redactHeader`. Its a comma separated list of dot delimited field paths to be redacted. Can contain wildcard `*` path to target all array items.

For example to redact some fields.

```typescript
api.post('/update/path', body, { headers: { [redactHeader]: 'requestBody.id, responseBody.user' });
```

You can also set redact at the axios instance level for global redaction rules:

```typescript
const api = axios.create({ redact: ['requestBody'] });
```

### Granular logging

You can perform different things on error / success by inspecting the "level" argument, passed to the log function.

```typescript
import { axiosLogger } from '@ovotech/axios-logger';
import axios from 'axios';

const logger = axiosLogger((level, meta) => {
  if (level === 'info') {
    myOwnLogger.info('Successful request', meta);
    graphResponseTimes(meta.responseTime);
  }
  if (level === 'error') {
    myOwnLogger.error('Error request', meta);
  }
});
```

## Running the tests

You can run the tests with:

```bash
yarn test
```

### Coding style (linting, etc) tests

Style is maintained with prettier and tslint

```
yarn lint
```

## Deployment

Deployment is preferment by lerna automatically on merge / push to master, but you'll need to bump the package version numbers yourself. Only updated packages with newer versions will be pushed to the npm registry.

## Contributing

Have a bug? File an issue with a simple example that reproduces this so we can take a look & confirm.

Want to make a change? Submit a PR, explain why it's useful, and make sure you've updated the docs (this file) and the tests (see [test folder](test)).

## License

This project is licensed under Apache 2 - see the [LICENSE](LICENSE) file for details

---
_Source: https://npm.io/package/@ovotech/axios-logger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
