Skip to content
An Axios interceptor that logs all axios requests and responses to a MySQL table.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.eslintignore
.eslintrc.js
.gitattributes
.gitignore
LICENSE
README.md
package-lock.json
package.json

README.md

Axios Logger MySQL

npm

A Axios interceptor that logs all axios requests and responses to a MySQL table.

Couldn't find one when I was working on a nodejs project so I built this off Yoctol's MongoDB version. Hope it helps :)

Installation

Install using npm:

npm install axios-logger-mysql

Run the SQL below to insert table holds the request logs

CREATE TABLE `requestlogs` (
  `id` int(11) NOT NULL,
  `method` varchar(255) DEFAULT NULL,
  `host` varchar(255) DEFAULT NULL,
  `path` varchar(255) DEFAULT NULL,
  `requestheaders` text,
  `requestQuery` text,
  `requestBody` text,
  `responseStatus` text,
  `responseHeaders` text,
  `responseBody` text,
  `responseError` text,
  `responseTime` varchar(255) DEFAULT NULL,
  `createdAt` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `requestlogs`
  ADD PRIMARY KEY (`id`),
  ADD KEY `host` (`host`),
  ADD KEY `path` (`path`),
  ADD KEY `createdAt` (`createdAt`);

ALTER TABLE `requestlogs` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

API Reference

Param Type Description Options
host String MySQL connection host url.
user String MySQL user.
password String MySQL user password.
database String MySQL database name.
port String MySQL connection port.
table String MySQL table where the logs will be stored.
excludeColumns Array Exclude parameters that you don't need on your logs. ['method', 'host', 'path', 'requestheaders', 'requestQuery', 'requestBody','responseStatus', 'responseHeaders', 'responseBody', 'responseError', 'responseTime', 'createdAt',]
allInstances Boolean Support all of axios instances or not.

Usage

const { useMysqlLogger } = require('axios-logger-mysql');

useMysqlLogger(axios, {
  host: '',
  user: '',
  password: '',
  database:'',
  table:'requestlogs',
  excludeColumns:['']
});

To support all of axios instances, set option allInstances to true:

useMysqlLogger(axios, {
  host: '',
  user: '',
  password: '',
  database:'',
  table:'requestlogs',
  excludeColumns:['']
  allInstances: true,
});

Credits

You can’t perform that action at this time.