# axios-logger

> Beautify Axios Logging Messages

Latest version **2.8.1** (published 2024-05-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install axios-logger
pnpm add axios-logger
yarn add axios-logger
bun add axios-logger
```

## Health

**Score 40/100 (D)** — status: abandoned.

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

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.8.1 |
| Published | 2024-05-29 |
| First published | 2018-06-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 54.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 183 |
| Author | Haegul Pyun |
| Maintainers | dev-momo |
| Keywords | javascript, nodejs, network, axios, http, log, logger, beautify, library, server |

## Links

- npm: https://www.npmjs.com/package/axios-logger
- Repository: https://github.com/hg-pyun/axios-logger
- Homepage: https://github.com/hg-pyun/axios-logger#readme
- Issues: https://github.com/hg-pyun/axios-logger/issues
- npm.io page: https://npm.io/package/axios-logger

## Dependencies (2)

- [chalk](https://npm.io/package/chalk.md) ^4.1.0
- [dateformat](https://npm.io/package/dateformat.md) ^3.0.3

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 2.8.1 (latest) — 2024-05-29
- 2.8.0 — 2024-03-14
- 2.7.1 — 2023-12-09
- 2.7.0 — 2023-08-02
- 2.6.2 — 2022-12-15
- 2.6.1 — 2022-05-07
- 2.6.0 — 2021-11-07
- 2.5.0 — 2020-12-23
- 2.4.0 — 2020-08-24
- 2.3.1 — 2020-01-30
- 2.3.0 — 2019-12-31
- 2.2.1 — 2019-10-05
- 2.2.0 — 2019-09-30
- 2.1.0 — 2019-07-14
- 2.0.1 — 2019-07-05
- … 7 more at https://npm.io/package/axios-logger/versions

## README

# axios-logger

[![npm](https://img.shields.io/npm/v/axios-logger.svg)](https://www.npmjs.com/package/axios-logger)
[![npm](https://img.shields.io/npm/dm/axios-logger.svg)](https://www.npmjs.com/package/axios-logger)
[![GitHub license](https://img.shields.io/github/license/hg-pyun/axios-logger.svg)](https://github.com/hg-pyun/axios-logger/blob/master/LICENSE)
[![CircleCI](https://circleci.com/gh/hg-pyun/axios-logger/tree/master.svg?style=svg)](https://circleci.com/gh/hg-pyun/axios-logger/tree/master)

> Beautify Axios Logging Messages.

![logo](https://user-images.githubusercontent.com/10627668/71610488-dbdd5c80-2bd4-11ea-8a8a-15c0328bba0b.png)

When you send a request in nodejs, you need to show the log to the console.
This library display the necessary information while communicating with the server.

Basically This package is working as [Axios's interceptors](https://github.com/axios/axios#interceptors).

![sample](https://user-images.githubusercontent.com/10627668/41816761-1700b662-77c8-11e8-80d4-7d223169364a.png)

## Install

```
$ npm install axios-logger --save-dev
```

## How to use

You can use various loggers through the `axios`'s interceptor API.

### Logging Request

```javascript
import axios from 'axios';
import * as AxiosLogger from 'axios-logger';

const instance = axios.create();
instance.interceptors.request.use(AxiosLogger.requestLogger);
```

If you want to use your own interceptor, you can compose(mixin) with `requestLogger`.

```javascript
instance.interceptors.request.use((request) => {
    // write down your request intercept.
    return AxiosLogger.requestLogger(request);
});
```

### Logging Response

```javascript
import axios from 'axios';
import * as AxiosLogger from 'axios-logger';

const instance = axios.create();
instance.interceptors.response.use(AxiosLogger.responseLogger);
```

Also if you want to use your own interceptor, you can compose(mixin) with `responseLogger`.

```javascript
instance.interceptors.response.use((response) => {
    // write down your response intercept.
    return AxiosLogger.responseLogger(response);
});
```

### Error

You can inject `errorLogger` right after `requestLogger` or `responseLogger`.

```javascript
import axios from 'axios';
import * as AxiosLogger from 'axios-logger';

const instance = axios.create();
instance.interceptors.request.use(AxiosLogger.requestLogger, AxiosLogger.errorLogger);
instance.interceptors.response.use(AxiosLogger.responseLogger, AxiosLogger.errorLogger);
```

Also if you want to use your own interceptor, you can compose(mixin) with `errorLogger`.

```javascript
instance.interceptors.response.use(AxiosLogger.responseLogger, (err) => {
    // write down your error intercept.
    return AxiosLogger.errorLogger(err);
});
```

## Configuration Settings

You can adjust several features as desired through configuration file.
If you want to set config globally, using `setGlobalConfig` method.

```javascript
setGlobalConfig({
    prefixText: 'your prefix',
    dateFormat: 'HH:MM:ss',
    status: false,
    headers: true,
    logger: someLogger.info.bind(this),
});
```

Or it can also be passed on as a second argument and applied locally. In this case, any property explicitly defined will
override the global configuration.

```javascript
instance.interceptors.request.use((request) => {
    // write down your request intercept.
    return AxiosLogger.requestLogger(request, {
        prefixText: 'your prefix',
        dateFormat: 'HH:MM:ss',
        status: false,
        headers: true,
        logger: someLogger.error.bind(this),
    });
});
```

#### Enable config list

| Property     | Type                                                                | Default       | Description                                                                                                                                    |
| ------------ | ------------------------------------------------------------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `method`     | boolean                                                             | `true`        | Whether to include HTTP method or not.                                                                                                         |
| `url`        | boolean                                                             | `true`        | Whether to include the URL or not.                                                                                                             |
| `params`     | boolean                                                             | `false`       | Whether to include the URL params or not.                                                                                                      |
| `data`       | boolean                                                             | `true`        | Whether to include request/response data or not.                                                                                               |
| `status`     | boolean                                                             | `true`        | Whether to include response statuses or not.                                                                                                   |
| `statusText` | boolean                                                             | `true`        | Whether to include response status text or not.                                                                                                |
| `headers`    | boolean                                                             | `false`       | Whether to include HTTP headers or not.                                                                                                        |
| `prefixText` | string \| `false`                                                   | `'Axios'`     | `false` => no prefix, otherwise, customize the prefix wanted.                                                                                  |
| `dateFormat` | [dateformat](https://github.com/felixge/node-dateformat) \| `false` | `false`       | `false` => no timestamp, otherwise, customize its format                                                                                       |
| `logger`     | function<string, any>                                               | `console.log` | Allows users to customize the logger function to be used. e.g. Winston's `logger.info` could be leveraged, like this: `logger.info.bind(this)` |

## CONTRIBUTE

I always welcome Feedback and Pull Request :)

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