rainbow icon indicating copy to clipboard operation
rainbow copied to clipboard

feat: new `@/logger` interface

Open estrattonbailey opened this issue 1 year ago • 2 comments

Fixes RNBW-4477

What changed (plus any additional context for devs)

This is the first pass at a new logger, based on our spec.

Features:

  • unified interface
  • log levels: debug, info, warn, and error
  • deeper integration with Sentry, automatic breadcrumbs, exception handling for errors
  • differentiation between dev/test/prod modes
    • no logs in test mode by default
  • filtered logs, no spamming of console
    • only see logs that are relevant to what you're working on
  • documentation
  • full test suite

Key notes:

  • log level defaults to warn in dev (i.e. only warn or error are logged to console), and we should set it to info in prod to capture more breadcrumbs
  • debug, info, and warn are all capable of being reported to Sentry as breadcrumbs, depending on log level
  • error is always sent as captureException

FAQ:

So should logger.debug be preferred over console.log?

Pretty much! I think using the logger is like saying "I want other devs to be able to see this too", like a more permanent approach. But I don't think we need to block the usage of console.log during dev. We should probably lint for console though, just to try and avoid some stray console.log(privateKey) ends up in Sentry.

At a glance:

import { logger, RainbowError } from '@/logger'

logger.debug('Entering fetch block') // Enabled with LOG_LEVEL=debug
logger.debug('Computing estimated gas', { duration }, logger.DebugContext.swaps) // Enabled with LOG_LEVEL or LOG_DEBUG=swaps

logger.info('User has X preference enabled') // Enabled with LOG_LEVEL=info
logger.info('Response returned successfully', {
  duration: '123ms' // Additional data
})
logger.info(`User navigated from ${prevRoute} to ${nextRoute}`, {
  type: 'navigation' // Sentry convention
})

logger.warn('Zerion failed, trying N more times before fallback', {
  type: 'http', // Sentry convention
  attempts: 2 // Additional data
})

try {
  // some async code
} catch (e) {
  const error = new RainbowError('Request to Simplehash threw an error. Look for network issues.')
  logger.error(error, {
    tags: { ...tags } // Sentry convention
  })
}

Screen recordings / screenshots

Screen Shot 2022-09-15 at 2 08 37 PM

What to test

Screen Shot 2022-09-15 at 3 03 49 PM

Final checklist

  • [x] Assigned individual reviewers?
  • [x] Added labels? (team1/team2, critical path, release, dev QA)
  • [x] Did you test both iOS and Android?
  • [x] If your changes are visual, did you check both the light and dark themes?
  • [x] Added e2e tests? If not, please specify why
  • [x] If you added new critical path files, did you update the CODEOWNERS file?
  • [x] If no dev QA label, did you add the PR to the QA Queue?

estrattonbailey avatar Sep 14 '22 16:09 estrattonbailey

@sammdec added a FAQ to the PR description. Great question 👍

estrattonbailey avatar Sep 16 '22 17:09 estrattonbailey

Added some colors for a nicer experience and improved the stacktrace output.

Screen Shot 2022-09-22 at 2 15 28 PM

estrattonbailey avatar Sep 22 '22 19:09 estrattonbailey