Skip to content

Commit 7bd3063

Browse files
committed
Add customization for console implementation
1 parent 6d5b915 commit 7bd3063

File tree

5 files changed

+144
-14
lines changed

5 files changed

+144
-14
lines changed

src/index.test.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,39 @@ describe('useLog', () => {
264264
`Mount in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
265265
)
266266
})
267+
268+
it('renders hook with custom printer', () => {
269+
const printer = { log: jest.fn() }
270+
271+
const printerLog = jest.spyOn(printer, 'log').mockImplementation(() => null)
272+
273+
renderHook(() => {
274+
const { log } = useLog({ printer })
275+
log('Test')
276+
})
277+
278+
expect(consoleLog).not.toHaveBeenCalled()
279+
280+
expect(printerLog).toHaveBeenCalled()
281+
})
282+
283+
it('renders log with custom printer', () => {
284+
const printer = { log: jest.fn() }
285+
const anotherPrinter = { log: jest.fn() }
286+
287+
const printerLog = jest.spyOn(printer, 'log').mockImplementation(() => null)
288+
const anotherPrinterLog = jest
289+
.spyOn(anotherPrinter, 'log')
290+
.mockImplementation(() => null)
291+
292+
renderHook(() => {
293+
const { log } = useLog({ printer })
294+
log('Test', { printer: anotherPrinter })
295+
})
296+
297+
expect(consoleLog).not.toHaveBeenCalled()
298+
expect(printerLog).not.toHaveBeenCalled()
299+
300+
expect(anotherPrinterLog).toHaveBeenCalled()
301+
})
267302
})

src/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
LogConfig,
1414
_PrintTypes,
1515
_PrintConfig,
16+
Printer,
1617
} from './types'
1718
import { getComponentName, print } from './utils'
1819
import {
@@ -44,6 +45,7 @@ export function useLog({
4445
environments = ALLOWED_NODE_ENVS,
4546
isGroupingEnabled = true,
4647
isGroupCollapsed = false,
48+
printer = console as Printer,
4749
}: UseLogConfig = {}): UseLogReturn {
4850
const componentName = getComponentName()
4951

@@ -65,7 +67,7 @@ export function useLog({
6567
const prevValueRef = useRef<T>()
6668
const printProps: Pick<
6769
_PrintConfig<T>,
68-
'value' | 'styles' | 'componentName' | 'flags'
70+
'value' | 'styles' | 'componentName' | 'flags' | 'printer'
6971
> = {
7072
value: clonedValue,
7173
styles: {
@@ -78,6 +80,7 @@ export function useLog({
7880
isGrouped: props?.isGroupingEnabled ?? isGroupingEnabled,
7981
isCollapsed: props?.isGroupCollapsed ?? isGroupCollapsed,
8082
},
83+
printer: props?.printer ?? printer,
8184
}
8285

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

src/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export type UseLogConfig = {
4040
styles?: Styles
4141
/** Contains array of environments of `process.env.NODE_ENV` in which logging will be allowed */
4242
environments?: string[]
43+
/** Contains custom implementation of console */
44+
printer?: Printer | Console
4345
} & (
4446
| {
4547
/** Enable grouping for logs */
@@ -79,6 +81,7 @@ export interface _PrintConfig<T> {
7981
styles?: Styles
8082
componentName: string
8183
flags?: _PrintFlags
84+
printer?: Printer | Console
8285
}
8386

8487
/**
@@ -104,3 +107,22 @@ export enum _PrintTypes {
104107
Unmount = 'Unmount',
105108
Change = 'Change',
106109
}
110+
111+
/**
112+
* Supported console methods
113+
* @internal
114+
*/
115+
export type _SupportedConsole = Pick<
116+
Console,
117+
| 'group'
118+
| 'groupCollapsed'
119+
| 'groupEnd'
120+
| 'log'
121+
| 'info'
122+
| 'error'
123+
| 'warn'
124+
| 'debug'
125+
>
126+
127+
/** Describes custom implementation of console object with only supported methods used to render logs */
128+
export type Printer = Partial<_SupportedConsole>

src/utils.test.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getGroupLabel, getComponentName, print } from './utils'
2-
import { _PrintConfig, _PrintTypes } from './types'
1+
import { getGroupLabel, getComponentName, print, getPrinter } from './utils'
2+
import { _PrintConfig, _PrintTypes, Printer } from './types'
33

44
describe('utils', () => {
55
describe('getGroupLabel', () => {
@@ -22,6 +22,28 @@ describe('utils', () => {
2222
})
2323
})
2424

25+
describe('getPrinter', () => {
26+
it('returns printer for existing printer method', () => {
27+
const printer: Printer = { log: jest.fn() }
28+
expect(getPrinter(printer, 'log')).toBe(printer.log)
29+
})
30+
31+
it('returns console for non-existing printer method', () => {
32+
const printer: Printer = { log: jest.fn() }
33+
expect(getPrinter(printer, 'warn')).toBe(console.warn)
34+
})
35+
36+
it('returns console for empty printer', () => {
37+
const printer: Printer = {}
38+
expect(getPrinter(printer, 'log')).toBe(console.log)
39+
})
40+
41+
it('returns console for empty printer method', () => {
42+
const printer: Printer = { log: undefined }
43+
expect(getPrinter(printer, 'log')).toBe(console.log)
44+
})
45+
})
46+
2547
describe('print', () => {
2648
const consoleLog = jest.spyOn(console, 'log').mockImplementation(() => null)
2749
const consoleGroup = jest
@@ -104,5 +126,38 @@ describe('utils', () => {
104126
expect(consoleLog).toHaveBeenCalledWith(' A Label: Test Value')
105127
expect(consoleGroupEnd).toHaveBeenCalled()
106128
})
129+
130+
it('prints with custom printer', () => {
131+
const printer: Printer = { log: jest.fn() }
132+
const printPropsWithPrinter: _PrintConfig<string> = {
133+
...printProps,
134+
printer,
135+
}
136+
const printerLog = jest
137+
.spyOn(printer, 'log')
138+
.mockImplementation(() => 'Some logs')
139+
140+
print(printPropsWithPrinter)
141+
142+
expect(consoleLog).not.toHaveBeenCalled()
143+
144+
expect(consoleGroup).toHaveBeenCalled()
145+
expect(printerLog).toHaveBeenCalled()
146+
expect(consoleGroupEnd).toHaveBeenCalled()
147+
})
148+
149+
it('prints with custom empty printer', () => {
150+
const printer: Printer = {}
151+
const printPropsWithPrinter: _PrintConfig<string> = {
152+
...printProps,
153+
printer,
154+
}
155+
156+
print(printPropsWithPrinter)
157+
158+
expect(consoleGroup).toHaveBeenCalled()
159+
expect(consoleLog).toHaveBeenCalled()
160+
expect(consoleGroupEnd).toHaveBeenCalled()
161+
})
107162
})
108163
})

src/utils.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { _PrintConfig, _PrintTypes } from './types'
1+
import { Printer, _PrintConfig, _PrintTypes, _SupportedConsole } from './types'
22

33
export function getGroupLabel(
44
type: _PrintTypes,
@@ -30,6 +30,16 @@ export function getComponentName(): string {
3030
}
3131
}
3232

33+
export function getPrinter(
34+
printer: Printer | Console,
35+
method: keyof _SupportedConsole,
36+
): _SupportedConsole[keyof _SupportedConsole] {
37+
return (
38+
(printer && method in printer ? printer[method] : console[method]) ??
39+
console[method]
40+
)
41+
}
42+
3343
export function print<T>({
3444
value,
3545
label,
@@ -42,23 +52,28 @@ export function print<T>({
4252
type = _PrintTypes.Change,
4353
group = getGroupLabel(type, componentName),
4454
styles: { componentCSS, subValueCSS, changeCSS } = {},
55+
printer = {},
4556
}: _PrintConfig<T>): void {
46-
flags.isGrouped &&
47-
console[flags.isCollapsed ? 'groupCollapsed' : 'group'](
48-
group,
49-
componentCSS,
50-
subValueCSS,
51-
)
57+
const getCurrentPrinter = (
58+
method: keyof _SupportedConsole,
59+
): _SupportedConsole[keyof _SupportedConsole] => getPrinter(printer, method)
60+
const groupMethod = flags.isCollapsed ? 'groupCollapsed' : 'group'
61+
62+
if (flags.isGrouped) {
63+
getCurrentPrinter(groupMethod)(group, componentCSS, subValueCSS)
64+
}
5265

5366
if (!('prevValue' in arguments[0])) {
54-
console.log(`${label.padStart(14, ' ')}: ${String(value)}`)
67+
getCurrentPrinter('log')(`${label.padStart(14, ' ')}: ${String(value)}`)
5568
} else {
56-
console.log(
69+
getCurrentPrinter('log')(
5770
`Previous value: %c${String(arguments[0].prevValue)}`,
5871
subValueCSS,
5972
)
60-
console.log(` Current value: %c${String(value)}`, changeCSS)
73+
getCurrentPrinter('log')(` Current value: %c${String(value)}`, changeCSS)
6174
}
6275

63-
flags.isGrouped && console.groupEnd()
76+
if (flags.isGrouped) {
77+
getCurrentPrinter('groupEnd')()
78+
}
6479
}

0 commit comments

Comments
 (0)