forked from adieuadieu/alagarr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.ts
100 lines (88 loc) · 2.81 KB
/
log.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as querystring from 'querystring'
import {
InterfaceAlagarrOptions,
InterfaceRequest,
InterfaceResponseData,
} from '../../types'
function aws(request: InterfaceRequest): object {
const { headers: requestHeaders, requestContext, context } = request
return {
awsAccountId: requestContext.accountId,
deploymentStage: requestContext.stage,
// API Gateway stuff
apig: {
requestId: requestContext.requestId,
resourcePath: requestContext.resourcePath,
},
// cloudfront
cloudfront: {
country: requestHeaders['cloudfront-viewer-country'],
desktop: requestHeaders['cloudfront-is-desktop-viewer'] === 'true',
mobile: requestHeaders['cloudfront-is-mobile-viewer'] === 'true',
smarttv: requestHeaders['cloudfront-is-smarttv-viewer'] === 'true',
tablet: requestHeaders['cloudfront-is-tablet-viewer'] === 'true',
},
lambda: {
functionName: context.functionName,
memoryLimit: Number(context.memoryLimitInMB),
remainingExecutionTime:
context.getRemainingTimeInMillis && context.getRemainingTimeInMillis(),
requestId: context.awsRequestId,
},
sourceIp: requestContext.identity.sourceIp,
}
}
export function logger(
request: InterfaceRequest,
response: InterfaceResponseData,
): any {
const {
source,
headers: requestHeaders,
meta,
requestContext,
provider,
} = request
const error = response.statusCode >= 500 && response.statusCode < 600
const logEntry = {
dateTime: new Date().toISOString(),
eventSource: source,
hostname: request.hostname,
httpMethod: requestContext.httpMethod,
httpProtocol: requestHeaders.via && requestHeaders.via.split(' ')[0],
httpReferrer: requestHeaders.referer,
httpUserAgent: requestHeaders['user-agent'],
requestContentLength: (request.body && request.body.length) || 0,
requestTime: Date.now() - request.timestamp,
requestUri:
request.path +
(request.query && Object.keys(request.query).length
? `?${querystring.stringify(request.query)}`
: ''),
responseContentLength: response.headers['content-length'],
statusCode: response.statusCode,
xForwardedHost: requestHeaders['x-forwarded-host'],
...meta,
// Provider specific properties
...(provider === 'aws' && aws(request)),
}
return (
(error ? process.stderr : process.stdout).write(JSON.stringify(logEntry)) &&
logEntry
)
}
// Logs request/response info
export default function logResponse(
response: InterfaceResponseData,
request: InterfaceRequest,
options: InterfaceAlagarrOptions,
): InterfaceResponseData {
return (
(options.enableLogger &&
((typeof options.logger === 'function'
? options.logger(request, response)
: logger(request, response)) &&
response)) ||
response
)
}