@gcoreplus/axios-logger

中文文档
2.0.2发布于 2024/07/18ISCTypeScriptCommonJS近 30 天下载 1.3万

Logger for Axios

原始使用文档
文档目录7 节
  1. About
  2. 👨🏻‍💻Installation
  3. Usage
  4. Configurations
  5. Backers
  6. License
  7. Acknowledgements

npm version Codecov Coverage ISC license

Axios Logger

About

@gcoreplus/axios-logger is a axios interceptor that can log the request/response data displaying the necessary information while communicating with the server.

Inspired by the axios-logger

👨🏻‍💻Installation

Install the main lib

npm install --save @gcoreplus/axios-logger

Usage

import axios from 'axios';
import * as AxiosLogger from '@gcoreplus/axios-logger';

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

Configurations

Custom request/error output logger sample

const instance = axios.create();
instance.interceptors.request.use(
  (request) => AxiosLogger.requestLogger(request, { logger: console.log }),
  (error) => AxiosLogger.errorLogger(error, { logger: console.error })
);

Custom output logger builder sample

import * as AxiosLogger from '@gcoreplus/axios-logger';

AxiosLogger.setGlobalConfig({
  logger: console.log,
  createLoggerBuilder: config => new ObjectLoggerBuilder(config)
});
PropertyTypeDefaultDescription
methodbooleantrueWhether to include HTTP method or not.
urlbooleantrueWhether to include the URL or not.
params.applybooleantrueWhether to include HTTP params or not.
params.redactRedactOptionsundefinedWhether to filter restrict data from params object before print.
data.applybooleantrueWhether to include request/response data or not.
data.redactRedactOptionsundefinedWhether to filter restrict data from data object before print.
headers.applybooleanfalseWhether to include HTTP headers or not.
headers.redactRedactOptionsundefinedWhether to filter restrict data from headers object before print.
statusbooleantrueWhether to include response statuses or not.
prefixTextstring | false'Axios'false => no prefix, otherwise, customize the prefix wanted.
dateFormatdateformat | falsenew Date().toISOString()false => no timestamp, otherwise, customize its format
loggerfunction<string, any>console.logAllows users to customize the logger function to be used. e.g. Winston's logger.info could be leveraged, like this: logger.info.bind(this)
createLoggerBuilderfunction => LoggerBuildernew StringLoggerBuilder(config)Allows to customize the logger output. Possible values, new StringLoggerBuilder(config) and new ObjectLoggerBuilder(config)

Redact with fast-redact - [RedactOptions]

@gcoreplus/axios-logger uses fast-redact to handle restrict data

For more information see fast-redact and redact documentation

Redact configuration example

import * as AxiosLogger from '@gcoreplus/axios-logger';

AxiosLogger.setGlobalConfig({
  headers: {
    apply: true,
    redact: {
      paths: ['["my-token"]', 'cookie'],
      censor: '***'
    }
  }
});

Backers

Rennan Stefan Boni

License

Distributed under the ISC License. See LICENSE for more information.

Acknowledgements

Copyright © 2020 gCorePlus Inc.

版本与发布记录

@gcoreplus/axios-logger 共发布 15 个版本,首次发布于 2020/12/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.2latest2024/07/182 年前
2.0.2-rc.22024/07/182 年前
2.0.2-rc.12024/07/182 年前
2.0.12024/07/182 年前
2.0.1-rc.02024/07/182 年前
2.0.02024/03/182 年前

关于 @gcoreplus/axios-logger 的常见问题

@gcoreplus/axios-logger 是什么?

Logger for Axios

如何安装 @gcoreplus/axios-logger?

在项目目录运行 npm install @gcoreplus/axios-logger,或使用 pnpm add / yarn add / bun add。

@gcoreplus/axios-logger 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gcoreplus/axios-logger 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@gcoreplus/axios-logger 可以商用吗?

@gcoreplus/axios-logger 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gcoreplus/axios-logger 的下载量有多少?

根据 npm 官方下载统计,@gcoreplus/axios-logger 在 2026/10/16 – 2026/11/14 这 30 天内被下载 13,083 次,日均约 436 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gcoreplus/axios-logger 还在维护吗?

最新版本 2.0.2 发布于 2024/07/18(2 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/12/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。