Nuns Practicing Misanthropy

    @ovotech/axios-logger
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.1 • Public • Published

    Axios Logger Interceptor

    Log request and response, redact all possible PII sources by default, but allow to add custom redact paths so more info can be logged.

    Using

    yarn add @ovotech/axios-logger
    import { axiosLogger, redactHeader } from '@ovotech/axios-logger';
    import axios from 'axios';
    
    const logger = axiosLogger((level, meta, config) => console.log(level, meta, config.url));
    
    const api = axios.create();
    api.interceptors.request.use(logger.request.onFullfilled);
    api.interceptors.response.use(logger.response.onFullfilled, logger.response.onRejected);
    
    // ...
    
    api.get('/my/path');
    
    const body = { user: { cards: [{ id: '111' }, { id: '222' }] } };
    api.post('/update/path', body, { headers: { [redactHeader]: 'requestBody.user.cards.*.id' } });

    You have 3 interceptors. logger.request.onFullfilled, logger.response.onFullfilled and logger.response.onRejected.

    • logger.request.onFullfilled used to setup the initial execution time. If omitted request time would not be logged.
    • logger.response.onFullfilled logs a successful response
    • logger.response.onRejected logs an error

    Each one can be omitted if you don't want or need that feature.

    The log function will receive 3 arguments - level, meta and axios request config. The first one indicates what type of log level to use - "info" for success and "error" for error. The second contains an object of data to log.

    {
      uri: '/my/path',
      method: 'get',
      params: { id: '10' }
      requestBody: { id: '10' },
      responseBody: { user: 'Name' },
      status: 200,
      responseTime: 21,
    }

    By default uri, params, requestBody and responseBody will be "redacted", since they can contain personally identifiable information. You can control that with the redactHeader. Its a comma separated list of dot delimited field paths to be redacted. Can contain wildcard * path to target all array items.

    For example to redact some fields.

    api.post('/update/path', body, { headers: { [redactHeader]: 'requestBody.id, responseBody.user' });

    You can also set redact at the axios instance level for global redaction rules:

    const api = axios.create({ redact: ['requestBody'] });

    Granular logging

    You can perform different things on error / success by inspecting the "level" argument, passed to the log function.

    import { axiosLogger } from '@ovotech/axios-logger';
    import axios from 'axios';
    
    const logger = axiosLogger((level, meta) => {
      if (level === 'info') {
        myOwnLogger.info('Successful request', meta);
        graphResponseTimes(meta.responseTime);
      }
      if (level === 'error') {
        myOwnLogger.error('Error request', meta);
      }
    });

    Running the tests

    You can run the tests with:

    yarn test

    Coding style (linting, etc) tests

    Style is maintained with prettier and tslint

    yarn lint
    

    Deployment

    Deployment is preferment by lerna automatically on merge / push to master, but you'll need to bump the package version numbers yourself. Only updated packages with newer versions will be pushed to the npm registry.

    Contributing

    Have a bug? File an issue with a simple example that reproduces this so we can take a look & confirm.

    Want to make a change? Submit a PR, explain why it's useful, and make sure you've updated the docs (this file) and the tests (see test folder).

    License

    This project is licensed under Apache 2 - see the LICENSE file for details

    Keywords

    none

    Install

    npm i @ovotech/axios-logger

    DownloadsWeekly Downloads

    12

    Version

    2.0.1

    License

    Apache-2.0

    Unpacked Size

    16 kB

    Total Files

    10

    Last publish

    Collaborators

    • rtsarykcorgi
    • ylukomskyi-corgi
    • david.regula.ovo
    • vitalii_khudenko
    • samuelnyamukapa
    • ishanfernando-ovo
    • radhika-bijibilla
    • mbartish-ovo
    • kaluzajianzu
    • a.skomarovskyi
    • grahamdawson-ovo
    • pkari
    • srinivasdudam2
    • shatchardovo
    • tigrago
    • ronald.nsabiyera
    • marcopie_at_kaluza
    • clueless_benefactee
    • michaelwheeler
    • prtn-ovo
    • rafolo78
    • tc-kaluza
    • lutza
    • dmytro.kubatko
    • bucabug
    • thomas.finch
    • v.kolesnyk
    • mykola.p
    • s.slotskyi-ovo
    • hantastico
    • joemaher
    • tanjil-hussain
    • adam.vile
    • raluza
    • ipa-bot
    • luigi94
    • anthony_tonev
    • simonarcher999
    • dominicboston-kaluza
    • iblamefish
    • robert-smith-01
    • katebee-kaluza
    • gideoncaspi
    • coderdanuk
    • kawbot
    • markberesford301
    • ovo-homeplan
    • rupson
    • oeptariffs
    • tony.ross
    • bvjones
    • data.discovery.ovo
    • ralitsa
    • fraserhamiltonovo
    • accrecovo
    • luigi.riefolo
    • andy-heywood-ovo
    • rmcnovo
    • ggerikp
    • xnejp03
    • emmapr123
    • andrew-brook-rad
    • a.calderwood
    • sulgee.kim
    • sseccombe
    • samcooper720x
    • lughino
    • amayuk
    • jagreenwood1
    • radek_tomasek
    • clarencedglee
    • ovocms
    • ovotech-sme-team
    • gjain-npm-ovo
    • kenneth-gray
    • nevenablagoeva
    • pete-woodland
    • orion-migration-team
    • ovotech-sg
    • chanex
    • ovotech-qs
    • ovotech-paym
    • ovotech-payments
    • ovotech-live
    • ovotech-payg
    • freddybushboy
    • sarahlikeshiny
    • ovotech-boost
    • agaovo
    • filose
    • props
    • yesdaveovo
    • rob-ovo
    • ellafutkowska
    • ikerin
    • mwidurek
    • andreaborsos
    • tomshawovo
    • ovotech-identity
    • iuna4e
    • lcatallo
    • orex-team
    • ahvargas
    • cwkaluza
    • smart-heat-uat
    • smart-heat-prod
    • potsec
    • rosario-ovo
    • tech.international
    • jvmovo
    • boost-smile
    • nicolasov
    • ovo.cms.devs
    • mkohlmyr
    • sketchingdev
    • mrkiplin
    • ovo-devices
    • sophiepoole
    • joepurnell-ovo
    • vkobyletskyi_ovo
    • richardday-ovo
    • rparkhomchuk
    • daumenta
    • benoitovo
    • v.vasylets
    • enza252
    • lewisunsworthkaluza
    • t.vytrykush
    • vslepkan
    • jubril
    • petro.shevchuk.corgi
    • melcbuckov
    • kir_exp
    • zyurii
    • vlavrynenko-nix
    • timmy.antonio
    • brettburman-code
    • vzahakailo
    • petro.pavlenko
    • goncalogarcia
    • chris.dickson.kaluza
    • muigui
    • k-fernandez
    • alonabarabash
    • manojkakarla
    • nelsma
    • jadamiec-ovo
    • georgi.krastev
    • marknreynolds
    • m_heald
    • gregshielkaluza
    • gordonmartin
    • ovo-data-science-engineering-bot
    • jonnyleakaluza
    • stephen.harris
    • alex-wilson-ovo
    • scdf
    • xenjke
    • rob.straughan
    • molbalazs
    • joewhittles
    • sampennington64
    • marcusgriff
    • amelia.ovo
    • juliabutterly
    • chris.smith
    • sophiesillmanovo
    • marcesquerra
    • pedoublety
    • ovo-engagement
    • marcuskielly
    • david.chellapah.ovo
    • kupxc
    • harrisonbaxter
    • apjm
    • shnist
    • mikemchugh
    • sarahbeharry
    • ursularodgers
    • inlustra
    • andrewjtn
    • tarlingovo
    • troyb95
    • david.ovo
    • ovoenergyapps
    • marina-ovo
    • tom-g-dane
    • homemoves
    • samwest
    • sophiefield
    • simonmclean-ovo
    • jamesbaum
    • robert-g-j
    • paceteamkaluza
    • ovo-oot-bot
    • stuharv-ovo
    • eddiec86
    • csherwin
    • g-tibbs
    • puzzledbytheweb
    • lewright
    • pedro.caldeira
    • aidenscott2016
    • keirlawson
    • dwfullerton
    • gordok
    • darrenthomas-sse
    • seagullmouse
    • markwood23
    • timsteeleovo
    • jacktreble
    • gordon-rennie-ovo
    • adam-mcdevitt
    • jkiely
    • ovo-dc
    • orion-digital-support-experience
    • ovotech-smart-thermostat
    • yahmad
    • kelemensanyi
    • wtaylor-ovo
    • jrdavenport
    • dleyland-ovo
    • rob.desbois
    • sicrossley
    • lewisdick-ovo
    • sjmann
    • stevemossovo
    • eduardolaranjo
    • unibozu
    • cp-ui-tooling
    • lenardprattovo
    • dieman89
    • jensraaby-ovo
    • anthonysmithovo
    • tom.sherman
    • tomlloyd
    • matyas-ovo
    • jamesnoble1
    • phil-pinkowski
    • ovo-aarongibbison
    • nebuladesignsystem
    • ovo-matt-hodges
    • luke-adams-ovo
    • j_kapella
    • mike.walters
    • bgzstephen
    • matthewbursteinovo
    • kslat3r
    • mtardugno-ovo
    • zoelanham
    • jchoskins
    • tom.mottram.kaluza
    • ewan-m
    • mishabruml
    • maciek-kaluza
    • vmary
    • mugishau
    • peterh-ovo
    • dvidg
    • nickfitton-ovo
    • dave.allison
    • oep-accounts-bot
    • cbousie
    • j.okeefe
    • ursula_rodgers
    • jgarciapaj
    • petegrace
    • retrojetpacks
    • benaston13
    • ovo-bit-tech
    • fulvio.ovo
    • blair.calderwood.radically
    • pedromss
    • simonlewissse
    • joolskaluza
    • jifarra-kaluza
    • jameswelshkaluza
    • kimnil
    • carolinelywood
    • mike-gregory-kaluza
    • vasil.dininski
    • sir_hiss
    • csillabarna
    • luca.sale
    • friendigo
    • oroberts221
    • pedro.costa.kaluza
    • annebeth-ovo
    • quartin
    • georgexcollins
    • kapikaluza
    • iovana.pavlovici
    • filosganga
    • chris.brindley
    • engagement-insights
    • hentielouw
    • jaws-bot
    • leaski
    • elliekempster91
    • zoejm
    • rekaelek-ovo
    • nathanmarshovo
    • robturnerovo
    • dsingh07
    • ovo-james
    • jthomasovo