Skip to content

Commit 344bd82

Browse files
refactor: create distinct log methods
1 parent e9cfaf5 commit 344bd82

File tree

2 files changed

+117
-15
lines changed

2 files changed

+117
-15
lines changed

src/helpers/logger.js

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
// @ts-check
22

33
/**
4-
*
4+
* We really only need three log levels
5+
* * Error
6+
* * Info
7+
* * Verbose
8+
* For the purposes of minimal refactor, I'm aliasing debug() to verbose()
9+
* I'm also mapping the log() options to the new calls and
10+
* deprecating log()
11+
*/
12+
13+
function _getTimestamp() {
14+
return new Date().toISOString()
15+
}
16+
17+
/**
18+
* @deprecated - Please use error(), info(), or verbose()
519
* @param {string} message
620
* @param {Object} [options]
721
* @param {string} [options.level]
@@ -11,24 +25,56 @@
1125
*/
1226
function log(message, options) {
1327
if (!options || !options.level) {
14-
return console.log(message)
28+
return info(message)
1529
}
1630
switch (options.level) {
1731
case 'debug':
1832
if (options.args && options.args.verbose) {
19-
console.debug(message)
33+
return verbose(message, true)
2034
}
2135

2236
break
2337
case 'error':
24-
console.error(message)
25-
break
38+
return error(message)
2639
default:
27-
console.log(message)
28-
break
40+
return info(message)
41+
}
42+
}
43+
44+
/**
45+
*
46+
* @param {string} message - message to log
47+
* @param {boolean} shouldVerbose - pass the value of the verbose flag
48+
* @return void
49+
*/
50+
function verbose(message, shouldVerbose = false) {
51+
if (shouldVerbose === true) {
52+
console.debug(`[${_getTimestamp()}] ['verbose'] ${message}`)
2953
}
3054
}
3155

56+
/**
57+
*
58+
* @param {string} message - message to log
59+
* @return void
60+
*/
61+
function error(message) {
62+
console.error(`[${_getTimestamp()}] ['error'] ${message}`)
63+
}
64+
65+
/**
66+
*
67+
* @param {string} message - message to log
68+
* @return void
69+
*/
70+
function info(message) {
71+
console.log(`[${_getTimestamp()}] ['info'] ${message}`)
72+
}
73+
3274
module.exports = {
75+
debug: verbose,
76+
error,
77+
info,
3378
log,
79+
verbose,
3480
}

test/helpers/logger.test.js

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// @ts-check
2-
const logger = require('../../src/helpers/logger')
2+
const { error, info, verbose, log } = require('../../src/helpers/logger')
33
const { expect, it } = require('@jest/globals')
44

5-
describe('Logger Helper', () => {
5+
describe('Logger Helper - Legacy log() tests', () => {
66
afterEach(() => {
7-
jest.restoreAllMocks()
7+
jest.clearAllMocks()
88
})
99

1010
it('Should call logger with default options', () => {
1111
// eslint-disable-next-line
1212
jest.spyOn(console, 'log').mockImplementation(() => {})
13-
logger.log('message with no options')
13+
log('message with no options')
1414
expect(console.log).toHaveBeenCalledWith(
1515
expect.stringMatching(/no options/),
1616
)
@@ -19,15 +19,15 @@ describe('Logger Helper', () => {
1919
it('Should not call logger with default options.level = debug and verbose not set', () => {
2020
// eslint-disable-next-line
2121
jest.spyOn(console, 'debug').mockImplementation(() => {})
22-
logger.log('message with debug level', { level: 'debug' })
22+
log('message with debug level', { level: 'debug' })
2323
expect(console.debug).not.toHaveBeenCalled()
2424
})
2525

2626
it('Should call logger with options.level = debug and verbose set', () => {
2727
jest.spyOn(console, 'debug').mockImplementation(() => {
2828
// Intentionally empty
2929
})
30-
logger.log('message with debug level and verbose', {
30+
log('message with debug level and verbose', {
3131
level: 'debug',
3232
args: { verbose: true },
3333
})
@@ -40,7 +40,7 @@ describe('Logger Helper', () => {
4040
jest.spyOn(console, 'error').mockImplementation(() => {
4141
// Intentionally empty
4242
})
43-
logger.log('message with error level', { level: 'error' })
43+
log('message with error level', { level: 'error' })
4444
expect(console.error).toHaveBeenCalledWith(
4545
expect.stringMatching(/error level/),
4646
)
@@ -50,7 +50,63 @@ describe('Logger Helper', () => {
5050
jest.spyOn(console, 'log').mockImplementation(() => {
5151
// Intentionally empty
5252
})
53-
logger.log('message with error level of foobar', { level: 'foobar' })
53+
log('message with error level of foobar', { level: 'foobar' })
5454
expect(console.log).toHaveBeenCalledWith(expect.stringMatching(/of foobar/))
5555
})
5656
})
57+
58+
describe('Logging methods', () => {
59+
afterEach(() => {
60+
jest.clearAllMocks()
61+
})
62+
63+
it('should call console.error() when error() is called', () => {
64+
jest.spyOn(console, 'error').mockImplementation(() => {
65+
// Intentionally empty
66+
})
67+
error('this is a test error')
68+
expect(console.error).toHaveBeenCalledWith(
69+
expect.stringMatching(/\['error'\]/),
70+
)
71+
expect(console.error).toHaveBeenCalledWith(
72+
expect.stringMatching(/this is a test error/),
73+
)
74+
expect(console.error).toHaveBeenCalledWith(expect.stringMatching(/Z\]/))
75+
})
76+
77+
it('should call console.debug() when verbose() is called with shouldVerbose === true', () => {
78+
jest.spyOn(console, 'debug').mockImplementation(() => {
79+
// Intentionally empty
80+
})
81+
verbose('this is a test verbose', true)
82+
expect(console.debug).toHaveBeenCalledWith(
83+
expect.stringMatching(/\['verbose'\]/),
84+
)
85+
expect(console.debug).toHaveBeenCalledWith(
86+
expect.stringMatching(/this is a test verbose/),
87+
)
88+
expect(console.debug).toHaveBeenCalledWith(expect.stringMatching(/Z\]/))
89+
})
90+
91+
it('should not call console.debug() when verbose() is called without shouldVerbose', () => {
92+
jest.spyOn(console, 'debug').mockImplementation(() => {
93+
// Intentionally empty
94+
})
95+
verbose('this is a test verbose')
96+
expect(console.debug).not.toBeCalled()
97+
})
98+
99+
it('should call console.log() when info() is called ', () => {
100+
jest.spyOn(console, 'log').mockImplementation(() => {
101+
// Intentionally empty
102+
})
103+
info('this is a test info')
104+
expect(console.log).toHaveBeenCalledWith(
105+
expect.stringMatching(/\['info'\]/),
106+
)
107+
expect(console.log).toHaveBeenCalledWith(
108+
expect.stringMatching(/this is a test info/),
109+
)
110+
expect(console.log).toHaveBeenCalledWith(expect.stringMatching(/Z\]/))
111+
})
112+
})

0 commit comments

Comments
 (0)