Skip to content

Commit

Permalink
feat(log): remove logging on production
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyupe committed Apr 26, 2022
1 parent 466da89 commit 5955d18
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "matcha-overlay",
"version": "3.1.0",
"version": "3.2.0",
"private": true,
"dependencies": {
"axios": "^0.21.1",
Expand Down
17 changes: 9 additions & 8 deletions src/app/overlay/map-event/data-source/post-moogle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { List } from 'immutable'
import { speak } from '../../../../lib/tts'
import { FFXIVFate } from '../../../../data/fates'
import { track } from '../../../../lib/track'
import { debug } from '../../../../lib/log'

export interface FateWatchListChangedDTO {
world: number
Expand Down Expand Up @@ -116,14 +117,14 @@ const connect = debounce(async (cb: (err: Error | null, conn: NatsConnection, cl
const error = await conn.closed()
if (error) {
console.error(error)
console.log(`[nats] Connection closed with error, retry in 10s`)
debug(`[nats] Connection closed with error, retry in 10s`)

setTimeout(() => connect(cb), 10e3)
} else if (!manuallyClosed) {
console.log(`[nats] Connection unexpectly closed, retry`)
debug(`[nats] Connection unexpectly closed, retry`)
void connect(cb)
} else {
console.log(`[nats] Connection closed`)
debug(`[nats] Connection closed`)
}
})

Expand All @@ -145,14 +146,14 @@ function useConnection(enabled: boolean, topics: string[], handler: (message: Ms

closeConnection = _close

console.log(`[nats] New connection established. Clearing subscribed.`)
debug(`[nats] New connection established. Clearing subscribed.`)
subscribed.clear()
setConnection(conn)
})

return () => {
if (closeConnection) {
console.log(`[nats] Closing connection.`)
debug(`[nats] Closing connection.`)
closeConnection()
}
}
Expand All @@ -166,7 +167,7 @@ function useConnection(enabled: boolean, topics: string[], handler: (message: Ms
for (const [key, sub] of subscribed) {
if (topics.includes(key)) continue

console.log(`[nats][topic:${key}] Unsubscribing ...`)
debug(`[nats][topic:${key}] Unsubscribing ...`)
subscribed.delete(key)
sub.unsubscribe()
}
Expand All @@ -175,14 +176,14 @@ function useConnection(enabled: boolean, topics: string[], handler: (message: Ms
if (subscribed.has(key)) continue

const sub = connection.subscribe(key)
console.log(`[nats][topic:${key}] Subscribing ...`)
debug(`[nats][topic:${key}] Subscribing ...`)
subscribed.set(key, sub)
;(async () => {
for await (const m of sub) {
handlerRef.current(m)
}
})().catch((err) => {
console.log(`[nats][topic:${key}] Error: ${err.code} ${err.message}`)
debug(`[nats][topic:${key}] Error: ${err.code} ${err.message}`)
subscribed.delete(key)
})
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable no-redeclare */
import { Dispatch, SetStateAction, useMemo } from 'react'
import { atom, RecoilState, useRecoilState } from 'recoil'
import { debug } from './log'

export function getConfig<T = unknown>(key: string, defaultValue?: T): T | undefined {
console.log('[config:get]', key)
debug('[config:get]', key)
const stored = window.localStorage.getItem(`config:${key}`)
if (stored) {
try {
Expand All @@ -26,7 +27,7 @@ export function getConfigWithInit<T = unknown>(key: string, defaultValue: () =>
}

export function setConfig<T = unknown>(key: string, value: T): void {
console.log('[config:set]', key, value)
debug('[config:set]', key, value)
window.localStorage.setItem(`config:${key}`, JSON.stringify(value))
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const showDebugLogs = ['localhost', '127.0.0.1'].includes(window.location.hostname)
export function debug(...args: any[]) {
showDebugLogs && console.log(...args)
}

0 comments on commit 5955d18

Please sign in to comment.