axios-logger
Advanced tools
Comparing version
{ | ||
"name": "axios-logger", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Beautify Axios Logging Messages", | ||
@@ -34,3 +34,4 @@ "main": "src/index.js", | ||
"eslint": "^4.19.1", | ||
"express": "^4.16.3" | ||
"express": "^4.16.3", | ||
"nodemon": "^1.17.5" | ||
}, | ||
@@ -37,0 +38,0 @@ "dependencies": { |
# axios-logger | ||
Beautify Axios Logging Messages. | ||
> Beautify Axios Logging Messages. | ||
# Installation | ||
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. | ||
 | ||
# Install | ||
``` | ||
$ npm install axios-logger --save-dev | ||
``` | ||
# How to use | ||
You can use logger with `axios` interceptor. | ||
#### Request | ||
```javascript | ||
import axios from 'axios'; | ||
import AxiosLogger from 'axios-logger'; | ||
const instance = axios.create(); | ||
instance.interceptors.request.use(AxiosLogger.requestLogger); | ||
``` | ||
If you want to use your custom interceptor, you can mixin with logger like this. | ||
```javascript | ||
instance.interceptors.request.use((config) => { | ||
// write down your request intercept. | ||
AxiosLogger.requestLogger(config); | ||
}); | ||
``` | ||
#### Response | ||
```javascript | ||
import axios from 'axios'; | ||
import AxiosLogger from 'axios-logger'; | ||
const instance = axios.create(); | ||
instance.interceptors.request.use(AxiosLogger.responseLogger); | ||
``` | ||
If you want to use your custom interceptor, you can mixin with logger like this. | ||
```javascript | ||
instance.interceptors.response.use((response) => { | ||
// write down your response intercept. | ||
AxiosLogger.responseLogger(response); | ||
}); | ||
``` | ||
#### Error | ||
You can errorLogger that pass second argument. | ||
```javascript | ||
import axios from 'axios'; | ||
import AxiosLogger from 'axios-logger'; | ||
const instance = axios.create(); | ||
instance.interceptors.request.use(AxiosLogger.requestLogger, AxiosLogger.errorLogger); | ||
instance.interceptors.response.use(AxiosLogger.responseLogger, AxiosLogger.errorLogger); | ||
``` | ||
If you want to use your custom interceptor, you can mixin with logger like this. | ||
```javascript | ||
instance.interceptors.response.use(AxiosLogger.requestLogger, (err) =>{ | ||
// write down your error intercept. | ||
AxiosLogger.errorLogger(err); | ||
}); | ||
``` | ||
# CONTRIBUTE | ||
I always welcome Feedback and Pall Request :) |
@@ -0,4 +1,20 @@ | ||
const chalk = require('chalk'); | ||
const Printer = require('../utility/Printer'); | ||
const time = require('../utility/time'); | ||
function errorLogger(err) { | ||
console.log(error); | ||
return Promise.reject(error); | ||
// logging objects. | ||
const printer = new Printer(); | ||
const {config, status, statusText} = err.response; | ||
printer.addText(chalk.red('[Axios Error]')); | ||
printer.addText(`[${time.getCurrentWithFormat()}]`); | ||
printer.addText(`${chalk.yellow(`${config.method.toUpperCase()}`)}`); | ||
printer.addText(config.url); | ||
printer.addText(`${status}:${statusText}`); | ||
Printer.execute(printer.getFullTextWithSpace()); | ||
return Promise.reject(err); | ||
} | ||
@@ -5,0 +21,0 @@ |
const chalk = require('chalk'); | ||
const Printer = require('../utility/Printer'); | ||
const time = require('../utility/time'); | ||
function requestLogger(config) { | ||
const {url, method, data, contentType, responseType} = config; | ||
function requestLogger(axiosConfig) { | ||
const {url, method, data} = axiosConfig; | ||
const printer = new Printer(); | ||
printer.addLine(`${chalk.green(`[Request Helper] ${url}`)}`); | ||
printer.addLine(`- method: ${method}`); | ||
printer.addLine(`- contentType: ${contentType}`); | ||
printer.addLine(`- responseType: ${responseType}`); | ||
printer.addLine(`- data: ${JSON.stringify(data)}`); | ||
printer.addText(chalk.green('[Axios Request]')); | ||
printer.addText(`[${time.getCurrentWithFormat()}]`); | ||
printer.addText(`${chalk.yellow(`${method.toUpperCase()}`)}`); | ||
printer.addText(`${url}`); | ||
printer.addText(`${JSON.stringify(data)}`); | ||
printer.execute(); | ||
Printer.execute(printer.getFullTextWithSpace()); | ||
return config; | ||
return axiosConfig; | ||
} | ||
@@ -19,0 +19,0 @@ |
const chalk = require('chalk'); | ||
const Printer = require('../utility/Printer'); | ||
const time = require('../utility/time'); | ||
function responseLogger(response) { | ||
const {status, statusText, headers, data} = response; | ||
const {config, status, statusText, data} = response; | ||
const printer = new Printer(); | ||
printer.addLine(`${chalk.green(`[Response Helper] ${status}:${statusText}`)}`); | ||
printer.addLine(`- data: ${JSON.stringify(data)}`); | ||
printer.addText(chalk.green('[Axios Response]')); | ||
printer.addText(`[${time.getCurrentWithFormat()}]`); | ||
printer.addText(`${chalk.yellow(`${config.method.toUpperCase()}`)}`); | ||
printer.addText(`${status}:${statusText}`); | ||
printer.addText(config.url); | ||
printer.addText(`${JSON.stringify(data)}`); | ||
printer.execute(); | ||
Printer.execute(printer.getFullTextWithSpace()); | ||
@@ -14,0 +19,0 @@ return response; |
@@ -6,16 +6,17 @@ class Printer { | ||
addLine(string) { | ||
this.textArray.push(string); | ||
addText(string) { | ||
if (string !== 'undefined') | ||
this.textArray.push(string); | ||
} | ||
getFullText() { | ||
return this.textArray.join('\n'); | ||
getFullTextWithSpace() { | ||
return this.textArray.join(' '); | ||
} | ||
execute() { | ||
console.log(this.getFullText()); | ||
getFullTextWithLine() { | ||
return this.textArray.join('\n'); | ||
} | ||
toString() { | ||
return this.getFullText(); | ||
static execute(text) { | ||
console.log(text); | ||
} | ||
@@ -22,0 +23,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
28897
43.93%18
5.88%69
46.81%70
775%11
10%