axios-logger
repository·master·Indexed 41 days ago
https://github.com/hg-pyun/axios-loggerA utility for beautifying Axios logging messages using interceptors for requests, responses, and errors. Version 2.8.1 provides pre-built logger functions like requestLogger, responseLogger, and errorLogger that can be configured globally via setGlobalConfig or locally to customize the output of HTTP methods, URLs, status codes, and data.
What's inside axios-logger
- The library works by leveraging Axios's interceptors. It provides pre-built logger functions that you can plug directly into your Axios instance to beautify request, response, and error logs in your console.
Install axios-logger
masterInstall the package using npm as a development dependency:
$ npm install axios-logger --save-devConfigure axios-logger settings
masterYou can configure the logger in two ways:
- Globally: Use
setGlobalConfigto apply settings to all loggers. - Locally: Pass a configuration object as the second argument to the logger functions (e.g.,
requestLogger(request, config)). Local settings override global ones.
Configuration Options
Property Type Default Description methodbooleantrueWhether to include HTTP method or not. urlbooleantrueWhether to include the URL or not. paramsbooleanfalseWhether to include the URL params or not. databooleantrueWhether to include request/response data or not. statusbooleantrueWhether to include response statuses or not. statusTextbooleantrueWhether to include response status text or not. headersbooleanfalseWhether to include HTTP headers or not. prefixTextstring'Axios'Custom prefix string. Use falseto disable.dateFormatstringfalseUses node-dateformat. Use falseto disable timestamp.loggerfunctionconsole.logCustom logger function (e.g., winston.info.bind(winston)).// Global configuration setGlobalConfig({ prefixText: 'your prefix', dateFormat: 'HH:MM:ss', status: false, headers: true, logger: someLogger.info.bind(this), }); // Local configuration (overrides global) instance.interceptors.request.use((request) => { return AxiosLogger.requestLogger(request, { prefixText: 'your prefix', dateFormat: 'HH:MM:ss', status: false, headers: true, logger: someLogger.error.bind(this), }); });- Globally: Use
Log Axios requests
masterTo log outgoing requests, use
AxiosLogger.requestLoggerwithin the request interceptor. You can also compose it with your own custom interceptor logic by returning the result ofrequestLogger(request).import axios from 'axios'; import * as AxiosLogger from 'axios-logger'; const instance = axios.create(); // Basic usage instance.interceptors.request.use(AxiosLogger.requestLogger); // Composed usage (mixing with custom logic) instance.interceptors.request.use((request) => { // write down your request intercept. return AxiosLogger.requestLogger(request); });Log Axios errors
masterTo log errors, use
AxiosLogger.errorLogger. You can inject it as the second argument (the error handler) in both request and response interceptors.import axios from 'axios'; import * as AxiosLogger from 'axios-logger'; const instance = axios.create(); // Inject errorLogger into request and response interceptors instance.interceptors.request.use(AxiosLogger.requestLogger, AxiosLogger.errorLogger); instance.interceptors.response.use(AxiosLogger.responseLogger, AxiosLogger.errorLogger); // Composed error usage instance.interceptors.response.use(AxiosLogger.responseLogger, (err) => { // write down your error intercept. return AxiosLogger.errorLogger(err); });Log Axios responses
masterTo log incoming responses, use
AxiosLogger.responseLoggerwithin the response interceptor. You can also compose it with your own custom interceptor logic by returning the result ofresponseLogger(response).import axios from 'axios'; import * as AxiosLogger from 'axios-logger'; const instance = axios.create(); // Basic usage instance.interceptors.response.use(AxiosLogger.responseLogger); // Composed usage (mixing with custom logic) instance.interceptors.response.use((response) => { // write down your response intercept. return AxiosLogger.responseLogger(response); });Configure axios-logger logging settings
masterThe
axios-loggerlibrary uses a hierarchical configuration system. You can define settings globally or specifically for requests, responses, and errors. All configuration interfaces extendCommonConfig, which provides shared formatting and output options.Common Configuration Options
These options are available in all configuration interfaces (
CommonConfig,GlobalLogConfig,RequestLogConfig,ResponseLogConfig, andErrorLogConfig):prefixText: Astringorbooleanto define the text prefix for log entries.dateFormat: Astringorbooleanto define the date format used in logs.headers: Abooleanindicating whether to log request/response headers.logger: A function(text: string) => anythat allows you to provide a custom logging implementation (e.g., usingconsole.log,winston, orpino).params: Abooleanindicating whether to log request parameters.
Specialized Configuration Interfaces
GlobalLogConfig
Used for global logging behavior. Extends
CommonConfigwith:data:boolean(log the body data)method:boolean(log the HTTP method)url:boolean(log the request URL)status:boolean(log the HTTP status code)statusText:boolean(log the HTTP status text)
RequestLogConfig
Used specifically for logging outgoing requests. Extends
CommonConfigwith:data:boolean(log the request body data)method:boolean(log the HTTP method)url:boolean(log the request URL)
ResponseLogConfig
Used specifically for logging incoming responses. Extends
CommonConfigwith:data:boolean(log the response body data)status:boolean(log the HTTP status code)statusText:boolean(log the HTTP status text)
ErrorLogConfig
Used specifically for logging errors. Extends
CommonConfigwith:data:boolean(log the error data)status:boolean(log the HTTP status code)statusText:boolean(log the HTTP status text)
Use requestLogger to log Axios requests
masterThe
requestLoggeris an Axios interceptor used to log details about outgoing requests. You can attach it to an Axios instance to monitor request metadata.import axios from 'axios'; import { requestLogger } from 'axios-logger'; const instance = axios.create(); instance.interceptors.request.use(requestLogger);Configure global settings with setGlobalConfig
masterUsesetGlobalConfigto apply configuration settings globally across all loggers provided by the library.Use responseLogger to log Axios responses
masterThe
responseLoggeris an Axios interceptor used to log details about incoming responses. You can attach it to an Axios instance to monitor response metadata.import axios from 'axios'; import { responseLogger } from 'axios-logger'; const instance = axios.create(); instance.interceptors.response.use(responseLogger);Use errorLogger to log Axios errors
masterThe
errorLoggeris an Axios interceptor used to log details about failed requests and errors. You can attach it to an Axios instance to monitor error metadata.import axios from 'axios'; import { errorLogger } from 'axios-logger'; const instance = axios.create(); instance.interceptors.response.use(null, errorLogger);