Skip to content

Commit

Permalink
feat: serialize logger messages (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Apr 13, 2023
1 parent 98bfcec commit 2a171f3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type LogColors = keyof typeof colors
export interface LogEntry {
timestamp: Date
level: LogLevel
message: string
message: any
}

export class Logger {
Expand Down Expand Up @@ -60,7 +60,7 @@ export class Logger {
* @example
* logger.debug('no duplicates found, creating a document...')
*/
public debug(message: string, ...positionals: Array<unknown>): void {
public debug(message: any, ...positionals: Array<unknown>): void {
this.logEntry({
level: 'debug',
message: colors.gray(message),
Expand All @@ -77,7 +77,7 @@ export class Logger {
* @example
* logger.info('start parsing...')
*/
public info(message: string, ...positionals: Array<unknown>) {
public info(message: any, ...positionals: Array<unknown>) {
this.logEntry({
level: 'info',
message,
Expand All @@ -90,7 +90,7 @@ export class Logger {

const performance = new PerformanceEntry()

return (message: string, ...positionals: Array<unknown>) => {
return (message: any, ...positionals: Array<unknown>) => {
performance.measure()

this.logEntry({
Expand All @@ -110,7 +110,7 @@ export class Logger {
* @example
* logger.success('successfully created document')
*/
public success(message: string, ...positionals: Array<unknown>): void {
public success(message: any, ...positionals: Array<unknown>): void {
this.logEntry({
level: 'info',
message,
Expand All @@ -128,7 +128,7 @@ export class Logger {
* @example
* logger.warning('found legacy document format')
*/
public warning(message: string, ...positionals: Array<unknown>): void {
public warning(message: any, ...positionals: Array<unknown>): void {
this.logEntry({
level: 'warning',
message,
Expand All @@ -146,7 +146,7 @@ export class Logger {
* @example
* logger.error('something went wrong')
*/
public error(message: string, ...positionals: Array<unknown>): void {
public error(message: any, ...positionals: Array<unknown>): void {
this.logEntry({
level: 'error',
message,
Expand All @@ -172,7 +172,7 @@ export class Logger {
callback()
}

private createEntry(level: LogLevel, message: string): LogEntry {
private createEntry(level: LogLevel, message: unknown): LogEntry {
return {
timestamp: new Date(),
level,
Expand All @@ -182,7 +182,7 @@ export class Logger {

private logEntry(args: {
level: LogLevel
message: string
message: unknown
positionals?: Array<unknown>
prefix?: string
colors?: {
Expand Down Expand Up @@ -211,9 +211,9 @@ export class Logger {
write(
[colorize.timestamp(this.formatTimestamp(entry.timestamp))]
.concat(prefix != null ? colorize.prefix(prefix) : [])
.concat(message)
.concat(serializeInput(message))
.join(' '),
...positionals
...positionals.map(serializeInput)
)
}

Expand Down Expand Up @@ -304,3 +304,23 @@ function isDefinedAndNotEquals(
): boolean {
return value !== undefined && value !== expected
}

function serializeInput(message: any): string {
if (typeof message === 'undefined') {
return 'undefined'
}

if (message === null) {
return 'null'
}

if (typeof message === 'string') {
return message
}

if (typeof message === 'object') {
return JSON.stringify(message)
}

return message.toString()
}
16 changes: 16 additions & 0 deletions test/node/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,19 @@ it('supports positionals', () => {
`${colors.red('12:34:56:789')} ${colors.red('✖ [parser]')} hello world\n`
)
})

it('serializes the input message', () => {
logger.info({ hello: 'world' })
expect(process.stdout.write).toHaveBeenCalledWith(
`${colors.gray('12:34:56:789')} ${colors.blue(
'[parser]'
)} {"hello":"world"}\n`
)

logger.info([1, 'two', { three: 3 }])
expect(process.stdout.write).toHaveBeenCalledWith(
`${colors.gray('12:34:56:789')} ${colors.blue(
'[parser]'
)} [1,"two",{"three":3}]\n`
)
})

0 comments on commit 2a171f3

Please sign in to comment.