-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1813 from bluesky-social/eric/app-903-extract-log…
…ger-into-singleton Add new logger
- Loading branch information
Showing
70 changed files
with
1,109 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
SENTRY_AUTH_TOKEN= | ||
EXPO_PUBLIC_LOG_LEVEL=debug | ||
EXPO_PUBLIC_LOG_DEBUG= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const IS_TEST = process.env.NODE_ENV === 'test' | ||
export const IS_DEV = __DEV__ | ||
export const IS_PROD = !IS_DEV | ||
export const LOG_DEBUG = process.env.EXPO_PUBLIC_LOG_DEBUG || '' | ||
export const LOG_LEVEL = (process.env.EXPO_PUBLIC_LOG_LEVEL || 'info') as | ||
| 'debug' | ||
| 'info' | ||
| 'warn' | ||
| 'error' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Logger | ||
|
||
Simple logger for Bluesky. Supports log levels, debug contexts, and separate | ||
transports for production, dev, and test mode. | ||
|
||
## At a Glance | ||
|
||
```typescript | ||
import { logger } from '#/logger' | ||
|
||
logger.debug(message[, metadata, debugContext]) | ||
logger.info(message[, metadata]) | ||
logger.log(message[, metadata]) | ||
logger.warn(message[, metadata]) | ||
logger.error(error[, metadata]) | ||
``` | ||
|
||
#### Modes | ||
|
||
The "modes" referred to here are inferred from the values exported from `#/env`. | ||
Basically, the booleans `IS_DEV`, `IS_TEST`, and `IS_PROD`. | ||
|
||
#### Log Levels | ||
|
||
Log levels are used to filter which logs are either printed to the console | ||
and/or sent to Sentry and other reporting services. To configure, set the | ||
`EXPO_PUBLIC_LOG_LEVEL` environment variable in `.env` to one of `debug`, | ||
`info`, `log`, `warn`, or `error`. | ||
|
||
This variable should be `info` in production, and `debug` in dev. If it gets too | ||
noisy in dev, simply set it to a higher level, such as `warn`. | ||
|
||
## Usage | ||
|
||
```typescript | ||
import { logger } from '#/logger'; | ||
``` | ||
|
||
### `logger.error` | ||
|
||
The `error` level is for... well, errors. These are sent to Sentry in production mode. | ||
|
||
`error`, along with all log levels, supports an additional parameter, `metadata: Record<string, unknown>`. Use this to provide values to the [Sentry | ||
breadcrumb](https://docs.sentry.io/platforms/react-native/enriching-events/breadcrumbs/#manual-breadcrumbs). | ||
|
||
```typescript | ||
try { | ||
// some async code | ||
} catch (e) { | ||
logger.error(e, { ...metadata }); | ||
} | ||
``` | ||
|
||
### `logger.warn` | ||
|
||
Warnings will be sent to Sentry as a separate Issue with level `warning`, as | ||
well as as breadcrumbs, with a severity level of `warning` | ||
|
||
### `logger.log` | ||
|
||
Logs with level `log` will be sent to Sentry as a separate Issue with level `log`, as | ||
well as as breadcrumbs, with a severity level of `default`. | ||
|
||
### `logger.info` | ||
|
||
The `info` level should be used for information that would be helpful in a | ||
tracing context, like Sentry. In production mode, `info` logs are sent | ||
to Sentry as breadcrumbs, which decorate log levels above `info` such as `log`, | ||
`warn`, and `error`. | ||
|
||
### `logger.debug` | ||
|
||
Debug level is really only intended for local development. Use this instead of | ||
`console.log`. | ||
|
||
```typescript | ||
logger.debug(message, { ...metadata }); | ||
``` | ||
|
||
Inspired by [debug](https://www.npmjs.com/package/debug), when writing debug | ||
logs, you can optionally pass a _context_, which can be then filtered when in | ||
debug mode. | ||
|
||
This value should be related to the feature, component, or screen | ||
the code is running within, and **it should be defined in `#/logger/debugContext`**. | ||
This way we know if a relevant context already exists, and we can trace all | ||
active contexts in use in our app. This const enum is conveniently available on | ||
the `logger` at `logger.DebugContext`. | ||
|
||
For example, a debug log like this: | ||
|
||
```typescript | ||
logger.debug(message, {}, logger.DebugContext.composer); | ||
``` | ||
|
||
Would be logged to the console in dev mode if `EXPO_PUBLIC_LOG_LEVEL=debug`, _or_ if you | ||
pass a separate environment variable `LOG_DEBUG=composer`. This variable supports | ||
multiple contexts using commas like `LOG_DEBUG=composer,profile`, and _automatically | ||
sets the log level to `debug`, regardless of `EXPO_PUBLIC_LOG_LEVEL`._ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {expect, test} from '@jest/globals' | ||
|
||
import {ConsoleTransportEntry, LogLevel} from '#/logger' | ||
import {add, getEntries} from '#/logger/logDump' | ||
|
||
test('works', () => { | ||
const items: ConsoleTransportEntry[] = [ | ||
{ | ||
id: '1', | ||
level: LogLevel.Debug, | ||
message: 'hello', | ||
metadata: {}, | ||
timestamp: Date.now(), | ||
}, | ||
{ | ||
id: '2', | ||
level: LogLevel.Debug, | ||
message: 'hello', | ||
metadata: {}, | ||
timestamp: Date.now(), | ||
}, | ||
{ | ||
id: '3', | ||
level: LogLevel.Debug, | ||
message: 'hello', | ||
metadata: {}, | ||
timestamp: Date.now(), | ||
}, | ||
] | ||
|
||
for (const item of items) { | ||
add(item) | ||
} | ||
|
||
expect(getEntries()).toEqual(items.reverse()) | ||
}) |
Oops, something went wrong.