This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae706a0
commit 47bf5a9
Showing
17 changed files
with
372 additions
and
20 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
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,24 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Logger configuration when setting a level that doesn't exist should keep default info level 1`] = ` | ||
Array [ | ||
"[WeTransfer SDK] [33m\\"yolo\\" is not a valid logger level. Please specify one of the following levels: [[39m | ||
[33m 'error',[39m | ||
[33m 'warning',[39m | ||
[33m 'info',[39m | ||
[33m 'verbose,'[39m | ||
[33m 'debug',[39m | ||
[33m 'silly'[39m | ||
[33m ][39m | ||
", | ||
] | ||
`; | ||
|
||
exports[`Logger configuration when setting an empty level should keep default info level 1`] = `undefined`; | ||
|
||
exports[`Logger configuration when silly level is set should log silly logs 1`] = ` | ||
Array [ | ||
"[WeTransfer SDK] [35mThis is a silly log[39m | ||
", | ||
] | ||
`; |
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,48 @@ | ||
const logger = require('../../src/config/logger'); | ||
|
||
describe('Logger configuration', () => { | ||
let log; | ||
beforeEach(() => { | ||
logger.setLoggerLevel('info'); | ||
log = jest.spyOn(console._stdout, 'write').mockImplementation(() => { | ||
/* don't console.log anything */ | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
log.mockRestore(); | ||
}); | ||
|
||
describe('when silly level is set', () => { | ||
it('should log silly logs', () => { | ||
logger.setLoggerLevel('silly'); | ||
logger.silly('This is a silly log'); | ||
expect(log.mock.calls[log.mock.calls.length - 1]).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('when error level is set', () => { | ||
it('should NOT log silly logs', () => { | ||
logger.setLoggerLevel('error'); | ||
logger.silly('This is a silly log'); | ||
expect(log).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('when setting a level that doesn\'t exist', () => { | ||
it('should keep default info level', () => { | ||
logger.setLoggerLevel('yolo'); | ||
expect(log.mock.calls[log.mock.calls.length - 1]).toMatchSnapshot(); | ||
expect(logger.transports[0].level).toBe('info'); | ||
}); | ||
}); | ||
|
||
describe('when setting an empty level', () => { | ||
it('should keep default info level', () => { | ||
expect(logger.transports[0].level).toBe('info'); | ||
logger.setLoggerLevel(); | ||
expect(log.mock.calls[log.mock.calls.length - 1]).toMatchSnapshot(); | ||
expect(logger.transports[0].level).toBe('info'); | ||
}); | ||
}); | ||
}); |
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
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,3 @@ | ||
const logger = require('../../src/config/logger'); | ||
|
||
logger.setLoggerLevel('error'); |
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,45 @@ | ||
const { includes } = require('lodash'); | ||
const winston = require('winston'); | ||
const { createLogger, config, format } = winston; | ||
const { colorize, combine, label, printf } = format; | ||
|
||
const customFormat = printf((info) => `[${info.label}] ${info.message}`); | ||
|
||
const transports = { | ||
console: new winston.transports.Console({ level: 'info' }) | ||
}; | ||
|
||
const logger = createLogger({ | ||
levels: config.npm.levels, | ||
format: combine( | ||
colorize({ all: true }), | ||
label({ label: 'WeTransfer SDK' }), | ||
customFormat | ||
), | ||
transports: [transports.console] | ||
}); | ||
|
||
winston.addColors({ | ||
error: 'red', | ||
warn: 'yellow', | ||
info: 'white', | ||
debug: 'white' | ||
}); | ||
|
||
function setLoggerLevel(level = 'info') { | ||
if (!includes(Object.keys(config.npm.levels), level)) { | ||
return logger.warn(`"${level}" is not a valid logger level. Please specify one of the following levels: [ | ||
'error', | ||
'warning', | ||
'info', | ||
'verbose,' | ||
'debug', | ||
'silly' | ||
]`); | ||
} | ||
|
||
transports.console.level = level; | ||
} | ||
|
||
module.exports = logger; | ||
module.exports.setLoggerLevel = setLoggerLevel; |
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
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
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
Oops, something went wrong.