Skip to content

Commit

Permalink
feat(Logger): Update RNDS_LOG_LEVEL env var
Browse files Browse the repository at this point in the history
  • Loading branch information
m7kvqbe1 committed Jun 7, 2022
1 parent 08bb001 commit da12d80
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/react-component-library/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module.exports = {
replace: 'replacement',
},
{
search: /__RNDS_LOG_LEVEL/,
replace: process.env.RNDS_LOG_LEVEL || 'warn',
search: /__MODDS_LOG_LEVEL/,
replace: process.env.MODDS_LOG_LEVEL || 'warn',
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion packages/react-component-library/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
process.env.RNDS_LOG_LEVEL = 'debug'
process.env.MODDS_LOG_LEVEL = 'debug'

module.exports = {
moduleNameMapper: {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-component-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
"build": "run-s build:es build:cjs types",
"build:cjs": "BABEL_ENV=cjs webpack --config=webpack/prod.js",
"build:es": "BABEL_ENV=es babel src --out-dir \"dist/es\" --extensions \".ts,.tsx\"",
"build:cjs:dev": "BABEL_ENV=cjs RNDS_LOG_LEVEL=debug webpack --config=webpack/dev.js",
"build:cjs:dev": "BABEL_ENV=cjs MODDS_LOG_LEVEL=debug webpack --config=webpack/dev.js",
"chromatic": "chromatic --build-script-name storybook:static",
"lint": "eslint src cypress --ext .js --ext .jsx --ext .ts --ext .tsx",
"prepare": "yarn build",
"storybook": "BABEL_ENV=es RNDS_LOG_LEVEL=debug start-storybook -p 6006",
"storybook": "BABEL_ENV=es MODDS_LOG_LEVEL=debug start-storybook -p 6006",
"storybook:static": "BABEL_ENV=es build-storybook -c .storybook -o .static_storybook",
"storybook:test": "BABEL_ENV=storybook-test RNDS_LOG_LEVEL=debug start-storybook -p 6006",
"storybook:test": "BABEL_ENV=storybook-test MODDS_LOG_LEVEL=debug start-storybook -p 6006",
"test": "BABEL_ENV=test jest --testPathIgnorePatterns=./src/a11y/index.test.tsx",
"test:a11y": "BABEL_ENV=test yarn storybook:static && jest ./src/a11y/index.test.tsx",
"test:watch": "BABEL_ENV=test jest --watch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ describe('Breadcrumbs', () => {

it('should warn the consumer `isFirst` will be overwritten', () => {
expect(consoleWarnSpy).toHaveBeenCalledWith(
'WARN - RNDS - Prop `isFirst` on `BreadcrumbsItem` will be overwritten'
'WARN - MODDS - Prop `isFirst` on `BreadcrumbsItem` will be overwritten'
)
})

it('should warn the consumer `isLast` will be overwritten', () => {
expect(consoleWarnSpy).toHaveBeenCalledWith(
'WARN - RNDS - Prop `isLast` on `BreadcrumbsItem` will be overwritten'
'WARN - MODDS - Prop `isLast` on `BreadcrumbsItem` will be overwritten'
)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('ButtonGroup', () => {
it('should warn the consumer about specifying sizes for each item', () => {
expect(consoleWarnSpy).toHaveBeenCalledTimes(2)
expect(consoleWarnSpy).toHaveBeenCalledWith(
'WARN - RNDS - Prop `size` on `ButtonGroupItem` will be replaced by `size` from `ButtonGroup`'
'WARN - MODDS - Prop `size` on `ButtonGroupItem` will be replaced by `size` from `ButtonGroup`'
)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,19 @@ describe('List', () => {

it('should warn the consumer `isActive` will be overwritten', () => {
expect(consoleWarnSpy).toHaveBeenCalledWith(
'WARN - RNDS - Prop `isActive` on `ListItem` will be overwritten'
'WARN - MODDS - Prop `isActive` on `ListItem` will be overwritten'
)
})

it('should warn the consumer `onMouseEnter` will be overwritten', () => {
expect(consoleWarnSpy).toHaveBeenCalledWith(
'WARN - RNDS - Prop `onMouseEnter` on `ListItem` will be overwritten'
'WARN - MODDS - Prop `onMouseEnter` on `ListItem` will be overwritten'
)
})

it('should warn the consumer `onMouseLeave` will be overwritten', () => {
expect(consoleWarnSpy).toHaveBeenCalledWith(
'WARN - RNDS - Prop `onMouseLeave` on `ListItem` will be overwritten'
'WARN - MODDS - Prop `onMouseLeave` on `ListItem` will be overwritten'
)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,7 @@ describe('Timeline', () => {

it('writes an error to the console', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
'ERROR - RNDS - `startDate` is after `endDate`'
'ERROR - MODDS - `startDate` is after `endDate`'
)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('Masthead', () => {
it('should warn the consumer for using the deprecated `link` prop', () => {
expect(consoleWarnSpy).toHaveBeenCalledTimes(1)
expect(consoleWarnSpy).toHaveBeenCalledWith(
'WARN - RNDS - The `link` prop is deprecated'
'WARN - MODDS - The `link` prop is deprecated'
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ describe('logger', () => {
logger.debug('Hello, World!')

expect(consoleLogSpy).toHaveBeenCalledTimes(1)
expect(consoleLogSpy).toHaveBeenCalledWith('DEBUG - RNDS - Hello, World!')
expect(consoleLogSpy).toHaveBeenCalledWith(
'DEBUG - MODDS - Hello, World!'
)
})
})

Expand All @@ -21,7 +23,9 @@ describe('logger', () => {
logger.warn('Hello, World!')

expect(consoleWarnSpy).toHaveBeenCalledTimes(1)
expect(consoleWarnSpy).toHaveBeenCalledWith('WARN - RNDS - Hello, World!')
expect(consoleWarnSpy).toHaveBeenCalledWith(
'WARN - MODDS - Hello, World!'
)
})
})

Expand All @@ -31,7 +35,9 @@ describe('logger', () => {
logger.error('Hello, World!')

expect(consoleErrorSpy).toHaveBeenCalledTimes(1)
expect(consoleErrorSpy).toHaveBeenCalledWith('ERROR - RNDS - Hello, World!')
expect(consoleErrorSpy).toHaveBeenCalledWith(
'ERROR - MODDS - Hello, World!'
)
})
})
})
14 changes: 9 additions & 5 deletions packages/react-component-library/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | '__RNDS_LOG_LEVEL'
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | '__MODDS_LOG_LEVEL'

export type Logger = Record<LogLevel, typeof console.log>

const RNDS_LOG_LEVEL: LogLevel = '__RNDS_LOG_LEVEL'
const MODDS_LOG_LEVEL: LogLevel = '__MODDS_LOG_LEVEL'
const levels: LogLevel[] = ['debug', 'info', 'warn', 'error']

function isValidLogLevel(logLevel: LogLevel, index: number): boolean {
Expand All @@ -15,7 +15,7 @@ function isValidLogLevel(logLevel: LogLevel, index: number): boolean {

/**
* Generates a `logger[debug | info | warn | error](...args)`
* Log level is set at build time via `RNDS_LOG_LEVEL` env var
* Log level is set at build time via `MODDS_LOG_LEVEL` env var
*
*/
export default levels.reduce((logger, level: LogLevel, index: number) => {
Expand All @@ -24,11 +24,15 @@ export default levels.reduce((logger, level: LogLevel, index: number) => {
[level]: (...args: unknown[]) => {
const func = level === 'debug' ? 'log' : level

if (RNDS_LOG_LEVEL && console && isValidLogLevel(RNDS_LOG_LEVEL, index)) {
if (
MODDS_LOG_LEVEL &&
console &&
isValidLogLevel(MODDS_LOG_LEVEL, index)
) {
const [message, ...rest] = [...args]

// eslint-disable-next-line no-console
console[func](`${level.toUpperCase()} - RNDS - ${message}`, ...rest)
console[func](`${level.toUpperCase()} - MODDS - ${message}`, ...rest)
}
},
}
Expand Down

0 comments on commit da12d80

Please sign in to comment.