Skip to content

Commit

Permalink
Merge pull request #1017 from interval/quiet-log-level
Browse files Browse the repository at this point in the history
Use/document more specific log methods (info, warn, etc), add "quiet"
  • Loading branch information
jacobmischka authored Dec 22, 2022
2 parents 48209fc + 0056d02 commit 612c7ae
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 29 deletions.
14 changes: 7 additions & 7 deletions src/classes/IntervalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ export default class IntervalClient {
})
.catch(async err => {
if (err instanceof IOError) {
this.#logger.error(
this.#logger.warn(
'Failed resending pending IO call: ',
err.kind
)
Expand Down Expand Up @@ -532,7 +532,7 @@ export default class IntervalClient {
})
.catch(async err => {
if (err instanceof IOError) {
this.#logger.error(
this.#logger.warn(
'Failed resending transaction loading state: ',
err.kind
)
Expand Down Expand Up @@ -653,14 +653,14 @@ export default class IntervalClient {
} catch (err) {
this.#logger.warn('Pong not received in time')
if (!(err instanceof TimeoutError)) {
this.#logger.error(err)
this.#logger.warn(err)
}

if (
lastSuccessfulPing.getTime() <
new Date().getTime() - this.#closeUnresponsiveConnectionTimeoutMs
) {
this.#logger.error(
this.#logger.warn(
'No pong received in last three minutes, closing connection to Interval and retrying...'
)
if (this.#pingIntervalHandle) {
Expand Down Expand Up @@ -767,7 +767,7 @@ export default class IntervalClient {
inputs.type as DescriptionType
)
} else {
this.#logger.warn(
this.#logger.debug(
'INITIALIZE_PEER_CONNECTION:',
'DCC not found for inputs',
inputs
Expand All @@ -794,7 +794,7 @@ export default class IntervalClient {
}
}
} catch (err) {
this.#logger.error('Failed initializing peer connection', err)
this.#logger.warn('Failed initializing peer connection', err)
}
},
START_TRANSACTION: async inputs => {
Expand Down Expand Up @@ -1213,7 +1213,7 @@ export default class IntervalClient {
pageSendTimeout = null
sendPagePromise = sendPage()
.catch(err => {
this.#logger.error(`Failed sending page with key ${pageKey}`, err)
this.#logger.debug(`Failed sending page with key ${pageKey}`, err)
})
.finally(() => {
sendPagePromise = null
Expand Down
66 changes: 51 additions & 15 deletions src/classes/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,66 @@ import {
} from '../utils/packageManager'
import * as pkg from '../../package.json'

export type LogLevel = 'prod' | 'debug'
export type LogLevel =
| 'quiet'
| 'info'
| 'prod' /* @deprecated, alias for 'info' */
| 'debug'

export const CHANGELOG_URL = 'https://interval.com/changelog'

export default class Logger {
logLevel: LogLevel = 'prod'
logLevel: LogLevel = 'info'

constructor(logLevel?: LogLevel) {
if (logLevel) {
this.logLevel = logLevel
}
}

/* Important messages, always emitted */
prod(...args: any[]) {
console.log('[Interval] ', ...args)
}

warn(...args: any[]) {
console.warn('[Interval] ', ...args)
/* Same as prod, but without the [Interval] prefix */
prodNoPrefix(...args: any[]) {
console.log(...args)
}

/* Fatal errors or errors in user code, always emitted */
error(...args: any[]) {
console.error('[Interval] ', ...args)
}

/* Informational messages, not emitted in "quiet" logLevel */
info(...args: any[]) {
if (this.logLevel !== 'quiet') {
console.info('[Interval] ', ...args)
}
}

/* Same as info, but without the [Interval] prefix */
infoNoPrefix(...args: any[]) {
console.log(...args)
}

/* Non-fatal warnings, not emitted in "quiet" logLevel */
warn(...args: any[]) {
if (this.logLevel !== 'quiet') {
console.warn('[Interval] ', ...args)
}
}

/* Debugging/tracing information, only emitted in "debug" logLevel */
debug(...args: any[]) {
if (this.logLevel === 'debug') {
console.debug('[Interval] ', ...args)
}
}

handleSdkAlert(sdkAlert: SdkAlert) {
console.log('')
console.info('')

const WARN_EMOJI = '\u26A0\uFE0F'
const ERROR_EMOJI = '‼️'
Expand All @@ -46,30 +73,39 @@ export default class Logger {

switch (severity) {
case 'INFO':
this.prod('🆕\tA new Interval SDK version is available.')
this.info('🆕\tA new Interval SDK version is available.')
if (message) {
this.info(message)
}
break
case 'WARNING':
this.prod(
this.warn(
`${WARN_EMOJI}\tThis version of the Interval SDK has been deprecated. Please update as soon as possible, it will not work in a future update.`
)
if (message) {
this.warn(message)
}
break
case 'ERROR':
this.prod(
this.error(
`${ERROR_EMOJI}\tThis version of the Interval SDK is no longer supported. Your app will not work until you update.`
)
if (message) {
this.error(message)
}
break
default:
if (message) {
this.prod(message)
}
}

if (message) {
this.prod(message)
}

this.prod("\t- See what's new at:", CHANGELOG_URL)
this.prod(
this.info("\t- See what's new at:", CHANGELOG_URL)
this.info(
'\t- Update now by running:',
getInstallCommand(`${pkg.name}@latest`, detectPackageManager())
)

console.log('')
console.info('')
}
}
2 changes: 1 addition & 1 deletion src/classes/TransactionLoadingState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class TransactionLoadingState {
async completeOne() {
if (!this.#state || !this.#state.itemsInQueue) {
this.#logger.warn(
'Please call `loading.start` with `itemsInQueue` before `loading.completeOne`, failing to do so does nothing.'
'Please call `loading.start` with `itemsInQueue` before `loading.completeOne`, nothing to complete.'
)
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/displayTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function displayTable(logger: Logger) {
props: PublicProps<Row>
) {
const initialColumns = columnsBuilder(props, column =>
logger.error(missingColumnMessage('io.display.table')(column))
logger.warn(missingColumnMessage('io.display.table')(column))
)

// Rendering all rows on initialization is necessary for filtering and sorting
Expand Down Expand Up @@ -74,7 +74,7 @@ export default function displayTable(logger: Logger) {
data,
},
column =>
logger.error(missingColumnMessage('io.display.table')(column))
logger.warn(missingColumnMessage('io.display.table')(column))
)
serializedData = data.map((row, index) =>
tableRowSerializer({
Expand Down
2 changes: 1 addition & 1 deletion src/components/selectTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function selectTable(logger: Logger) {
type DataList = typeof props['data']

const columns = columnsBuilder(props, column =>
logger.error(missingColumnMessage('io.select.table')(column))
logger.warn(missingColumnMessage('io.select.table')(column))
)

// Rendering all rows on initialization is necessary for filtering and sorting
Expand Down
1 change: 1 addition & 0 deletions src/components/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function file(logger: Logger) {
const urls = await generatePresignedUrls(newState)
return urls
} catch (error) {
// TODO: We should not swallow this error after merging #1012
logger.error(
'An error was unexpectedly thrown from the `generatePresignedUrls` function:'
)
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod'
import fetch from 'cross-fetch'
import Routes from './classes/Routes'
import IOError from './classes/IOError'
import Logger from './classes/Logger'
import Logger, { LogLevel } from './classes/Logger'
import Page from './classes/Page'
import {
NOTIFY,
Expand Down Expand Up @@ -54,7 +54,7 @@ export interface InternalConfig {
// TODO: Mark as deprecated soon, remove soon afterward
groups?: Record<string, Page>
endpoint?: string
logLevel?: 'prod' | 'debug'
logLevel?: LogLevel
retryIntervalMs?: number
retryChunkIntervalMs?: number
pingIntervalMs?: number
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fileActionLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function loadFolder(currentDirectory: string, logger: Logger) {
}
}
} catch (err) {
logger.error(
logger.warn(
`Failed loading file at ${fullPath} as module, skipping.`,
err
)
Expand Down

0 comments on commit 612c7ae

Please sign in to comment.