Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(client): create log module into utils (#1953)
- Loading branch information
1 parent
1581adc
commit 6973e4c
Showing
9 changed files
with
152 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
client | ||
/client | ||
!/test/client | ||
coverage | ||
client-src/live/web_modules/ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
client | ||
/client | ||
!/test/client | ||
coverage | ||
client-src/live/web_modules/ | ||
node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const log = require('loglevel').getLogger('webpack-dev-server'); | ||
|
||
const INFO = 'info'; | ||
const WARN = 'warn'; | ||
const ERROR = 'error'; | ||
const DEBUG = 'debug'; | ||
const TRACE = 'trace'; | ||
const SILENT = 'silent'; | ||
// deprecated | ||
// TODO: remove these at major released | ||
// https://github.com/webpack/webpack-dev-server/pull/1825 | ||
const WARNING = 'warning'; | ||
const NONE = 'none'; | ||
|
||
// Set the default log level | ||
log.setDefaultLevel(INFO); | ||
|
||
function setLogLevel(level) { | ||
switch (level) { | ||
case INFO: | ||
case WARN: | ||
case ERROR: | ||
case DEBUG: | ||
case TRACE: | ||
log.setLevel(level); | ||
break; | ||
// deprecated | ||
case WARNING: | ||
// loglevel's warning name is different from webpack's | ||
log.setLevel('warn'); | ||
break; | ||
// deprecated | ||
case NONE: | ||
case SILENT: | ||
log.disableAll(); | ||
break; | ||
default: | ||
log.error(`[WDS] Unknown clientLogLevel '${level}'`); | ||
} | ||
} | ||
|
||
module.exports = { | ||
log, | ||
setLogLevel, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`log should output exception log when the level is unknown 1`] = `"[WDS] Unknown clientLogLevel 'foo'"`; | ||
|
||
exports[`log should set log level via setLogLevel 1`] = ` | ||
Array [ | ||
Array [ | ||
"info", | ||
], | ||
Array [ | ||
"warn", | ||
], | ||
Array [ | ||
"error", | ||
], | ||
Array [ | ||
"debug", | ||
], | ||
Array [ | ||
"trace", | ||
], | ||
Array [ | ||
"warn", | ||
], | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
describe('log', () => { | ||
let logMock; | ||
let setLogLevel; | ||
|
||
beforeEach(() => { | ||
jest.mock('loglevel'); | ||
// eslint-disable-next-line global-require | ||
logMock = require('loglevel'); | ||
logMock.getLogger.mockImplementation(() => { | ||
return { | ||
error: jest.fn(), | ||
setLevel: jest.fn(), | ||
setDefaultLevel: jest.fn(), | ||
disableAll: jest.fn(), | ||
}; | ||
}); | ||
|
||
// eslint-disable-next-line global-require | ||
setLogLevel = require('../../../../client-src/default/utils/log') | ||
.setLogLevel; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
jest.resetModules(); | ||
}); | ||
|
||
test('should set info as the default level', () => { | ||
const setDefaultLevel = | ||
logMock.getLogger.mock.results[0].value.setDefaultLevel; | ||
|
||
expect(setDefaultLevel).toBeCalled(); | ||
expect(setDefaultLevel.mock.calls[0][0]).toEqual('info'); | ||
}); | ||
|
||
test('should set log level via setLogLevel', () => { | ||
['info', 'warn', 'error', 'debug', 'trace', 'warning'].forEach((level) => { | ||
setLogLevel(level); | ||
}); | ||
|
||
expect( | ||
logMock.getLogger.mock.results[0].value.setLevel.mock.calls | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
test('should set none and silent via setLogLevel', () => { | ||
['none', 'silent'].forEach((level) => { | ||
setLogLevel(level); | ||
}); | ||
|
||
expect( | ||
logMock.getLogger.mock.results[0].value.disableAll.mock.results | ||
).toHaveLength(2); | ||
}); | ||
|
||
test('should output exception log when the level is unknown', () => { | ||
setLogLevel('foo'); | ||
|
||
expect( | ||
logMock.getLogger.mock.results[0].value.error.mock.calls[0][0] | ||
).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters