Skip to content

Commit

Permalink
Replace all logs with new logger
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Nov 4, 2023
1 parent e49a3d8 commit f51351e
Show file tree
Hide file tree
Showing 66 changed files with 301 additions and 230 deletions.
11 changes: 6 additions & 5 deletions src/lib/analytics/analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {RootStoreModel, AppInfo} from 'state/models/root-store'
import {useStores} from 'state/models/root-store'
import {sha256} from 'js-sha256'
import {ScreenEvent, TrackEvent} from './types'
import {logger} from '#/logger'

const segmentClient = createClient({
writeKey: '8I6DsgfiSLuoONyaunGoiQM7A6y2ybdI',
Expand Down Expand Up @@ -54,9 +55,9 @@ export function init(store: RootStoreModel) {
if (sess.did) {
const did_hashed = sha256(sess.did)
segmentClient.identify(did_hashed, {did_hashed})
store.log.debug('Ping w/hash')
logger.debug('Ping w/hash')
} else {
store.log.debug('Ping w/o hash')
logger.debug('Ping w/o hash')
segmentClient.identify()
}
}
Expand All @@ -68,19 +69,19 @@ export function init(store: RootStoreModel) {
// -prf
segmentClient.isReady.onChange(() => {
if (AppState.currentState !== 'active') {
store.log.debug('Prevented a metrics ping while the app was backgrounded')
logger.debug('Prevented a metrics ping while the app was backgrounded')
return
}
const context = segmentClient.context.get()
if (typeof context?.app === 'undefined') {
store.log.debug('Aborted metrics ping due to unavailable context')
logger.debug('Aborted metrics ping due to unavailable context')
return
}

const oldAppInfo = store.appInfo
const newAppInfo = context.app as AppInfo
store.setAppInfo(newAppInfo)
store.log.debug('Recording app info', {new: newAppInfo, old: oldAppInfo})
logger.debug('Recording app info', {new: newAppInfo, old: oldAppInfo})

if (typeof oldAppInfo === 'undefined') {
if (store.session.hasSession) {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/analytics/analytics.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {RootStoreModel} from 'state/models/root-store'
import {useStores} from 'state/models/root-store'
import {sha256} from 'js-sha256'
import {logger} from '#/logger'

const segmentClient = createClient(
{
Expand Down Expand Up @@ -49,9 +50,9 @@ export function init(store: RootStoreModel) {
if (sess.did) {
const did_hashed = sha256(sess.did)
segmentClient.identify(did_hashed, {did_hashed})
store.log.debug('Ping w/hash')
logger.debug('Ping w/hash')
} else {
store.log.debug('Ping w/o hash')
logger.debug('Ping w/o hash')
segmentClient.identify()
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {LinkMeta} from '../link-meta/link-meta'
import {isWeb} from 'platform/detection'
import {ImageModel} from 'state/models/media/image'
import {shortenLinks} from 'lib/strings/rich-text-manip'
import {logger} from '#/logger'

export interface ExternalEmbedDraft {
uri: string
Expand Down Expand Up @@ -178,7 +179,7 @@ export async function post(store: RootStoreModel, opts: PostOpts) {
) {
encoding = 'image/jpeg'
} else {
store.log.warn('Unexpected image format for thumbnail, skipping', {
logger.warn('Unexpected image format for thumbnail, skipping', {
thumbnail: opts.extLink.localThumb.path,
})
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/hooks/useFollowProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import {AppBskyActorDefs} from '@atproto/api'
import {useStores} from 'state/index'
import {FollowState} from 'state/models/cache/my-follows'
import {logger} from '#/logger'

export function useFollowProfile(profile: AppBskyActorDefs.ProfileViewBasic) {
const store = useStores()
Expand All @@ -22,7 +23,7 @@ export function useFollowProfile(profile: AppBskyActorDefs.ProfileViewBasic) {
following: false,
}
} catch (e: any) {
store.log.error('Failed to delete follow', {error: e})
logger.error('Failed to delete follow', {error: e})
throw e
}
} else if (state === FollowState.NotFollowing) {
Expand All @@ -40,7 +41,7 @@ export function useFollowProfile(profile: AppBskyActorDefs.ProfileViewBasic) {
following: true,
}
} catch (e: any) {
store.log.error('Failed to create follow', {error: e})
logger.error('Failed to create follow', {error: e})
throw e
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/lib/hooks/useOTAUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Updates from 'expo-updates'
import {useCallback, useEffect} from 'react'
import {AppState} from 'react-native'
import {useStores} from 'state/index'
import {logger} from '#/logger'

export function useOTAUpdate() {
const store = useStores()
Expand All @@ -21,7 +22,7 @@ export function useOTAUpdate() {
})
}, [store.shell])
const checkForUpdate = useCallback(async () => {
store.log.debug('useOTAUpdate: Checking for update...')
logger.debug('useOTAUpdate: Checking for update...')
try {
// Check if new OTA update is available
const update = await Updates.checkForUpdateAsync()
Expand All @@ -34,16 +35,16 @@ export function useOTAUpdate() {
// show a popup modal
showUpdatePopup()
} catch (e) {
store.log.error('useOTAUpdate: Error while checking for update', {
logger.error('useOTAUpdate: Error while checking for update', {
error: e,
})
}
}, [showUpdatePopup, store.log])
}, [showUpdatePopup])
const updateEventListener = useCallback(
(event: Updates.UpdateEvent) => {
store.log.debug('useOTAUpdate: Listening for update...')
logger.debug('useOTAUpdate: Listening for update...')
if (event.type === Updates.UpdateEventType.ERROR) {
store.log.error('useOTAUpdate: Error while listening for update', {
logger.error('useOTAUpdate: Error while listening for update', {
message: event.message,
})
} else if (event.type === Updates.UpdateEventType.NO_UPDATE_AVAILABLE) {
Expand All @@ -55,7 +56,7 @@ export function useOTAUpdate() {
showUpdatePopup()
}
},
[showUpdatePopup, store.log],
[showUpdatePopup],
)

useEffect(() => {
Expand Down
61 changes: 44 additions & 17 deletions src/lib/notifications/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {RootStoreModel} from '../../state'
import {resetToTab} from '../../Navigation'
import {devicePlatform, isIOS} from 'platform/detection'
import {track} from 'lib/analytics/analytics'
import {logger} from '#/logger'

const SERVICE_DID = (serviceUrl?: string) =>
serviceUrl?.includes('staging')
Expand All @@ -29,19 +30,27 @@ export function init(store: RootStoreModel) {
token: token.data,
appId: 'xyz.blueskyweb.app',
})
store.log.debug('Notifications: Sent push token (init)', {
tokenType: token.type,
token: token.data,
})
logger.debug(
'Notifications: Sent push token (init)',
{
tokenType: token.type,
token: token.data,
},
logger.DebugContext.notifications,
)
} catch (error) {
store.log.error('Notifications: Failed to set push token', {error})
logger.error('Notifications: Failed to set push token', {error})
}
}

// listens for new changes to the push token
// In rare situations, a push token may be changed by the push notification service while the app is running. When a token is rolled, the old one becomes invalid and sending notifications to it will fail. A push token listener will let you handle this situation gracefully by registering the new token with your backend right away.
Notifications.addPushTokenListener(async ({data: t, type}) => {
store.log.debug('Notifications: Push token changed', {t, tokenType: type})
logger.debug(
'Notifications: Push token changed',
{t, tokenType: type},
logger.DebugContext.notifications,
)
if (t) {
try {
await store.agent.api.app.bsky.notification.registerPush({
Expand All @@ -50,20 +59,28 @@ export function init(store: RootStoreModel) {
token: t,
appId: 'xyz.blueskyweb.app',
})
store.log.debug('Notifications: Sent push token (event)', {
tokenType: type,
token: t,
})
logger.debug(
'Notifications: Sent push token (event)',
{
tokenType: type,
token: t,
},
logger.DebugContext.notifications,
)
} catch (error) {
store.log.error('Notifications: Failed to set push token', {error})
logger.error('Notifications: Failed to set push token', {error})
}
}
})
})

// handle notifications that are received, both in the foreground or background
Notifications.addNotificationReceivedListener(event => {
store.log.debug('Notifications: received', {event})
logger.debug(
'Notifications: received',
{event},
logger.DebugContext.notifications,
)
if (event.request.trigger.type === 'push') {
// refresh notifications in the background
store.me.notifications.syncQueue()
Expand All @@ -75,7 +92,11 @@ export function init(store: RootStoreModel) {
// TODO: handle android payload deeplink
}
if (payload) {
store.log.debug('Notifications: received payload', payload)
logger.debug(
'Notifications: received payload',
payload,
logger.DebugContext.notifications,
)
// TODO: deeplink notif here
}
}
Expand All @@ -84,14 +105,20 @@ export function init(store: RootStoreModel) {
// handle notifications that are tapped on
const sub = Notifications.addNotificationResponseReceivedListener(
response => {
store.log.debug('Notifications: response received', {
actionIdentifier: response.actionIdentifier,
})
logger.debug(
'Notifications: response received',
{
actionIdentifier: response.actionIdentifier,
},
logger.DebugContext.notifications,
)
if (
response.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER
) {
store.log.debug(
logger.debug(
'User pressed a notification, opening notifications tab',
{},
logger.DebugContext.notifications,
)
track('Notificatons:OpenApp')
store.me.notifications.refresh() // refresh notifications
Expand Down
2 changes: 2 additions & 0 deletions src/logger/debugContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
*/
export const DebugContext = {
// e.g. composer: 'composer'
session: 'session',
notifications: 'notifications',
} as const
5 changes: 3 additions & 2 deletions src/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {BskyAgent} from '@atproto/api'
import {RootStoreModel} from './models/root-store'
import * as apiPolyfill from 'lib/api/api-polyfill'
import * as storage from 'lib/storage'
import {logger} from '#/logger'

export const LOCAL_DEV_SERVICE =
Platform.OS === 'android' ? 'http://10.0.2.2:2583' : 'http://localhost:2583'
Expand All @@ -22,10 +23,10 @@ export async function setupState(serviceUri = DEFAULT_SERVICE) {
rootStore = new RootStoreModel(new BskyAgent({service: serviceUri}))
try {
data = (await storage.load(ROOT_STATE_STORAGE_KEY)) || {}
rootStore.log.debug('Initial hydrate', {hasSession: !!data.session})
logger.debug('Initial hydrate', {hasSession: !!data.session})
rootStore.hydrate(data)
} catch (e: any) {
rootStore.log.error('Failed to load state from storage', {error: e})
logger.error('Failed to load state from storage', {error: e})
}
rootStore.attemptSessionResumption()

Expand Down
11 changes: 6 additions & 5 deletions src/state/models/content/feed-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {sanitizeHandle} from 'lib/strings/handles'
import {bundleAsync} from 'lib/async/bundle'
import {cleanError} from 'lib/strings/errors'
import {track} from 'lib/analytics/analytics'
import {logger} from '#/logger'

export class FeedSourceModel {
// state
Expand Down Expand Up @@ -134,7 +135,7 @@ export class FeedSourceModel {
try {
await this.rootStore.preferences.addSavedFeed(this.uri)
} catch (error) {
this.rootStore.log.error('Failed to save feed', {error})
logger.error('Failed to save feed', {error})
} finally {
track('CustomFeed:Save')
}
Expand All @@ -147,7 +148,7 @@ export class FeedSourceModel {
try {
await this.rootStore.preferences.removeSavedFeed(this.uri)
} catch (error) {
this.rootStore.log.error('Failed to unsave feed', {error})
logger.error('Failed to unsave feed', {error})
} finally {
track('CustomFeed:Unsave')
}
Expand All @@ -157,7 +158,7 @@ export class FeedSourceModel {
try {
await this.rootStore.preferences.addPinnedFeed(this.uri)
} catch (error) {
this.rootStore.log.error('Failed to pin feed', {error})
logger.error('Failed to pin feed', {error})
} finally {
track('CustomFeed:Pin', {
name: this.displayName,
Expand Down Expand Up @@ -194,7 +195,7 @@ export class FeedSourceModel {
} catch (e: any) {
this.likeUri = undefined
this.likeCount = (this.likeCount || 1) - 1
this.rootStore.log.error('Failed to like feed', {error: e})
logger.error('Failed to like feed', {error: e})
} finally {
track('CustomFeed:Like')
}
Expand All @@ -215,7 +216,7 @@ export class FeedSourceModel {
} catch (e: any) {
this.likeUri = uri
this.likeCount = (this.likeCount || 0) + 1
this.rootStore.log.error('Failed to unlike feed', {error: e})
logger.error('Failed to unlike feed', {error: e})
} finally {
track('CustomFeed:Unlike')
}
Expand Down
7 changes: 4 additions & 3 deletions src/state/models/content/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {cleanError} from 'lib/strings/errors'
import {bundleAsync} from 'lib/async/bundle'
import {track} from 'lib/analytics/analytics'
import {until} from 'lib/async/until'
import {logger} from '#/logger'

const PAGE_SIZE = 30

Expand Down Expand Up @@ -339,7 +340,7 @@ export class ListModel {
try {
await this.rootStore.preferences.addPinnedFeed(this.uri)
} catch (error) {
this.rootStore.log.error('Failed to pin feed', {error})
logger.error('Failed to pin feed', {error})
} finally {
track('CustomFeed:Pin', {
name: this.data?.name || '',
Expand Down Expand Up @@ -455,10 +456,10 @@ export class ListModel {
this.error = cleanError(err)
this.loadMoreError = cleanError(loadMoreErr)
if (err) {
this.rootStore.log.error('Failed to fetch user items', {error: err})
logger.error('Failed to fetch user items', {error: err})
}
if (loadMoreErr) {
this.rootStore.log.error('Failed to fetch user items', {
logger.error('Failed to fetch user items', {
error: loadMoreErr,
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/state/models/content/post-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as apilib from 'lib/api/index'
import {cleanError} from 'lib/strings/errors'
import {ThreadViewPreference} from '../ui/preferences'
import {PostThreadItemModel} from './post-thread-item'
import {logger} from '#/logger'

export class PostThreadModel {
// state
Expand Down Expand Up @@ -163,7 +164,7 @@ export class PostThreadModel {
this.hasLoaded = true
this.error = cleanError(err)
if (err) {
this.rootStore.log.error('Failed to fetch post thread', {error: err})
logger.error('Failed to fetch post thread', {error: err})
}
this.notFound = err instanceof GetPostThread.NotFoundError
}
Expand Down
Loading

0 comments on commit f51351e

Please sign in to comment.