Skip to content

Commit

Permalink
Fix web build (bluesky-social#7069)
Browse files Browse the repository at this point in the history
* Fix web build

* Fix circular deps

* Hoist log functions
  • Loading branch information
gaearon authored Dec 12, 2024
1 parent 20ac42a commit 704e36c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 88 deletions.
23 changes: 23 additions & 0 deletions src/logger/bitdriftTransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
debug as bdDebug,
error as bdError,
info as bdInfo,
warn as bdWarn,
} from '@bitdrift/react-native'

import {LogLevel, Transport} from './types'

export function createBitdriftTransport(): Transport {
const logFunctions = {
[LogLevel.Debug]: bdDebug,
[LogLevel.Info]: bdInfo,
[LogLevel.Log]: bdInfo,
[LogLevel.Warn]: bdWarn,
[LogLevel.Error]: bdError,
} as const

return (level, message) => {
const log = logFunctions[level]
log(message.toString())
}
}
7 changes: 7 additions & 0 deletions src/logger/bitdriftTransport.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Transport} from './index'

export function createBitdriftTransport(): Transport {
return (_level, _message) => {
// noop
}
}
94 changes: 6 additions & 88 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import {
debug as bdDebug,
error as bdError,
info as bdInfo,
warn as bdWarn,
} from '@bitdrift/react-native'
import format from 'date-fns/format'
import {nanoid} from 'nanoid/non-secure'

Expand All @@ -12,74 +6,12 @@ import {DebugContext} from '#/logger/debugContext'
import {add} from '#/logger/logDump'
import {Sentry} from '#/logger/sentry'
import * as env from '#/env'
import {createBitdriftTransport} from './bitdriftTransport'
import {Metadata} from './types'
import {ConsoleTransportEntry, LogLevel, Transport} from './types'

export enum LogLevel {
Debug = 'debug',
Info = 'info',
Log = 'log',
Warn = 'warn',
Error = 'error',
}

type Transport = (
level: LogLevel,
message: string | Error,
metadata: Metadata,
timestamp: number,
) => void

/**
* A union of some of Sentry's breadcrumb properties as well as Sentry's
* `captureException` parameter, `CaptureContext`.
*/
type Metadata = {
/**
* Applied as Sentry breadcrumb types. Defaults to `default`.
*
* @see https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/#breadcrumb-types
*/
type?:
| 'default'
| 'debug'
| 'error'
| 'navigation'
| 'http'
| 'info'
| 'query'
| 'transaction'
| 'ui'
| 'user'

/**
* Passed through to `Sentry.captureException`
*
* @see https://github.com/getsentry/sentry-javascript/blob/903addf9a1a1534a6cb2ba3143654b918a86f6dd/packages/types/src/misc.ts#L65
*/
tags?: {
[key: string]:
| number
| string
| boolean
| bigint
| symbol
| null
| undefined
}

/**
* Any additional data, passed through to Sentry as `extra` param on
* exceptions, or the `data` param on breadcrumbs.
*/
[key: string]: unknown
} & Parameters<typeof Sentry.captureException>[1]

export type ConsoleTransportEntry = {
id: string
timestamp: number
level: LogLevel
message: string | Error
metadata: Metadata
}
export {LogLevel}
export type {ConsoleTransportEntry, Transport}

const enabledLogLevels: {
[key in LogLevel]: LogLevel[]
Expand Down Expand Up @@ -137,20 +69,6 @@ export const consoleTransport: Transport = (
}
}

export const bitdriftTransport: Transport = (level, message) => {
const log = (
{
[LogLevel.Debug]: bdDebug,
[LogLevel.Info]: bdInfo,
[LogLevel.Log]: bdInfo,
[LogLevel.Warn]: bdWarn,
[LogLevel.Error]: bdError,
} as const
)[level]

log(message.toString())
}

export const sentryTransport: Transport = (
level,
message,
Expand Down Expand Up @@ -349,7 +267,7 @@ export class Logger {
export const logger = new Logger()

if (!env.IS_TEST) {
logger.addTransport(bitdriftTransport)
logger.addTransport(createBitdriftTransport())
}

if (env.IS_DEV && !env.IS_TEST) {
Expand Down
69 changes: 69 additions & 0 deletions src/logger/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type {Sentry} from '#/logger/sentry'

export enum LogLevel {
Debug = 'debug',
Info = 'info',
Log = 'log',
Warn = 'warn',
Error = 'error',
}

export type Transport = (
level: LogLevel,
message: string | Error,
metadata: Metadata,
timestamp: number,
) => void

/**
* A union of some of Sentry's breadcrumb properties as well as Sentry's
* `captureException` parameter, `CaptureContext`.
*/
export type Metadata = {
/**
* Applied as Sentry breadcrumb types. Defaults to `default`.
*
* @see https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/#breadcrumb-types
*/
type?:
| 'default'
| 'debug'
| 'error'
| 'navigation'
| 'http'
| 'info'
| 'query'
| 'transaction'
| 'ui'
| 'user'

/**
* Passed through to `Sentry.captureException`
*
* @see https://github.com/getsentry/sentry-javascript/blob/903addf9a1a1534a6cb2ba3143654b918a86f6dd/packages/types/src/misc.ts#L65
*/
tags?: {
[key: string]:
| number
| string
| boolean
| bigint
| symbol
| null
| undefined
}

/**
* Any additional data, passed through to Sentry as `extra` param on
* exceptions, or the `data` param on breadcrumbs.
*/
[key: string]: unknown
} & Parameters<typeof Sentry.captureException>[1]

export type ConsoleTransportEntry = {
id: string
timestamp: number
level: LogLevel
message: string | Error
metadata: Metadata
}

0 comments on commit 704e36c

Please sign in to comment.