Migrate usages of debug context

This commit is contained in:
Eric Bailey
2025-02-11 12:32:36 -06:00
parent 9f7a4166aa
commit 11109c3854
4 changed files with 53 additions and 74 deletions
+7 -14
View File
@@ -6,7 +6,7 @@ import {useQueryClient} from '@tanstack/react-query'
import {useAccountSwitcher} from '#/lib/hooks/useAccountSwitcher' import {useAccountSwitcher} from '#/lib/hooks/useAccountSwitcher'
import {NavigationProp} from '#/lib/routes/types' import {NavigationProp} from '#/lib/routes/types'
import {logEvent} from '#/lib/statsig/statsig' import {logEvent} from '#/lib/statsig/statsig'
import {logger} from '#/logger' import {Logger} from '#/logger'
import {isAndroid} from '#/platform/detection' import {isAndroid} from '#/platform/detection'
import {useCurrentConvoId} from '#/state/messages/current-convo-id' import {useCurrentConvoId} from '#/state/messages/current-convo-id'
import {RQKEY as RQKEY_NOTIFS} from '#/state/queries/notifications/feed' import {RQKEY as RQKEY_NOTIFS} from '#/state/queries/notifications/feed'
@@ -50,6 +50,8 @@ const DEFAULT_HANDLER_OPTIONS = {
let storedPayload: NotificationPayload | undefined let storedPayload: NotificationPayload | undefined
let prevDate = 0 let prevDate = 0
const logger = Logger.create(Logger.Context.notifications)
export function useNotificationsHandler() { export function useNotificationsHandler() {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const {currentAccount, accounts} = useSession() const {currentAccount, accounts} = useSession()
@@ -186,11 +188,7 @@ export function useNotificationsHandler() {
return DEFAULT_HANDLER_OPTIONS return DEFAULT_HANDLER_OPTIONS
} }
logger.debug( logger.debug('Notifications: received', {e})
'Notifications: received',
{e},
logger.DebugContext.notifications,
)
const payload = e.request.trigger.payload as NotificationPayload const payload = e.request.trigger.payload as NotificationPayload
if ( if (
@@ -217,13 +215,9 @@ export function useNotificationsHandler() {
} }
prevDate = e.notification.date prevDate = e.notification.date
logger.debug( logger.debug('Notifications: response received', {
'Notifications: response received', actionIdentifier: e.actionIdentifier,
{ })
actionIdentifier: e.actionIdentifier,
},
logger.DebugContext.notifications,
)
if ( if (
e.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER && e.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER &&
@@ -235,7 +229,6 @@ export function useNotificationsHandler() {
logger.debug( logger.debug(
'User pressed a notification, opening notifications tab', 'User pressed a notification, opening notifications tab',
{}, {},
logger.DebugContext.notifications,
) )
logEvent('notifications:openApp', {}) logEvent('notifications:openApp', {})
invalidateCachedUnreadPage() invalidateCachedUnreadPage()
+7 -9
View File
@@ -4,11 +4,13 @@ import {getBadgeCountAsync, setBadgeCountAsync} from 'expo-notifications'
import {BskyAgent} from '@atproto/api' import {BskyAgent} from '@atproto/api'
import {logEvent} from '#/lib/statsig/statsig' import {logEvent} from '#/lib/statsig/statsig'
import {logger} from '#/logger' import {Logger} from '#/logger'
import {devicePlatform, isAndroid, isNative} from '#/platform/detection' import {devicePlatform, isAndroid, isNative} from '#/platform/detection'
import {SessionAccount, useAgent, useSession} from '#/state/session' import {SessionAccount, useAgent, useSession} from '#/state/session'
import BackgroundNotificationHandler from '../../../modules/expo-background-notification-handler' import BackgroundNotificationHandler from '../../../modules/expo-background-notification-handler'
const logger = Logger.create(Logger.Context.notifications)
const SERVICE_DID = (serviceUrl?: string) => const SERVICE_DID = (serviceUrl?: string) =>
serviceUrl?.includes('staging') serviceUrl?.includes('staging')
? 'did:web:api.staging.bsky.dev' ? 'did:web:api.staging.bsky.dev'
@@ -26,14 +28,10 @@ async function registerPushToken(
token: token.data, token: token.data,
appId: 'xyz.blueskyweb.app', appId: 'xyz.blueskyweb.app',
}) })
logger.debug( logger.debug('sent push token (init)', {
'Notifications: Sent push token (init)', tokenType: token.type,
{ token: token.data,
tokenType: token.type, })
token: token.data,
},
logger.DebugContext.notifications,
)
} catch (error) { } catch (error) {
logger.error('Notifications: Failed to set push token', {message: error}) logger.error('Notifications: Failed to set push token', {message: error})
} }
+24 -32
View File
@@ -10,7 +10,7 @@ import EventEmitter from 'eventemitter3'
import {nanoid} from 'nanoid/non-secure' import {nanoid} from 'nanoid/non-secure'
import {networkRetry} from '#/lib/async/retry' import {networkRetry} from '#/lib/async/retry'
import {logger} from '#/logger' import {Logger} from '#/logger'
import {isNative} from '#/platform/detection' import {isNative} from '#/platform/detection'
import { import {
ACTIVE_POLL_INTERVAL, ACTIVE_POLL_INTERVAL,
@@ -34,6 +34,8 @@ import {MessagesEventBus} from '#/state/messages/events/agent'
import {MessagesEventBusError} from '#/state/messages/events/types' import {MessagesEventBusError} from '#/state/messages/events/types'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const' import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
const logger = Logger.create(Logger.Context.convo)
export function isConvoItemMessage( export function isConvoItemMessage(
item: ConvoItem, item: ConvoItem,
): item is ConvoItem & {type: 'message'} { ): item is ConvoItem & {type: 'message'} {
@@ -125,7 +127,7 @@ export class Convo {
getSnapshot(): ConvoState { getSnapshot(): ConvoState {
if (!this.snapshot) this.snapshot = this.generateSnapshot() if (!this.snapshot) this.snapshot = this.generateSnapshot()
// logger.debug('Convo: snapshotted', {}, logger.DebugContext.convo) // logger.debug('snapshotted', {})
return this.snapshot return this.snapshot
} }
@@ -375,15 +377,11 @@ export class Convo {
break break
} }
logger.debug( logger.debug(`dispatch '${action.event}'`, {
`Convo: dispatch '${action.event}'`, id: this.id,
{ prev: prevStatus,
id: this.id, next: this.status,
prev: prevStatus, })
next: this.status,
},
logger.DebugContext.convo,
)
this.updateLastActiveTimestamp() this.updateLastActiveTimestamp()
this.commit() this.commit()
@@ -454,13 +452,13 @@ export class Convo {
* Some validation prior to `Ready` status * Some validation prior to `Ready` status
*/ */
if (!this.convo) { if (!this.convo) {
throw new Error('Convo: could not find convo') throw new Error('could not find convo')
} }
if (!this.sender) { if (!this.sender) {
throw new Error('Convo: could not find sender in convo') throw new Error('could not find sender in convo')
} }
if (!this.recipients) { if (!this.recipients) {
throw new Error('Convo: could not find recipients in convo') throw new Error('could not find recipients in convo')
} }
const userIsDisabled = Boolean(this.sender.chatDisabled) const userIsDisabled = Boolean(this.sender.chatDisabled)
@@ -471,7 +469,7 @@ export class Convo {
this.dispatch({event: ConvoDispatchEvent.Ready}) this.dispatch({event: ConvoDispatchEvent.Ready})
} }
} catch (e: any) { } catch (e: any) {
logger.error(e, {context: 'Convo: setup failed'}) logger.error(e, {context: 'setup failed'})
this.dispatch({ this.dispatch({
event: ConvoDispatchEvent.Error, event: ConvoDispatchEvent.Error,
@@ -576,7 +574,7 @@ export class Convo {
this.sender = sender || this.sender this.sender = sender || this.sender
this.recipients = recipients || this.recipients this.recipients = recipients || this.recipients
} catch (e: any) { } catch (e: any) {
logger.error(e, {context: `Convo: failed to refresh convo`}) logger.error(e, {context: `failed to refresh convo`})
} }
} }
@@ -586,7 +584,7 @@ export class Convo {
} }
| undefined | undefined
async fetchMessageHistory() { async fetchMessageHistory() {
logger.debug('Convo: fetch message history', {}, logger.DebugContext.convo) logger.debug('fetch message history', {})
/* /*
* If oldestRev is null, we've fetched all history. * If oldestRev is null, we've fetched all history.
@@ -640,7 +638,7 @@ export class Convo {
} }
} }
} catch (e: any) { } catch (e: any) {
logger.error('Convo: failed to fetch message history') logger.error('failed to fetch message history')
this.fetchMessageHistoryError = { this.fetchMessageHistoryError = {
retry: () => { retry: () => {
@@ -773,7 +771,7 @@ export class Convo {
// Ignore empty messages for now since they have no other purpose atm // Ignore empty messages for now since they have no other purpose atm
if (!message.text.trim() && !message.embed) return if (!message.text.trim() && !message.embed) return
logger.debug('Convo: send message', {}, logger.DebugContext.convo) logger.debug('send message', {})
const tempId = nanoid() const tempId = nanoid()
@@ -791,9 +789,8 @@ export class Convo {
async processPendingMessages() { async processPendingMessages() {
logger.debug( logger.debug(
`Convo: processing messages (${this.pendingMessages.size} remaining)`, `processing messages (${this.pendingMessages.size} remaining)`,
{}, {},
logger.DebugContext.convo,
) )
const pendingMessage = Array.from(this.pendingMessages.values()).shift() const pendingMessage = Array.from(this.pendingMessages.values()).shift()
@@ -837,7 +834,7 @@ export class Convo {
// continue queue processing // continue queue processing
await this.processPendingMessages() await this.processPendingMessages()
} catch (e: any) { } catch (e: any) {
logger.error(e, {context: `Convo: failed to send message`}) logger.error(e, {context: `failed to send message`})
this.handleSendMessageFailure(e) this.handleSendMessageFailure(e)
this.isProcessingPendingMessages = false this.isProcessingPendingMessages = false
} }
@@ -900,9 +897,8 @@ export class Convo {
this.commit() this.commit()
logger.debug( logger.debug(
`Convo: batch retrying ${this.pendingMessages.size} pending messages`, `batch retrying ${this.pendingMessages.size} pending messages`,
{}, {},
logger.DebugContext.convo,
) )
try { try {
@@ -934,19 +930,15 @@ export class Convo {
this.commit() this.commit()
logger.debug( logger.debug(`sent ${this.pendingMessages.size} pending messages`, {})
`Convo: sent ${this.pendingMessages.size} pending messages`,
{},
logger.DebugContext.convo,
)
} catch (e: any) { } catch (e: any) {
logger.error(e, {context: `Convo: failed to batch retry messages`}) logger.error(e, {context: `failed to batch retry messages`})
this.handleSendMessageFailure(e) this.handleSendMessageFailure(e)
} }
} }
async deleteMessage(messageId: string) { async deleteMessage(messageId: string) {
logger.debug('Convo: delete message', {}, logger.DebugContext.convo) logger.debug('delete message', {})
this.deletedMessages.add(messageId) this.deletedMessages.add(messageId)
this.commit() this.commit()
@@ -962,7 +954,7 @@ export class Convo {
) )
}) })
} catch (e: any) { } catch (e: any) {
logger.error(e, {context: `Convo: failed to delete message`}) logger.error(e, {context: `failed to delete message`})
this.deletedMessages.delete(messageId) this.deletedMessages.delete(messageId)
this.commit() this.commit()
throw e throw e
+15 -19
View File
@@ -3,7 +3,7 @@ import EventEmitter from 'eventemitter3'
import {nanoid} from 'nanoid/non-secure' import {nanoid} from 'nanoid/non-secure'
import {networkRetry} from '#/lib/async/retry' import {networkRetry} from '#/lib/async/retry'
import {logger} from '#/logger' import {Logger} from '#/logger'
import { import {
BACKGROUND_POLL_INTERVAL, BACKGROUND_POLL_INTERVAL,
DEFAULT_POLL_INTERVAL, DEFAULT_POLL_INTERVAL,
@@ -18,7 +18,7 @@ import {
} from '#/state/messages/events/types' } from '#/state/messages/events/types'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const' import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
const LOGGER_CONTEXT = 'MessagesEventBus' const logger = Logger.create(Logger.Context.convo)
export class MessagesEventBus { export class MessagesEventBus {
private id: string private id: string
@@ -90,17 +90,17 @@ export class MessagesEventBus {
} }
background() { background() {
logger.debug(`${LOGGER_CONTEXT}: background`, {}, logger.DebugContext.convo) logger.debug(`background`, {})
this.dispatch({event: MessagesEventBusDispatchEvent.Background}) this.dispatch({event: MessagesEventBusDispatchEvent.Background})
} }
suspend() { suspend() {
logger.debug(`${LOGGER_CONTEXT}: suspend`, {}, logger.DebugContext.convo) logger.debug(`suspend`, {})
this.dispatch({event: MessagesEventBusDispatchEvent.Suspend}) this.dispatch({event: MessagesEventBusDispatchEvent.Suspend})
} }
resume() { resume() {
logger.debug(`${LOGGER_CONTEXT}: resume`, {}, logger.DebugContext.convo) logger.debug(`resume`, {})
this.dispatch({event: MessagesEventBusDispatchEvent.Resume}) this.dispatch({event: MessagesEventBusDispatchEvent.Resume})
} }
@@ -222,19 +222,15 @@ export class MessagesEventBus {
break break
} }
logger.debug( logger.debug(`dispatch '${action.event}'`, {
`${LOGGER_CONTEXT}: dispatch '${action.event}'`, id: this.id,
{ prev: prevStatus,
id: this.id, next: this.status,
prev: prevStatus, })
next: this.status,
},
logger.DebugContext.convo,
)
} }
private async init() { private async init() {
logger.debug(`${LOGGER_CONTEXT}: init`, {}, logger.DebugContext.convo) logger.debug(`init`, {})
try { try {
const response = await networkRetry(2, () => { const response = await networkRetry(2, () => {
@@ -258,7 +254,7 @@ export class MessagesEventBus {
this.dispatch({event: MessagesEventBusDispatchEvent.Ready}) this.dispatch({event: MessagesEventBusDispatchEvent.Ready})
} catch (e: any) { } catch (e: any) {
logger.error(e, { logger.error(e, {
context: `${LOGGER_CONTEXT}: init failed`, context: `init failed`,
}) })
this.dispatch({ this.dispatch({
@@ -321,7 +317,7 @@ export class MessagesEventBus {
this.isPolling = true this.isPolling = true
// logger.debug( // logger.debug(
// `${LOGGER_CONTEXT}: poll`, // `poll`,
// { // {
// requestedPollIntervals: Array.from( // requestedPollIntervals: Array.from(
// this.requestedPollIntervals.values(), // this.requestedPollIntervals.values(),
@@ -372,12 +368,12 @@ export class MessagesEventBus {
this.emitter.emit('event', {type: 'logs', logs: batch}) this.emitter.emit('event', {type: 'logs', logs: batch})
} catch (e: any) { } catch (e: any) {
logger.error(e, { logger.error(e, {
context: `${LOGGER_CONTEXT}: process latest events`, context: `process latest events`,
}) })
} }
} }
} catch (e: any) { } catch (e: any) {
logger.error(e, {context: `${LOGGER_CONTEXT}: poll events failed`}) logger.error(e, {context: `poll events failed`})
this.dispatch({ this.dispatch({
event: MessagesEventBusDispatchEvent.Error, event: MessagesEventBusDispatchEvent.Error,