Skip to content

Commit

Permalink
refactor!: make the all argument in same logger, print in the one line
Browse files Browse the repository at this point in the history
BREAKING CHANGE: rename the printWithColor to printIn, and make the all argument print in one line
  • Loading branch information
trylovetom committed May 11, 2020
1 parent 17c536c commit 9215054
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 58 deletions.
61 changes: 22 additions & 39 deletions packages/logger/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
formatContext,
printWithColor,
paintIn,
print,
LogLevel,
printWith,
Expand All @@ -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)
})
})
Expand All @@ -50,29 +50,28 @@ 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)
})
})

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, \
Expand All @@ -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)
Expand All @@ -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)
})
})
40 changes: 21 additions & 19 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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`)
}
}

Expand Down

0 comments on commit 9215054

Please sign in to comment.