-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add logging utility fn * reset * fix import
- Loading branch information
1 parent
1433b8b
commit b3bea1a
Showing
7 changed files
with
131 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type {PlatformConfig} from 'style-dictionary/types' | ||
import {log} from './log.js' | ||
|
||
describe('Utilities: log', () => { | ||
const consoleLogSpy = vi.spyOn(console, 'log') | ||
const consoleWarnSpy = vi.spyOn(console, 'warn') | ||
const consoleErrorSpy = vi.spyOn(console, 'error') | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks() | ||
}) | ||
|
||
it('logs correct type', () => { | ||
log.info('message.log') | ||
log.warning('message.warn') | ||
log.error('message.error') | ||
expect(consoleLogSpy).toHaveBeenLastCalledWith('message.log') | ||
expect(consoleWarnSpy).toHaveBeenLastCalledWith('message.warn') | ||
expect(consoleErrorSpy).toHaveBeenLastCalledWith('message.error') | ||
}) | ||
|
||
it('only logs errors when verbosity silent', () => { | ||
const config = { | ||
log: { | ||
verbosity: 'silent', | ||
}, | ||
} as PlatformConfig | ||
|
||
log.info('message.log', config) | ||
log.warning('message.warn', config) | ||
log.error('message.error', config) | ||
expect(consoleLogSpy).not.toHaveBeenLastCalledWith('message.log') | ||
expect(consoleWarnSpy).not.toHaveBeenLastCalledWith('message.warn') | ||
expect(consoleErrorSpy).toHaveBeenLastCalledWith('message.error') | ||
}) | ||
|
||
it('only logs errors & warnings when verbosity silent', () => { | ||
const config = { | ||
log: { | ||
verbosity: 'default', | ||
warnings: 'warn', | ||
}, | ||
} as PlatformConfig | ||
|
||
log.info('message.log', config) | ||
log.warning('message.warn', config) | ||
log.error('message.error', config) | ||
expect(consoleLogSpy).not.toHaveBeenLastCalledWith('message.log') | ||
expect(consoleWarnSpy).toHaveBeenLastCalledWith('message.warn') | ||
expect(consoleErrorSpy).toHaveBeenLastCalledWith('message.error') | ||
}) | ||
|
||
it('only logs errors & warnings when verbosity silent', () => { | ||
const config = { | ||
log: { | ||
verbosity: 'default', | ||
warnings: 'disabled', | ||
}, | ||
} as PlatformConfig | ||
|
||
log.info('message.log', config) | ||
log.warning('message.warn', config) | ||
log.error('message.error', config) | ||
expect(consoleLogSpy).not.toHaveBeenLastCalledWith('message.log') | ||
expect(consoleWarnSpy).not.toHaveBeenLastCalledWith('message.warn') | ||
expect(consoleErrorSpy).toHaveBeenLastCalledWith('message.error') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type {PlatformConfig} from 'style-dictionary/types' | ||
|
||
const logMessage = (message: string, type: 'info' | 'warning' | 'error' = 'warning', config?: PlatformConfig) => { | ||
// early return if verbosity is silent | ||
if (config?.log?.verbosity === 'silent' && type !== 'error') { | ||
return | ||
} | ||
// early return if verbosity is default and type is info | ||
if (config?.log?.verbosity === 'default' && type === 'info') { | ||
return | ||
} | ||
// early return | ||
if ((config?.log?.warnings === 'disabled' || config?.log?.warnings === 'warn') && type === 'info') { | ||
return | ||
} | ||
// early return | ||
if (config?.log?.warnings === 'disabled' && type === 'warning') { | ||
return | ||
} | ||
if (type === 'warning') { | ||
// eslint-disable-next-line no-console | ||
return console.warn(message) | ||
} | ||
if (type === 'error') { | ||
// eslint-disable-next-line no-console | ||
return console.error(message) | ||
} | ||
// eslint-disable-next-line no-console | ||
console.log(message) | ||
} | ||
|
||
export const log = { | ||
info: (message: string, config?: PlatformConfig) => logMessage(message, 'info', config), | ||
warning: (message: string, config?: PlatformConfig) => logMessage(message, 'warning', config), | ||
error: (message: string, config?: PlatformConfig) => logMessage(message, 'error', config), | ||
} |