Skip to content

Commit

Permalink
🎨 custom logging
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Feb 2, 2025
1 parent 9e90476 commit 396eb57
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/runtime/care.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import log from 'consola'
import type { ModuleOptions as Config } from '../module'

const signature = '[`fume`.`care`]'
const info = (msg: string) => log.info(`${signature} ${msg}`)
const warn = (msg: string) => log.warn(`${signature} ${msg}`)
const success = (msg: string) => log.success(`${signature} ${msg}`)

export const configDefaults = {
env: 'development',
domain: 'https://fume.care',
Expand All @@ -10,23 +15,26 @@ export const validKey = (config: Config): boolean =>
config && typeof config.key === 'string' && /^[a-z0-9]{32}$/i.test(config.key)

export const checkConfig = (config: Config): boolean =>
validKey(config) && config.env !== 'development'
validKey(config) && config.env !== 'development' && config.env !== ''

export const reportConfig = (config: Config) => {
if (!config.key) {
log.info('[fume.care] no key detected - reporting disabled')
info('no key detected - reporting disabled')
}
else if (!validKey(config)) {
log.warn('[fume.care] key is invalid - reporting disabled')
warn('key is invalid - reporting disabled')
}
else if (config.env === 'development') {
log.info('[fume.care] development or undetected environment - reporting disabled')
info('development environment detected - reporting disabled')
}
else if (config.env === '') {
info('undetected environment - reporting disabled')
}
else {
log.success(`[fume.care] Valid API key found - reporting enabled for \`${config.env}\` environment`)
success(`valid API key found - reporting enabled for \`${config.env}\` environment`)
}

if (checkConfig(config) && config.log) {
log.info('[fume.care] logging enabled - error details will be printed')
info('logging enabled - error details will be printed')
}
}

0 comments on commit 396eb57

Please sign in to comment.