axios-logger

repository·master·Indexed 41 days ago

https://github.com/hg-pyun/axios-logger

A 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.

Tokens
2.4K
Snippets
8
Records
11
Agent score
42%

What's inside axios-logger

  1. Configure axios-logger settings

    master

    You can configure the logger in two ways:

    1. Globally: Use setGlobalConfig to apply settings to all loggers.
    2. 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

    PropertyTypeDefaultDescription
    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 false to disable.
    dateFormatstringfalseUses node-dateformat. Use false to 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),
        });
    });
  2. Log Axios requests

    master

    To log outgoing requests, use AxiosLogger.requestLogger within the request interceptor. You can also compose it with your own custom interceptor logic by returning the result of requestLogger(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);
    });
  3. Log Axios errors

    master

    To 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);
    });
  4. Log Axios responses

    master

    To log incoming responses, use AxiosLogger.responseLogger within the response interceptor. You can also compose it with your own custom interceptor logic by returning the result of responseLogger(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);
    });
  5. Configure axios-logger logging settings

    master

    The axios-logger library uses a hierarchical configuration system. You can define settings globally or specifically for requests, responses, and errors. All configuration interfaces extend CommonConfig, which provides shared formatting and output options.

    Common Configuration Options

    These options are available in all configuration interfaces (CommonConfig, GlobalLogConfig, RequestLogConfig, ResponseLogConfig, and ErrorLogConfig):

    • prefixText: A string or boolean to define the text prefix for log entries.
    • dateFormat: A string or boolean to define the date format used in logs.
    • headers: A boolean indicating whether to log request/response headers.
    • logger: A function (text: string) => any that allows you to provide a custom logging implementation (e.g., using console.log, winston, or pino).
    • params: A boolean indicating whether to log request parameters.

    Specialized Configuration Interfaces

    GlobalLogConfig

    Used for global logging behavior. Extends CommonConfig with:

    • 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 CommonConfig with:

    • 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 CommonConfig with:

    • 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 CommonConfig with:

    • data: boolean (log the error data)
    • status: boolean (log the HTTP status code)
    • statusText: boolean (log the HTTP status text)
  6. Use requestLogger to log Axios requests

    master

    The requestLogger is 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);
  7. Use responseLogger to log Axios responses

    master

    The responseLogger is 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);
  8. Use errorLogger to log Axios errors

    master

    The errorLogger is 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);