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