Skip to content

Commit 18e316d

Browse files
feat: experimental CommonLogger/SimpleLogger interfaces
1 parent 4c033a8 commit 18e316d

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ import {
224224
} from './types'
225225
import { _gb, _hb, _kb, _mb } from './unit/size.util'
226226
import { is } from './vendor/is'
227+
import {
228+
CommonLogLevel,
229+
CommonLogFunction,
230+
CommonLogger,
231+
SimpleLogger,
232+
createSimpleLogger,
233+
noopLogger,
234+
} from './log/commonLogger'
227235

228236
export type {
229237
MemoCache,
@@ -295,6 +303,10 @@ export type {
295303
JsonSchemaArray,
296304
JsonSchemaTuple,
297305
JsonSchemaBuilder,
306+
CommonLogLevel,
307+
CommonLogFunction,
308+
CommonLogger,
309+
SimpleLogger,
298310
}
299311

300312
export {
@@ -453,4 +465,6 @@ export {
453465
_defineLazyProperty,
454466
_defineLazyProps,
455467
_lazyValue,
468+
createSimpleLogger,
469+
noopLogger,
456470
}

src/log/commonLogger.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { CommonLogger, createSimpleLogger, noopLogger } from './commonLogger'
2+
3+
// This "tests" that `console` is a valid CommonLogger by itself
4+
const consoleLogger: CommonLogger = console
5+
6+
test('commonLogger', () => {
7+
consoleLogger.log('hello')
8+
consoleLogger.warn('hello')
9+
consoleLogger.error('hello')
10+
})
11+
12+
test('createSimpleLogger', () => {
13+
const logger = createSimpleLogger(console)
14+
logger('hey')
15+
logger.log('hey')
16+
logger.warn('hey')
17+
logger.error('hey')
18+
})
19+
20+
test('noopLogger', () => {
21+
const logger = noopLogger
22+
logger('hey')
23+
logger.log('hey')
24+
logger.warn('hey')
25+
logger.error('hey')
26+
})

src/log/commonLogger.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* These levels follow console.* naming,
3+
* so you can use console[level] safely.
4+
*
5+
* `log` is considered default level.
6+
*
7+
* For simplicity - only these 3 levels are kept.
8+
*
9+
* @experimental
10+
*/
11+
export type CommonLogLevel = 'log' | 'warn' | 'error'
12+
13+
/**
14+
* Function that takes any number of arguments and logs them all.
15+
* It is expected that logged arguments are separated by "space", like console.log does.
16+
*
17+
* @experimental
18+
*/
19+
export type CommonLogFunction = (...args: any[]) => void
20+
21+
/**
22+
* Interface is inspired/compatible with `console.*`
23+
* So, `console` is a valid CommonLogger implementation as-is.
24+
*
25+
* @experimental
26+
*/
27+
export interface CommonLogger {
28+
log: CommonLogFunction
29+
warn: CommonLogFunction
30+
error: CommonLogFunction
31+
}
32+
33+
/**
34+
* Same as CommonLogger, but also is a "convenience function" itself.
35+
* So you can do `logger('hey')` which is the same as `logger.log('hey')`
36+
*
37+
* @experimental
38+
*/
39+
export interface SimpleLogger extends CommonLogFunction, CommonLogger {}
40+
41+
/**
42+
* Creates a SimpleLogger from CommonLogger.
43+
*
44+
* @experimental
45+
*/
46+
export function createSimpleLogger(logger: CommonLogger): SimpleLogger {
47+
return Object.assign(((...args: any[]) => logger.log(...args)) as any, {
48+
log: (...args: any[]) => logger.log(...args),
49+
warn: (...args: any[]) => logger.warn(...args),
50+
error: (...args: any[]) => logger.error(...args),
51+
})
52+
}
53+
54+
const noop = () => {}
55+
56+
/**
57+
* SimpleLogger that does nothing (noop).
58+
*
59+
* @experimental
60+
*/
61+
export const noopLogger: SimpleLogger = createSimpleLogger({
62+
log: noop,
63+
warn: noop,
64+
error: noop,
65+
})

0 commit comments

Comments
 (0)