Skip to content

Commit 7d42c6d

Browse files
authored
perf(logger): defer execution of expensive logging calls (#614)
1 parent 0909ec4 commit 7d42c6d

File tree

5 files changed

+88
-44
lines changed

5 files changed

+88
-44
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
expect: true,
66
describe: true,
77
beforeEach: true,
8+
afterEach: true,
89
jest: true
910
}
1011
}

__tests__/integration.js

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const express = require('express')
44
const bodyParser = require('body-parser')
55
const ejs = require('ejs').__express
66
const serverlessExpress = require('../src/index')
7+
const serverlessExpressLogger = require('../src/logger')
78
const {
89
makeEvent,
910
makeResponse,
@@ -467,39 +468,75 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
467468
expect(response).toEqual(expectedResponse)
468469
})
469470

470-
test('custom logger', async () => {
471-
app = express()
472-
router = express.Router()
473-
app.use('/', router)
474-
router.get('/users', (req, res) => {
475-
res.json({})
471+
describe('logger', () => {
472+
const mocks = []
473+
474+
beforeEach(() => {
475+
const mockMethods = [
476+
'error',
477+
'info',
478+
'warn',
479+
'log',
480+
'debug'
481+
]
482+
483+
for (const method of mockMethods) { mocks.push(jest.spyOn(global.console, method).mockImplementation()) }
476484
})
477-
const event = makeEvent({
478-
eventSourceName,
479-
path: '/users',
480-
httpMethod: 'GET'
485+
486+
afterEach(() => {
487+
for (const mock of mocks) mock.mockRestore()
481488
})
482-
const customLogger = {
483-
error: jest.fn(),
484-
warn: jest.fn(),
485-
info: jest.fn(),
486-
verbose: jest.fn(),
487-
debug: jest.fn()
488-
}
489-
serverlessExpressInstance = serverlessExpress({ app, log: customLogger })
490-
await serverlessExpressInstance(event)
491489

492-
expect(customLogger.debug.mock.calls.length).toBe(6)
490+
test('custom logger', async () => {
491+
app = express()
492+
router = express.Router()
493+
app.use('/', router)
494+
router.get('/users', (req, res) => {
495+
res.json({})
496+
})
497+
const event = makeEvent({
498+
eventSourceName,
499+
path: '/users',
500+
httpMethod: 'GET'
501+
})
502+
const customLogger = {
503+
error: jest.fn(),
504+
warn: jest.fn(),
505+
info: jest.fn(),
506+
verbose: jest.fn(),
507+
debug: jest.fn()
508+
}
509+
serverlessExpressInstance = serverlessExpress({ app, log: customLogger })
510+
await serverlessExpressInstance(event)
511+
512+
expect(customLogger.debug.mock.calls.length).toBe(6)
493513

494-
// TODO: test log levels
495-
// customLogger.level = 'error'
496-
// customLogger.debug.mockClear()
497-
// customLogger.debug.mockReset()
498-
// customLogger.debug = jest.fn()
514+
// TODO: test log levels
515+
// customLogger.level = 'error'
516+
// customLogger.debug.mockClear()
517+
// customLogger.debug.mockReset()
518+
// customLogger.debug = jest.fn()
499519

500-
// serverlessExpressInstance = serverlessExpress({ app, log: customLogger })
501-
// await serverlessExpressInstance(event)
502-
// expect(customLogger.debug.mock.calls.length).toBe(0)
520+
// serverlessExpressInstance = serverlessExpress({ app, log: customLogger })
521+
// await serverlessExpressInstance(event)
522+
// expect(customLogger.debug.mock.calls.length).toBe(0)
523+
})
524+
525+
test('lazy print of logger', async () => {
526+
const logger = serverlessExpressLogger()
527+
528+
logger.debug('debug', () => '=true', ' works')
529+
logger.debug(() => 'debug')
530+
531+
expect(global.console.debug).not.toHaveBeenNthCalledWith(
532+
1,
533+
'debug=true works'
534+
)
535+
expect(global.console.debug).not.toHaveBeenNthCalledWith(
536+
2,
537+
'debug'
538+
)
539+
})
503540
})
504541

505542
test('legacy/deprecated createServer', async () => {

src/configure.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ function configure ({
4343
log = configureLog,
4444
respondWithErrors = configureRespondWithErrors
4545
}) {
46-
log.debug('SERVERLESS_EXPRESS:PROXY', {
46+
log.debug('SERVERLESS_EXPRESS:PROXY', () => ({
4747
event: util.inspect(event, { depth: null }),
4848
context: util.inspect(context, { depth: null }),
4949
resolutionMode,
5050
eventSourceName,
5151
binarySettings,
5252
respondWithErrors
53-
})
53+
}))
5454

5555
if (binaryMimeTypes) {
5656
console.warn('[DEPRECATION NOTICE] { binaryMimeTypes: [] } is deprecated. base64 encoding is now automatically determined based on response content-type and content-encoding. If you need to manually set binary content types, instead, use { binarySettings: { contentTypes: [] } }')

src/logger.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
1+
const lazyPrint = (value) => {
2+
if (typeof value === 'function') { return value() }
3+
4+
return value
5+
}
6+
17
function logger ({
28
level = 'error'
39
} = {}) {
410
return {
5-
error (message, additional) {
11+
error (message, ...additional) {
612
if (!level.includes('debug', 'verbose', 'info', 'warn', 'error')) return
713
console.error({
8-
message,
9-
...additional
14+
message: lazyPrint(message),
15+
...additional.map(lazyPrint)
1016
})
1117
},
12-
warn (message, additional) {
18+
warn (message, ...additional) {
1319
if (!level.includes('debug', 'verbose', 'info', 'warn')) return
1420
console.warn({
15-
message,
16-
...additional
21+
message: lazyPrint(message),
22+
...additional.map(lazyPrint)
1723
})
1824
},
1925
info (message, additional) {
2026
if (!level.includes('debug', 'verbose', 'info')) return
2127
console.info({
22-
message,
23-
...additional
28+
message: lazyPrint(message),
29+
...additional.map(lazyPrint)
2430
})
2531
},
2632
verbose (message, additional) {
2733
if (!level.includes('debug', 'verbose')) return
2834
console.debug({
29-
message,
30-
...additional
35+
message: lazyPrint(message),
36+
...additional.map(lazyPrint)
3137
})
3238
},
3339
debug (message, additional) {
3440
if (level !== 'debug') return
3541
console.debug({
36-
message,
37-
...additional
42+
message: lazyPrint(message),
43+
...additional.map(lazyPrint)
3844
})
3945
}
4046
}

src/transport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ function forwardResponse ({
3939
response
4040
})
4141

42-
log.debug('SERVERLESS_EXPRESS:FORWARD_RESPONSE:EVENT_SOURCE_RESPONSE', {
42+
log.debug('SERVERLESS_EXPRESS:FORWARD_RESPONSE:EVENT_SOURCE_RESPONSE', () => ({
4343
successResponse: util.inspect(successResponse, { depth: null }),
4444
body: logBody
45-
})
45+
}))
4646

4747
resolver.succeed({
4848
response: successResponse

0 commit comments

Comments
 (0)