bunqCommunity / bunqJSClient

A javascript SDK for the bunq API
MIT License
58 stars 23 forks source link

How to disable axios logging to the console? #49

Closed igoramadas closed 4 years ago

igoramadas commented 4 years ago

Can't figure out how to disable axios logging errors to the console on my Node.js app. Mostly 401 errors when token has expired. These started appearing since I changed the keep alive to true - bunqJSClient.setKeepAlive(true).

It logs the full response, leaking all headers including tokens.

Example (omitted the additional error and response properties):

{ Error: Request failed with status code 401
  at createError (/var/node/jarbunq/node_modules/axios/lib/core/createError.js:16:15)
  at settle (/var/node/jarbunq/node_modules/axios/lib/core/settle.js:18:12)
  at IncomingMessage.handleStreamEnd (/var/node/jarbunq/node_modules/axios/lib/adapters/http.js:202:11)
  at IncomingMessage.emit (events.js:203:15)
  at endReadableNT (_stream_readable.js:1145:12)
  at process._tickCallback (internal/process/next_tick.js:63:19)
  config: { ... },
  request: { ... },
  response: { ... }

Could someone enlighten me on how to catch these errors myself so I can disable the default console log?

Crecket commented 4 years ago

The keepAlive method isn't as reliable because the bunq API doens't provide exact timestamps for the client to try and keep the session alive. So in all my applications I've started using the other variant where it'll simply attempt to create a new session when required.

But to answer your question. It logs directly using this.logger.error( ... ) (found here) at the moment so there is no way to stop that in the current version. I might log the details using a debug/trace level so they can be filtered out more easily or use events to allow for a 'keep-alive-error' event or something along those lines. Will look into it this weekend.

igoramadas commented 4 years ago

I found that specific part code section, but thought maybe I could do something via ENV variables or so. But just now figured out I can use my own logger interface, so I will try that. Thanks for the quick response!

Crecket commented 4 years ago

I understood it as you wanted to log the error but not display the other details. You can use the BUNQ_JS_CLIENT_LOG_LEVEL to set the error level used by the client awell if you want to disable the logging completely.

igoramadas commented 4 years ago

I was already setting the BUNQ_JS_CLIENT_LOG_LEVEL. But doesn't help, as I still want to know if something goes wrong. Quick and simple fix, now creating an instance passing my own custom logger:

const customLogger = {
    log: obj => {
        logger.debug("BunqJSClient", obj)
    },
    trace: obj => {
        logger.debug("BunqJSClient", obj)
    },
    debug: obj => {
        logger.debug("BunqJSClient", obj)
    },
    info: obj => {
        logger.info("BunqJSClient", obj)
    },
    warn: obj => {
        logger.warn("BunqJSClient", obj)
    },
    error: obj => {
        logger.error("BunqJSClient", obj)
    }
}

const bunqClient = new BunqJSClient(myFileStore, customLogger)

My logger has a pre-processor that filters out tokens, credentials and general noise from objects.