From fbc6fead145cd761713456ba1b01ff290ed18b19 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 25 Apr 2020 00:47:51 +0900 Subject: [PATCH 01/11] #31: updates logging module to allow multiple arguments and data types that are not string --- lib/log.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/log.js b/lib/log.js index a6ae987a..56913fbf 100644 --- a/lib/log.js +++ b/lib/log.js @@ -16,5 +16,24 @@ module.exports = (file) => { new transports.Console() ] }) - return logger + + const obj = { + info: (...args) => { + args.forEach((e) => { + logger.info(JSON.stringify(e)) + }) + }, + warn: (...args) => { + args.forEach((e) => { + logger.warn(JSON.stringify(e)) + }) + }, + error: (...args) => { + args.forEach((e) => { + logger.error(JSON.stringify(e)) + }) + } + } + + return obj } \ No newline at end of file From 9e18c8e99b0dabd9c3414c66b6235b01df3f6fca Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 26 Apr 2020 10:21:40 +0900 Subject: [PATCH 02/11] #31: fixes log.js --- lib/log.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/log.js b/lib/log.js index 56913fbf..8cb386a8 100644 --- a/lib/log.js +++ b/lib/log.js @@ -1,4 +1,5 @@ const {createLogger, format, transports} = require('winston') +const util = require('util') module.exports = (file) => { const logger = createLogger({ @@ -19,19 +20,13 @@ module.exports = (file) => { const obj = { info: (...args) => { - args.forEach((e) => { - logger.info(JSON.stringify(e)) - }) + logger.info(util.format('%j', args)) }, warn: (...args) => { - args.forEach((e) => { - logger.warn(JSON.stringify(e)) - }) + logger.warn(util.format('%j', args)) }, error: (...args) => { - args.forEach((e) => { - logger.error(JSON.stringify(e)) - }) + logger.error(util.format('%j', args)) } } From 657df0aaa8ed35dce7aaaa8f67b25afe9018b4fc Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 26 Apr 2020 20:53:07 +0900 Subject: [PATCH 03/11] #31: fixes log.js --- lib/log.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/log.js b/lib/log.js index 8cb386a8..df204d4c 100644 --- a/lib/log.js +++ b/lib/log.js @@ -20,13 +20,25 @@ module.exports = (file) => { const obj = { info: (...args) => { - logger.info(util.format('%j', args)) + const mappedArgs = args.map((e) => { + if (e instanceof Error) return e.toString + return e + }) + logger.info(util.format('%j', mappedArgs)) }, warn: (...args) => { - logger.warn(util.format('%j', args)) + const mappedArgs = args.map((e) => { + if (e instanceof Error) return e.toString + return e + }) + logger.warn(util.format('%j', mappedArgs)) }, error: (...args) => { - logger.error(util.format('%j', args)) + const mappedArgs = args.map((e) => { + if (e instanceof Error) return e.toString + return e + }) + logger.error(util.format('%j', mappedArgs)) } } From 959732b27a657acda92d342e5c404c2cce5338d4 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 26 Apr 2020 21:09:50 +0900 Subject: [PATCH 04/11] #31: fixes log.js --- lib/log.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/log.js b/lib/log.js index df204d4c..cf6f9883 100644 --- a/lib/log.js +++ b/lib/log.js @@ -21,21 +21,21 @@ module.exports = (file) => { const obj = { info: (...args) => { const mappedArgs = args.map((e) => { - if (e instanceof Error) return e.toString + if (e instanceof Error) return e.toString() return e }) logger.info(util.format('%j', mappedArgs)) }, warn: (...args) => { const mappedArgs = args.map((e) => { - if (e instanceof Error) return e.toString + if (e instanceof Error) return e.toString() return e }) logger.warn(util.format('%j', mappedArgs)) }, error: (...args) => { const mappedArgs = args.map((e) => { - if (e instanceof Error) return e.toString + if (e instanceof Error) return e.toString() return e }) logger.error(util.format('%j', mappedArgs)) From 03aa24613e05273845e17659146e762ffee064ce Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 27 Apr 2020 10:59:19 +0900 Subject: [PATCH 05/11] #31: adds log.test.js --- lib/log.test.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/log.test.js b/lib/log.test.js index a159152d..8595fff9 100644 --- a/lib/log.test.js +++ b/lib/log.test.js @@ -1,11 +1,19 @@ const winston = require('winston') -winston.createLogger = jest.fn() +const util = require('util') + describe('Testing log.js', () => { beforeEach(() => { jest.clearAllMocks() }) - it('should call winston.createLogger when log is required', () => { + it('should call util.format when log.info is called', () => { + util.format = jest.fn() const logger = require('./log')(__filename) + logger.info('message', [0,1,2], {obj: null}, new Error('error message')) + expect(util.format).toHaveBeenCalled() + }) + it('should call winston.createLogger when log is required', () => { + winston.createLogger = jest.fn() + const logger2 = require('./log')(__filename) expect(winston.createLogger).toHaveBeenCalled() }) }) \ No newline at end of file From c1e56f6a98746b6f1c4999fe444d886ec235b12a Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 27 Apr 2020 11:15:56 +0900 Subject: [PATCH 06/11] #31: added a helper function(processArgs) --- lib/log.js | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/lib/log.js b/lib/log.js index cf6f9883..045f090a 100644 --- a/lib/log.js +++ b/lib/log.js @@ -18,27 +18,22 @@ module.exports = (file) => { ] }) + const processArgs = (args) => { + return args.map((e) => { + if (e instanceof Error) return e.toString() + return e + }) + } + const obj = { info: (...args) => { - const mappedArgs = args.map((e) => { - if (e instanceof Error) return e.toString() - return e - }) - logger.info(util.format('%j', mappedArgs)) + logger.info(util.format('%j', processArgs(args))) }, warn: (...args) => { - const mappedArgs = args.map((e) => { - if (e instanceof Error) return e.toString() - return e - }) - logger.warn(util.format('%j', mappedArgs)) + logger.warn(util.format('%j', processArgs(args))) }, error: (...args) => { - const mappedArgs = args.map((e) => { - if (e instanceof Error) return e.toString() - return e - }) - logger.error(util.format('%j', mappedArgs)) + logger.error(util.format('%j', processArgs(args))) } } From 1038482fd0cb206bda36d0ddc0fb8b48d7c759d4 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 27 Apr 2020 20:51:06 +0900 Subject: [PATCH 07/11] #31: fixes log.test.js --- lib/log.test.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/log.test.js b/lib/log.test.js index a159152d..a734696c 100644 --- a/lib/log.test.js +++ b/lib/log.test.js @@ -1,11 +1,24 @@ const winston = require('winston') -winston.createLogger = jest.fn() +const util = require('util') + +winston.createLogger = jest.fn().mockReturnValue({ + info: () => {}, + warn: () => {}, + error: () => {} +}) + describe('Testing log.js', () => { beforeEach(() => { jest.clearAllMocks() }) - it('should call winston.createLogger when log is required', () => { + it('should call util.format when log.info is called', () => { + util.format = jest.fn() const logger = require('./log')(__filename) + logger.info('message', [0,1,2], {obj: null}) + expect(util.format).toHaveBeenCalled() + }) + it('should call winston.createLogger when log is required', () => { + const logger2 = require('./log')(__filename) expect(winston.createLogger).toHaveBeenCalled() }) }) \ No newline at end of file From 8fad7f598a9fb5bc37b8bcd13d0a68e2787ae9ae Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 27 Apr 2020 20:56:45 +0900 Subject: [PATCH 08/11] #31: fixes log.test.js --- lib/log.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/log.test.js b/lib/log.test.js index a734696c..3162e33d 100644 --- a/lib/log.test.js +++ b/lib/log.test.js @@ -14,10 +14,11 @@ describe('Testing log.js', () => { it('should call util.format when log.info is called', () => { util.format = jest.fn() const logger = require('./log')(__filename) - logger.info('message', [0,1,2], {obj: null}) + logger.info('message', [0,1,2], {obj: null}, new Error('error message')) expect(util.format).toHaveBeenCalled() }) it('should call winston.createLogger when log is required', () => { + winston.createLogger = jest.fn() const logger2 = require('./log')(__filename) expect(winston.createLogger).toHaveBeenCalled() }) From 59d1652624acf1ed7d214ee91e219794e496ab95 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 27 Apr 2020 21:33:09 +0900 Subject: [PATCH 09/11] fixes log.test.js --- lib/log.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/log.test.js b/lib/log.test.js index 8595fff9..2f963136 100644 --- a/lib/log.test.js +++ b/lib/log.test.js @@ -1,6 +1,12 @@ const winston = require('winston') const util = require('util') +winston.createLogger = jest.fn().mockReturnValue({ + info: () => {}, + warn: () => {}, + error: () => {} +}) + describe('Testing log.js', () => { beforeEach(() => { jest.clearAllMocks() @@ -12,7 +18,6 @@ describe('Testing log.js', () => { expect(util.format).toHaveBeenCalled() }) it('should call winston.createLogger when log is required', () => { - winston.createLogger = jest.fn() const logger2 = require('./log')(__filename) expect(winston.createLogger).toHaveBeenCalled() }) From 0c54de257c4e79920f3e1533ca94f3100a33960e Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 27 Apr 2020 21:33:54 +0900 Subject: [PATCH 10/11] #31: fixes log.test.js --- lib/log.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/log.test.js b/lib/log.test.js index 2f963136..f105a491 100644 --- a/lib/log.test.js +++ b/lib/log.test.js @@ -14,7 +14,7 @@ describe('Testing log.js', () => { it('should call util.format when log.info is called', () => { util.format = jest.fn() const logger = require('./log')(__filename) - logger.info('message', [0,1,2], {obj: null}, new Error('error message')) + logger.info('message', [0,1,2], {obj: () => {}}, new Error('error message')) expect(util.format).toHaveBeenCalled() }) it('should call winston.createLogger when log is required', () => { From 04203b93d1087cec33463af4b7d3bacba7d1ef21 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 30 Apr 2020 22:54:39 +0900 Subject: [PATCH 11/11] #31: to have 100% test coverage, show only filename, etc. --- lib/log.js | 17 +++++++++-------- lib/log.test.js | 30 ++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/log.js b/lib/log.js index 045f090a..cb379c57 100644 --- a/lib/log.js +++ b/lib/log.js @@ -6,12 +6,12 @@ module.exports = (file) => { format: format.combine( format.timestamp({format:'YYYY-MM-DD HH:mm:ss'}), format.label({ - label: file + label: file.split(/(\\|\/)/g).pop() }), format.colorize(), - format.printf( - info => `${info.timestamp} ${info.level} [${info.label}]: ${info.message}` - ) + format.printf((info) => { + return `${info.timestamp} ${info.level} [${info.label}]: ${info.message}` + }) ), transports : [ new transports.Console() @@ -19,21 +19,22 @@ module.exports = (file) => { }) const processArgs = (args) => { - return args.map((e) => { + const mappedArgs = args.map((e) => { if (e instanceof Error) return e.toString() return e }) + return util.format('%j', mappedArgs) } const obj = { info: (...args) => { - logger.info(util.format('%j', processArgs(args))) + logger.info(processArgs(args)) }, warn: (...args) => { - logger.warn(util.format('%j', processArgs(args))) + logger.warn(processArgs(args)) }, error: (...args) => { - logger.error(util.format('%j', processArgs(args))) + logger.error(processArgs(args)) } } diff --git a/lib/log.test.js b/lib/log.test.js index f105a491..b40c9d3c 100644 --- a/lib/log.test.js +++ b/lib/log.test.js @@ -7,18 +7,40 @@ winston.createLogger = jest.fn().mockReturnValue({ error: () => {} }) +winston.format = { + combine: () => {}, + timestamp: () => {}, + label: () => {}, + colorize: () => {}, + printf: jest.fn() +} + +util.format = jest.fn() + describe('Testing log.js', () => { beforeEach(() => { jest.clearAllMocks() }) - it('should call util.format when log.info is called', () => { - util.format = jest.fn() + it('should call util.format everytime when log.xxx is called', () => { const logger = require('./log')(__filename) - logger.info('message', [0,1,2], {obj: () => {}}, new Error('error message')) + logger.info('message', [0,1,2]) + logger.warn('message', {obj: () => {}}) + logger.error('message', new Error('error message')) expect(util.format).toHaveBeenCalled() }) it('should call winston.createLogger when log is required', () => { - const logger2 = require('./log')(__filename) + const logger = require('./log')(__filename) expect(winston.createLogger).toHaveBeenCalled() }) + it('should return log string correctly', () => { + const logger = require('./log')(__filename) + const argInPrintf = winston.format.printf.mock.calls[0][0] + const testData = { + timestamp: "2020-04-30 22:43:00", + level: "info", + label: "log.test.js", + message: "message" + } + expect(argInPrintf(testData)).toBe("2020-04-30 22:43:00 info [log.test.js]: message") + }) }) \ No newline at end of file