Skip to content

Commit a4ddfcd

Browse files
committed
Add custom log level
1 parent 78f3a03 commit a4ddfcd

File tree

6 files changed

+68
-12
lines changed

6 files changed

+68
-12
lines changed

src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { LogLevels } from './types'
2+
13
export const CSS_COMPONENT = 'color: DodgerBlue'
24
export const CSS_CHANGE = 'color: green; font-weight: bold;'
35
export const CSS_SUB_VALUE = 'color: SlateGray; font-weight: thin;'
46

57
export const ALLOWED_NODE_ENVS = ['dev', 'development']
8+
9+
export const DEFAULT_LOG_LEVEL: LogLevels = 'log'

src/index.test.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,36 @@ describe('useLog', () => {
302302

303303
expect(anotherPrinterLog).toHaveBeenCalled()
304304
})
305+
306+
it('renders hook with custom log level', () => {
307+
const consoleWarn = jest
308+
.spyOn(console, 'warn')
309+
.mockImplementation(() => null)
310+
311+
renderHook(() => {
312+
const { log } = useLog({ logLevel: 'warn' })
313+
log('Test')
314+
})
315+
316+
expect(consoleLog).not.toHaveBeenCalled()
317+
expect(consoleWarn).toHaveBeenCalled()
318+
})
319+
320+
it('renders hook with custom log level', () => {
321+
const consoleWarn = jest
322+
.spyOn(console, 'warn')
323+
.mockImplementation(() => null)
324+
const consoleError = jest
325+
.spyOn(console, 'error')
326+
.mockImplementation(() => null)
327+
328+
renderHook(() => {
329+
const { log } = useLog({ logLevel: 'error' })
330+
log('Test', { logLevel: 'warn' })
331+
})
332+
333+
expect(consoleLog).not.toHaveBeenCalled()
334+
expect(consoleError).not.toHaveBeenCalled()
335+
expect(consoleWarn).toHaveBeenCalled()
336+
})
305337
})

src/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
CSS_CHANGE,
2222
CSS_COMPONENT,
2323
CSS_SUB_VALUE,
24+
DEFAULT_LOG_LEVEL,
2425
} from './constants'
2526

2627
/**
@@ -46,6 +47,7 @@ export function useLog({
4647
isGroupingEnabled = true,
4748
isGroupCollapsed = false,
4849
printer = console as Printer,
50+
logLevel = DEFAULT_LOG_LEVEL,
4951
}: UseLogConfig = {}): UseLogReturn {
5052
const componentName = getComponentName()
5153

@@ -67,7 +69,7 @@ export function useLog({
6769
const prevValueRef = useRef<T>()
6870
const printProps: Pick<
6971
_PrintConfig<T>,
70-
'value' | 'styles' | 'componentName' | 'flags' | 'printer'
72+
'value' | 'styles' | 'componentName' | 'flags' | 'printer' | 'logLevel'
7173
> = {
7274
value: clonedValue,
7375
styles: {
@@ -81,6 +83,7 @@ export function useLog({
8183
isCollapsed: props?.isGroupCollapsed ?? isGroupCollapsed,
8284
},
8385
printer: props?.printer ?? printer,
86+
logLevel: props?.logLevel ?? logLevel,
8487
}
8588

8689
if (environments.includes(process.env.NODE_ENV ?? 'production')) {

src/types.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type UseLogConfig = {
4242
environments?: string[]
4343
/** Contains custom implementation of console */
4444
printer?: Printer | Console
45+
logLevel?: LogLevels
4546
} & (
4647
| {
4748
/** Enable grouping for logs */
@@ -82,6 +83,7 @@ export interface _PrintConfig<T> {
8283
componentName: string
8384
flags?: _PrintFlags
8485
printer?: Printer | Console
86+
logLevel?: LogLevels
8587
}
8688

8789
/**
@@ -108,20 +110,19 @@ export enum _PrintTypes {
108110
Change = 'Change',
109111
}
110112

113+
/** Supported log levels which can be used in the console or custom console implementation */
114+
export type LogLevels = keyof Pick<
115+
Console,
116+
'log' | 'info' | 'error' | 'warn' | 'debug'
117+
>
118+
111119
/**
112120
* Supported console methods
113121
* @internal
114122
*/
115123
export type _SupportedConsole = Pick<
116124
Console,
117-
| 'group'
118-
| 'groupCollapsed'
119-
| 'groupEnd'
120-
| 'log'
121-
| 'info'
122-
| 'error'
123-
| 'warn'
124-
| 'debug'
125+
'group' | 'groupCollapsed' | 'groupEnd' | LogLevels
125126
>
126127

127128
/** Describes custom implementation of console object with only supported methods used to render logs */

src/utils.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,20 @@ describe('utils', () => {
160160
expect(consoleLog).toHaveBeenCalled()
161161
expect(consoleGroupEnd).toHaveBeenCalled()
162162
})
163+
164+
it('prints with custom log level', () => {
165+
const consoleWarn = jest
166+
.spyOn(console, 'warn')
167+
.mockImplementation(() => null)
168+
print({
169+
...printProps,
170+
logLevel: 'warn',
171+
})
172+
173+
expect(consoleGroup).toHaveBeenCalled()
174+
expect(consoleLog).not.toHaveBeenCalled()
175+
expect(consoleWarn).toHaveBeenCalled()
176+
expect(consoleGroupEnd).toHaveBeenCalled()
177+
})
163178
})
164179
})

src/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export function print<T>({
6464
group = getGroupLabel(type, componentName),
6565
styles: { componentCSS, subValueCSS, changeCSS } = {},
6666
printer = {},
67+
logLevel = 'log',
6768
}: _PrintConfig<T>): void {
6869
const getCurrentPrinter = (
6970
method: keyof _SupportedConsole,
@@ -76,13 +77,13 @@ export function print<T>({
7677
}
7778

7879
if ('prevValue' in arguments[0]) {
79-
getCurrentPrinter('log')(
80+
getCurrentPrinter(logLevel)(
8081
`Previous value: %c${String(arguments[0].prevValue)}`,
8182
subValueCSS,
8283
)
83-
getCurrentPrinter('log')(` Current value: %c${String(value)}`, changeCSS)
84+
getCurrentPrinter(logLevel)(` Current value: %c${String(value)}`, changeCSS)
8485
} else {
85-
getCurrentPrinter('log')(`${label.padStart(14, ' ')}: ${String(value)}`)
86+
getCurrentPrinter(logLevel)(`${label.padStart(14, ' ')}: ${String(value)}`)
8687
}
8788

8889
if (flags.isGrouped) {

0 commit comments

Comments
 (0)