• Stars
    star
    146
  • Rank 252,769 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Beautify Axios Logging Messages

axios-logger

npm npm GitHub license CircleCI

Beautify Axios Logging Messages.

logo

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.

sample

Install

$ npm install axios-logger --save-dev

How to use

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

Logging Request

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.

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

Logging Response

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.

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

Error

You can inject errorLogger right after requestLogger or responseLogger.

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.

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.

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.

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 | 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 :)

More Repositories

1

minipack-kr

๐Ÿ“ฆ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.
JavaScript
104
star
2

performance-guide

Front-End performance guides
66
star
3

iterize

Use JavaScript Iterator, Easily
TypeScript
63
star
4

step-by-step-webpack-config

webpack ์„ค์ •ํŒŒ์ผ์„ ํ•œ๋•€ํ•œ๋•€ ๋งŒ๋“ค์–ด ๋ณด์ž
JavaScript
63
star
5

articles

The easy-to-understand articles
55
star
6

node-image-filter

Simple Image Filter for Node.js
JavaScript
35
star
7

study-example

examples for tistory
20
star
8

Momo

A Cute Kitty LINE BOT
JavaScript
16
star
9

awesome-snippets-js

code snippets for javascript
15
star
10

sigongjoa

์‹œ๊ณต์˜ ํญํ’์€ ์ •๋ง ์ตœ๊ณ ์•ผ!
JavaScript
14
star
11

plate-cli

Javascript Boilerplate Generator using CLI
JavaScript
9
star
12

use-infinite-scroll

JavaScript
8
star
13

hello-fp

Study for Functional Programming
JavaScript
7
star
14

prevent-bounce

simple prevent iOS dismiss event for web application
JavaScript
6
star
15

algorithm

Algorithm Problem Solutions
Python
4
star
16

t-throttle

Tiny throttle
JavaScript
3
star
17

clip

Install Node Modules, Rapidly
TypeScript
3
star
18

hg-pyun.github.io

My Development Log
JavaScript
3
star
19

hello-nestjs

Study for nestjs
TypeScript
3
star
20

react-ssr-v16

React Server-Side Rendering for v16
JavaScript
3
star
21

react-ssr-boilerplate

react server side rendering boilerplate for plate-cli
JavaScript
3
star
22

t-debounce

Tiny debounce
JavaScript
2
star
23

random-bot

LINE Random Bot
JavaScript
2
star
24

studybook

JavaScript
2
star
25

havto

I have to do it
JavaScript
2
star
26

pawstep

Pet blood glucose meter
TypeScript
2
star
27

everywhere-ocr

Can use OCR everywhere
2
star
28

hello-haskell

Haskell Study Playground
Haskell
2
star
29

hg-pyun

2
star
30

tour-into-the-picture

tour into the picture study
C++
2
star
31

vue-vuex-boilerplate

vuejs boilerplate for plate-cli
JavaScript
2
star
32

media-graduate-project

๋ฏธ๋””์–ด ์กธ์—… ํ”„๋กœ์ ํŠธ
JavaScript
1
star
33

escape-str

escape string module
JavaScript
1
star
34

react-boilerplate

react boilerplate for plate-cli
JavaScript
1
star
35

hello-kotlin

Study for Kotlin
Kotlin
1
star
36

hello-react

React ๊ณต๋ถ€๋ฅผ ์œ„ํ•ธ ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ ๊ณต๊ฐ„
1
star
37

virtual-dom

study for virtual dom
1
star
38

hello-python

Python
1
star
39

math-note

Implement mathematical formulas by code
1
star