Skip to content

Commit

Permalink
fix: only show debug logs in 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-vendia committed Feb 2, 2021
1 parent 9543516 commit b0c4814
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
35 changes: 35 additions & 0 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,39 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
})
expect(response).toEqual(expectedResponse)
})

test('custom logger', async () => {
app = express()
router = express.Router()
app.use('/', router)
router.get('/users', (req, res) => {
res.json({})
})
const event = makeEvent({
eventSourceName,
path: '/users',
httpMethod: 'GET'
})
const customLogger = {
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
verbose: jest.fn(),
debug: jest.fn()
}
serverlessExpressInstance = serverlessExpress({ app, log: customLogger })
await serverlessExpressInstance.handler(event)

expect(customLogger.debug.mock.calls.length).toBe(6)

// TODO: test log levels
// customLogger.level = 'error'
// customLogger.debug.mockClear()
// customLogger.debug.mockReset()
// customLogger.debug = jest.fn()

// serverlessExpressInstance = serverlessExpress({ app, log: customLogger })
// await serverlessExpressInstance.handler(event)
// expect(customLogger.debug.mock.calls.length).toBe(0)
})
})
25 changes: 22 additions & 3 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
const logger = {
// TODO: allow users to set log level without having to provide the other log methods
level: process.env === 'development' ? 'debug' : 'error',
error (message, additional) {
if (!logger.level.includes('debug', 'verbose', 'info', 'warn', 'error')) return
console.error({
message,
...additional
})
},
warn (message, additional) {
if (!logger.level.includes('debug', 'verbose', 'info', 'warn')) return
console.warn({
message,
...additional
})
},
info (message, additional) {
if (!logger.level.includes('debug', 'verbose', 'info')) return
console.info({
message,
...additional
})
},
debug (message, additional) {
verbose (message, additional) {
if (!logger.level.includes('debug', 'verbose')) return
console.debug({
message,
...additional
})
},
error (message, additional) {
console.error({
debug (message, additional) {
if (logger.level !== 'debug') return
console.debug({
message,
...additional
})
Expand Down

0 comments on commit b0c4814

Please sign in to comment.