-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathindex.js
More file actions
34 lines (27 loc) · 1.11 KB
/
index.js
File metadata and controls
34 lines (27 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import Promise from 'bluebird'
import { getLogGroup } from '../util/aws/cloudwatch-logs'
import { getAliasVersion } from '../util/aws/lambda'
import getLogs from '../util/get-logs'
import { lambdaConfig } from '../util/load'
export default async function ({ name, env, time = Infinity, logger = () => {} }) {
const { FunctionName } = await lambdaConfig(name, env)
const aliasName = env
const [logGroupName, functionVersion] = await Promise.all([getLogGroup({ FunctionName }), getAliasVersion({ functionName: FunctionName, aliasName })])
const logs = await getLogs({ logGroupName, functionVersion, timeLeft: time, logger })
return printEventsLoop(logs)
}
async function printEventsLoop ({ events, logger, nextLogCall }) {
const rate = 1000
if (events !== undefined && events.length !== 0) {
logger(events.map(formatEvent).join(''))
}
if (nextLogCall === undefined) {
return Promise.resolve()
}
await Promise.delay(rate)
const logs = await nextLogCall(rate)
return printEventsLoop(logs)
}
function formatEvent ({ timestamp, message }) {
return `[${new Date(timestamp).toTimeString()}]: ${message}`
}