rainbow
rainbow copied to clipboard
feat: new `@/logger` interface
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
, anderror
- 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. onlywarn
orerror
are logged to console), and we should set it toinfo
in prod to capture more breadcrumbs -
debug
,info
, andwarn
are all capable of being reported to Sentry as breadcrumbs, depending on log level -
error
is always sent ascaptureException
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](https://user-images.githubusercontent.com/4732330/190507212-d1a92161-6872-447a-a14e-3e17edef2efe.png)
What to test
![Screen Shot 2022-09-15 at 3 03 49 PM](https://user-images.githubusercontent.com/4732330/190507224-98b286de-198f-4b1d-a59e-f98853a179fb.png)
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?
@sammdec added a FAQ to the PR description. Great question 👍
Added some colors for a nicer experience and improved the stacktrace output.
![Screen Shot 2022-09-22 at 2 15 28 PM](https://user-images.githubusercontent.com/4732330/191832294-37be86cc-127d-41cf-9bad-65a16ce576e6.png)