mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1# Logger 2 3Simple logger for Bluesky. Supports log levels, debug contexts, and separate 4transports for production, dev, and test mode. 5 6## At a Glance 7 8```typescript 9import { logger } from '#/logger' 10 11logger.debug(message[, metadata, debugContext]) 12logger.info(message[, metadata]) 13logger.log(message[, metadata]) 14logger.warn(message[, metadata]) 15logger.error(error[, metadata]) 16``` 17 18#### Modes 19 20The "modes" referred to here are inferred from the values exported from `#/env`. 21Basically, the booleans `IS_DEV`, `IS_TEST`, and `IS_PROD`. 22 23#### Log Levels 24 25Log levels are used to filter which logs are either printed to the console 26and/or sent to Sentry and other reporting services. To configure, set the 27`EXPO_PUBLIC_LOG_LEVEL` environment variable in `.env` to one of `debug`, 28`info`, `log`, `warn`, or `error`. 29 30This variable should be `info` in production, and `debug` in dev. If it gets too 31noisy in dev, simply set it to a higher level, such as `warn`. 32 33## Usage 34 35```typescript 36import { logger } from '#/logger'; 37``` 38 39### `logger.error` 40 41The `error` level is for... well, errors. These are sent to Sentry in production mode. 42 43`error`, along with all log levels, supports an additional parameter, `metadata: Record<string, unknown>`. Use this to provide values to the [Sentry 44breadcrumb](https://docs.sentry.io/platforms/react-native/enriching-events/breadcrumbs/#manual-breadcrumbs). 45 46```typescript 47try { 48 // some async code 49} catch (e) { 50 logger.error(e, { ...metadata }); 51} 52``` 53 54### `logger.warn` 55 56Warnings will be sent to Sentry as a separate Issue with level `warning`, as 57well as as breadcrumbs, with a severity level of `warning` 58 59### `logger.log` 60 61Logs with level `log` will be sent to Sentry as a separate Issue with level `log`, as 62well as as breadcrumbs, with a severity level of `default`. 63 64### `logger.info` 65 66The `info` level should be used for information that would be helpful in a 67tracing context, like Sentry. In production mode, `info` logs are sent 68to Sentry as breadcrumbs, which decorate log levels above `info` such as `log`, 69`warn`, and `error`. 70 71### `logger.debug` 72 73Debug level is really only intended for local development. Use this instead of 74`console.log`. 75 76```typescript 77logger.debug(message, { ...metadata }); 78``` 79 80Inspired by [debug](https://www.npmjs.com/package/debug), when writing debug 81logs, you can optionally pass a _context_, which can be then filtered when in 82debug mode. 83 84This value should be related to the feature, component, or screen 85the code is running within, and **it should be defined in `#/logger/debugContext`**. 86This way we know if a relevant context already exists, and we can trace all 87active contexts in use in our app. This const enum is conveniently available on 88the `logger` at `logger.DebugContext`. 89 90For example, a debug log like this: 91 92```typescript 93logger.debug(message, {}, logger.DebugContext.composer); 94``` 95 96Would be logged to the console in dev mode if `EXPO_PUBLIC_LOG_LEVEL=debug`, _or_ if you 97pass a separate environment variable `LOG_DEBUG=composer`. This variable supports 98multiple contexts using commas like `LOG_DEBUG=composer,profile`, and _automatically 99sets the log level to `debug`, regardless of `EXPO_PUBLIC_LOG_LEVEL`._