From 92150545f1e10013b692fa2802f7bb4903398790 Mon Sep 17 00:00:00 2001 From: "TZU-YEN, CHANG" Date: Mon, 11 May 2020 18:06:34 +0800 Subject: [PATCH] refactor!: make the all argument in same logger, print in the one line BREAKING CHANGE: rename the printWithColor to printIn, and make the all argument print in one line --- packages/logger/src/index.spec.ts | 61 +++++++++++-------------------- packages/logger/src/index.ts | 40 ++++++++++---------- 2 files changed, 43 insertions(+), 58 deletions(-) diff --git a/packages/logger/src/index.spec.ts b/packages/logger/src/index.spec.ts index 1e19bf4..1acaa2b 100644 --- a/packages/logger/src/index.spec.ts +++ b/packages/logger/src/index.spec.ts @@ -1,6 +1,6 @@ import { formatContext, - printWithColor, + paintIn, print, LogLevel, printWith, @@ -24,24 +24,24 @@ afterEach(() => { }) describe('formatContext', () => { - it(`should just add break line, \ + it(`should no do anything, \ if type of context is not object.`, () => { const input = 'text' - const output = `${input}\n` + const output = input expect(formatContext(input)).toBe(output) }) - it(`should stringify the object and add break line, \ + it(`should stringify the object, \ if type of context is an object.`, () => { const input = { sub: { text: 'text' } } - const output = `${JSON.stringify(input)}\n` + const output = JSON.stringify(input) expect(formatContext(input)).toBe(output) }) - it(`should stringify the object with 2 spaces and add break line, \ + it(`should stringify the object with 2 spaces, \ if type of context is an object.`, () => { const input = { sub: { text: 'text' } } - const output = `${JSON.stringify(input, undefined, 2)}\n` + const output = `${JSON.stringify(input, undefined, 2)}` expect(formatContext(input, true)).toBe(output) }) }) @@ -50,18 +50,18 @@ describe('printWithColor', () => { it(`should write the process.stdout without color, \ if color is not setup.`, () => { const input = 'text' - const output = input - printWithColor(input) - expect(process.stdout.write).toBeCalledWith(output) + const target = input + const output = paintIn(input) + expect(output).toBe(target) }) it(`should write the process.stdout with color, \ if color is setup.`, () => { const input = 'text' const color = '90' - const output = `\u001B[${color}m${input}\u001B[m` - printWithColor(input, color) - expect(process.stdout.write).toBeCalledWith(output) + const target = `\u001B[${color}m${input}\u001B[m` + const output = paintIn(input, color) + expect(output).toBe(target) }) }) @@ -69,10 +69,9 @@ describe('print', () => { it(`should write the process.stdout, \ if level is higher or equal to default level(debug).`, () => { const input = 'text' - const output = `${input}\n` - print({}, input, input) + const output = `${input} ${input}\n` + print(undefined, input, input) expect(process.stdout.write).toHaveBeenNthCalledWith(1, output) - expect(process.stdout.write).toHaveBeenNthCalledWith(2, output) }) it(`should not write the process.stdout, \ @@ -91,32 +90,21 @@ if context is error.`, () => { print({ level: LogLevel.error }, input) expect(process.stdout.write).toHaveBeenNthCalledWith(1, outputStack) }) - - it(`should write the message to process.stdout, \ -if context is error and stack is missing.`, () => { - const input = new Error('Hi There!') - const outputMessage = `${input.message}\n` - input.stack = '' - process.env.LOG_LEVEL = 'error' - print({ level: LogLevel.error }, input) - expect(process.stdout.write).toHaveBeenNthCalledWith(1, outputMessage) - }) }) describe('printWith', () => { it('should create custom logger', () => { const input = 'text' const color = '96' - const output = `\u001B[${color}m${input}\n\u001B[m` + const output = `\u001B[${color}m${input} ${input}\u001B[m\n` const customLogger = printWith({ level: LogLevel.warn, color }) customLogger(input, input) expect(process.stdout.write).toHaveBeenNthCalledWith(1, output) - expect(process.stdout.write).toHaveBeenNthCalledWith(2, output) }) it('should print break line, if argument is empty', () => { const color = '96' - const output = `\u001B[${color}m\n\u001B[m` + const output = `\u001B[${color}m\u001B[m\n` const customLogger = printWith({ level: LogLevel.warn, color }) customLogger() expect(process.stdout.write).toHaveBeenNthCalledWith(1, output) @@ -127,50 +115,45 @@ describe('logger', () => { it('error', () => { const input = 'text' const color = defaultColors.error - const output = `\u001B[${color}m${input}\n\u001B[m` + const output = `\u001B[${color}m${input} ${input}\u001B[m\n` const customLogger = printWith({ level: LogLevel.warn, color }) customLogger(input, input) expect(process.stdout.write).toHaveBeenNthCalledWith(1, output) - expect(process.stdout.write).toHaveBeenNthCalledWith(2, output) }) it('warn', () => { const input = 'text' const color = defaultColors.warn - const output = `\u001B[${color}m${input}\n\u001B[m` + const output = `\u001B[${color}m${input} ${input}\u001B[m\n` const customLogger = printWith({ level: LogLevel.warn, color }) customLogger(input, input) expect(process.stdout.write).toHaveBeenNthCalledWith(1, output) - expect(process.stdout.write).toHaveBeenNthCalledWith(2, output) }) it('info', () => { const input = 'text' const color = defaultColors.info - const output = `\u001B[${color}m${input}\n\u001B[m` + const output = `\u001B[${color}m${input} ${input}\u001B[m\n` const customLogger = printWith({ level: LogLevel.warn, color }) customLogger(input, input) expect(process.stdout.write).toHaveBeenNthCalledWith(1, output) - expect(process.stdout.write).toHaveBeenNthCalledWith(2, output) }) it('debug', () => { const input = 'text' const color = defaultColors.debug - const output = `\u001B[${color}m${input}\n\u001B[m` + const output = `\u001B[${color}m${input} ${input}\u001B[m\n` const customLogger = printWith({ level: LogLevel.warn, color }) customLogger(input, input) expect(process.stdout.write).toHaveBeenNthCalledWith(1, output) - expect(process.stdout.write).toHaveBeenNthCalledWith(2, output) }) it('verbose', () => { const input = 'text' const color = defaultColors.verbose - const output = `\u001B[${color}m${input}\n\u001B[m` + const output = `\u001B[${color}m${input} ${input}\u001B[m\n` const customLogger = printWith({ level: LogLevel.warn, color }) customLogger(input, input) expect(process.stdout.write).toHaveBeenNthCalledWith(1, output) - expect(process.stdout.write).toHaveBeenNthCalledWith(2, output) }) }) diff --git a/packages/logger/src/index.ts b/packages/logger/src/index.ts index b64568f..38f1720 100644 --- a/packages/logger/src/index.ts +++ b/packages/logger/src/index.ts @@ -41,29 +41,28 @@ export const defaultColors: LogColor = { */ export function formatContext(context: any, format?: boolean) { if (!isObject(context)) { - return `${context}\n` + return context } if (!format) { - return `${JSON.stringify(context)}\n` + return `${JSON.stringify(context)}` } - return `${JSON.stringify(context, undefined, 2)}\n` + return `${JSON.stringify(context, undefined, 2)}` } /** - * print the context with color - * @param context something you want to print - * @param color ansi code + * print the message in the color + * @param message something you want to print + * @param color ansi color code * @see https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit */ -export function printWithColor(context: any, color?: string) { +export function paintIn(message: string, color?: string) { if (!color) { - process.stdout.write(context) - return + return message } - process.stdout.write(`\u001B[${color}m${context}\u001B[m`) + return `\u001B[${color}m${message}\u001B[m` } export type LogOptions = { @@ -74,24 +73,27 @@ export type LogOptions = { /** * print it, suggest use the `printWith` to create customer logger - * @param param0 the options of logger + * @param options the options of logger * @param args something you want to print */ export function print( - { level = LogLevel.debug, color, format }: LogOptions, + { level = LogLevel.debug, color, format }: LogOptions = {}, ...args: any[] ) { if (level <= LogLevel[process.env.LOG_LEVEL || 'debug']) { - args.forEach(context => { + let output = '' + args.forEach((context, index) => { if (isError(context)) { - printWithColor( - formatContext(context.stack || context.message, format), - color - ) - return + output += `${formatContext(context.stack, format)}` + } else { + output += `${formatContext(context, format)}` + } + + if (index < args.length - 1) { + output += ' ' } - printWithColor(formatContext(context, format), color) }) + process.stdout.write(`${paintIn(output, color)}\n`) } }