-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
207 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,40 @@ | ||
import type { Reporter } from '../types'; | ||
import { formatWithOptions } from 'node:util'; | ||
|
||
export const BasicReporter = () => { | ||
return <Reporter>{ | ||
print(obj) { | ||
console.log(obj.message); | ||
import type { LogObject, PrintContext, FormatOptions } from '../types'; | ||
|
||
import { LogLevels } from '../level'; | ||
import { bracket } from '../utils/format'; | ||
import { writeStream } from '../utils/stream'; | ||
|
||
import type { FormatReporter } from './types'; | ||
|
||
export const BasicReporter = (): FormatReporter => { | ||
return { | ||
formatArgs(opts: FormatOptions, message?: string, args: any[] = []) { | ||
return formatWithOptions(opts, message, ...args); | ||
}, | ||
formatLogObject(obj: LogObject, ctx: PrintContext) { | ||
const message = this.formatArgs( | ||
ctx.options.format, | ||
obj.message, | ||
obj.args | ||
); | ||
|
||
return [ | ||
bracket(obj.type === 'log' ? undefined : obj.type), | ||
bracket(obj.tag), | ||
message | ||
] | ||
.filter(Boolean) | ||
.join(' '); | ||
}, | ||
print(obj, ctx) { | ||
const message = this.formatLogObject(obj, ctx); | ||
const stream = | ||
obj.level < LogLevels.log | ||
? ctx.options.stderr || process.stderr | ||
: ctx.options.stdout || process.stdout; | ||
writeStream(message, stream); | ||
} | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,9 +1,38 @@ | ||
import type { Reporter } from '../types'; | ||
import { formatWithOptions } from 'node:util'; | ||
|
||
export const FancyReporter = () => { | ||
return <Reporter>{ | ||
print(obj) { | ||
console.log(obj.message); | ||
import { LogLevels } from '../level'; | ||
import { bracket } from '../utils/format'; | ||
import { writeStream } from '../utils/stream'; | ||
|
||
import type { FormatReporter } from './types'; | ||
|
||
export const FancyReporter = (): FormatReporter => { | ||
return { | ||
formatArgs(opts, message?: string, args: any[] = []) { | ||
return formatWithOptions(opts, message, ...args); | ||
}, | ||
formatLogObject(obj, ctx) { | ||
const message = this.formatArgs( | ||
ctx.options.format, | ||
obj.message, | ||
obj.args | ||
); | ||
|
||
return [ | ||
bracket(obj.type === 'log' ? undefined : obj.type), | ||
bracket(obj.tag), | ||
message | ||
] | ||
.filter(Boolean) | ||
.join(' '); | ||
}, | ||
print(obj, ctx) { | ||
const message = this.formatLogObject(obj, ctx); | ||
const stream = | ||
obj.level < LogLevels.log | ||
? ctx.options.stderr || process.stderr | ||
: ctx.options.stdout || process.stdout; | ||
writeStream(message, stream); | ||
} | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,12 +1,21 @@ | ||
import type { LogObject, Reporter } from '../types'; | ||
|
||
import type { FormatReporter } from './types'; | ||
|
||
interface HistoryLog { | ||
readonly output: string; | ||
|
||
readonly object: LogObject; | ||
} | ||
|
||
export const MockReporter = ( | ||
history: LogObject[] = [] | ||
): Reporter & { history: LogObject[] } => { | ||
reporter: FormatReporter, | ||
history: HistoryLog[] = [] | ||
): Reporter & { history: HistoryLog[] } => { | ||
return { | ||
history, | ||
print(obj) { | ||
history.push(obj); | ||
print(object, ctx) { | ||
history.push({ output: reporter.formatLogObject(object, ctx), object }); | ||
} | ||
}; | ||
}; |
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,12 @@ | ||
import type { | ||
Reporter, | ||
LogObject, | ||
PrintContext, | ||
FormatOptions | ||
} from '../types'; | ||
|
||
export interface FormatReporter extends Reporter { | ||
formatArgs(opts: FormatOptions, message?: string, args?: any[]): string; | ||
|
||
formatLogObject(obj: LogObject, ctx: PrintContext): string; | ||
} |
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,7 @@ | ||
export const bracket = (text?: string) => { | ||
if (text) { | ||
return `[${text}]`; | ||
} else { | ||
return undefined; | ||
} | ||
}; |
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,3 @@ | ||
export * from './format'; | ||
|
||
export * from './stream'; |
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,4 @@ | ||
export function writeStream(data: any, stream: NodeJS.WriteStream) { | ||
const write = (stream as any).__write || stream.write; | ||
return write.call(stream, data); | ||
} |
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 |
---|---|---|
@@ -1,32 +1,31 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { Logger, MockReporter } from '../src'; | ||
import { Logger, LogLevels, BasicReporter, MockReporter } from '../src'; | ||
|
||
describe('Basic Logger', () => { | ||
it('should work', () => { | ||
const reporter = MockReporter(); | ||
const logger = Logger({ reporter: [reporter] }); | ||
const reporter = MockReporter(BasicReporter()); | ||
const logger = Logger({ level: LogLevels.verbose, reporter: [reporter] }); | ||
|
||
logger.log('Hello'); | ||
logger.log('World'); | ||
expect(reporter.history.map((obj) => ({ ...obj, date: undefined }))) | ||
.toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"args": [], | ||
"date": undefined, | ||
"level": 0, | ||
"message": "Hello", | ||
"type": "info", | ||
}, | ||
{ | ||
"args": [], | ||
"date": undefined, | ||
"level": 0, | ||
"message": "World", | ||
"type": "info", | ||
}, | ||
] | ||
`); | ||
expect(reporter.history.map((obj) => obj.output)).toMatchInlineSnapshot(` | ||
[ | ||
"Hello", | ||
"World", | ||
] | ||
`); | ||
}); | ||
|
||
it('should format', () => { | ||
const reporter = MockReporter(BasicReporter()); | ||
const logger = Logger({ level: LogLevels.verbose, reporter: [reporter] }); | ||
|
||
logger.log('Hello %s', 'world'); | ||
expect(reporter.history.map((obj) => obj.output)).toMatchInlineSnapshot(` | ||
[ | ||
"Hello world", | ||
] | ||
`); | ||
}); | ||
}); |