Skip to content

Commit

Permalink
feat: add configurable logging output with ENABLE_LOGGING
Browse files Browse the repository at this point in the history
The default log behaviour causes significant console spam over time.
It's now off by default but can be turned on by providing any value to
the environment variable "ENABLE_LOGGING".
  • Loading branch information
JackCuthbert committed Feb 5, 2020
1 parent 0a047dd commit 7e41df0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Variable | Default | Description
`UPDATE_WEEKENDS` | `undefined` | Provide any value to enable status updates during the weekend
`UPDATE_EXPIRATION` | `10` | The time in minutes to use as a default status expiration length
`SENTRY_DSN` | `undefined` | Optionally provide a Sentry DSN to enable error reporting
`ENABLE_LOGGING` | `undefined` | Enable verbose console output

## Hosting

Expand Down
6 changes: 5 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const {
UPDATE_INTERVAL = '1',
UPDATE_EXPIRATION = '10',
UPDATE_WEEKENDS,
SENTRY_DSN
SENTRY_DSN,
ENABLE_LOGGING = ''
} = process.env

export const lastFM = {
Expand Down Expand Up @@ -42,4 +43,7 @@ export const updateWeekends = !!UPDATE_WEEKENDS
/** Time in minutes to use as a default expiration time */
export const updateExpiration = Number(UPDATE_EXPIRATION)

/** Whether or not logging has been enabled */
export const loggingEnabled = ENABLE_LOGGING !== ''

export const tz = TZ
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ async function loop () {

validateConfig(config)
.then(enableErrorTracking)
.then(() => {
log('slack-fm ready', 'bot', true)
})
.then(main)
.then(loop)
.catch(handleError)
7 changes: 5 additions & 2 deletions src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from 'chalk'
import { loggingEnabled } from '../config'

const contexts = {
lastfm: chalk.red('[LastFM]'),
Expand All @@ -8,6 +9,8 @@ const contexts = {

type LogContext = keyof typeof contexts

export function log (message: string, ctx: LogContext = 'bot') {
console.log(`${contexts[ctx]} ${message}`)
export function log (message: string, ctx: LogContext = 'bot', force = false) {
if (loggingEnabled || force === true) {
console.log(`${contexts[ctx]} ${message}`)
}
}

0 comments on commit 7e41df0

Please sign in to comment.