Skip to content

Commit 4400825

Browse files
committed
feat: set logger
1 parent e5361a6 commit 4400825

File tree

4 files changed

+225
-8
lines changed

4 files changed

+225
-8
lines changed

libs/controller.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'tslib'
2+
import bunyan from 'bunyan'
23
import { CloudFront } from 'aws-sdk'
34
import ConfigUpdator from './configUpdator'
45
import { ControllerClient, EventType } from './models'
6+
import { getLogger } from './logger'
57

68
export class LambdaEdgeController {
79
// CloudFront client from AWS SDK
@@ -10,6 +12,12 @@ export class LambdaEdgeController {
1012
// CloudFront config updator
1113
private updator: ConfigUpdator
1214

15+
// Log request / response
16+
private isDebug = false
17+
18+
// Logger
19+
private logger: bunyan = getLogger('LambdaEdgeController')
20+
1321
/**
1422
* constructor
1523
* @param {string} lambdaArn - Lambda arn
@@ -21,21 +29,43 @@ export class LambdaEdgeController {
2129
this.updator = client && client.configUpdator ? client.configUpdator : new ConfigUpdator(lambdaArn, eventType)
2230
}
2331

32+
/**
33+
* Disable logger
34+
*/
35+
public disableDebugger (): this {
36+
this.isDebug = false
37+
return this
38+
}
39+
40+
/**
41+
* Enable logger
42+
*/
43+
public enableDebugger (): this {
44+
this.isDebug = true
45+
return this
46+
}
47+
2448
/**
2549
* Reove lambda edge function from specific CloudFront Distribution
2650
*
2751
* @param {string} distributionId - CloudFront Distribution ID
2852
* @return {Promise} results of the workflow
2953
**/
3054
public async detachEdgeFunction (distributionId: string): Promise<CloudFront.UpdateDistributionResult> {
31-
const data = await this.cloudfront.getDistribution({ Id: distributionId }).promise()
55+
const req = { Id: distributionId }
56+
if (this.isDebug) this.logger.info({ data: req, action: 'getDistribution' })
57+
const data = await this.cloudfront.getDistribution(req).promise()
58+
if (this.isDebug) this.logger.info({ data, action: 'getDistribution' })
3259
if (!data.Distribution) throw new Error('No such distribution')
3360
const config = await this.updator.createUpdateDistributionConfig(
3461
data.Distribution.DistributionConfig,
3562
'detachEdge'
3663
)
3764
const params = this.updator.createUpdateDistributionParam(data, config)
38-
return this.cloudfront.updateDistribution(params).promise()
65+
if (this.isDebug) this.logger.info({ data: params, action: 'updateDistribution' })
66+
const result = await this.cloudfront.updateDistribution(params).promise()
67+
if (this.isDebug) this.logger.info({ data: result, action: 'updateDistribution' })
68+
return result
3969
}
4070

4171
/**
@@ -45,14 +75,20 @@ export class LambdaEdgeController {
4575
* @return {Promise} results of the workflow
4676
**/
4777
public async attachEdgeFunction (distributionId: string): Promise<CloudFront.UpdateDistributionResult> {
48-
const data = await this.cloudfront.getDistribution({ Id: distributionId }).promise()
78+
const req = { Id: distributionId }
79+
if (this.isDebug) this.logger.info({ data: req, action: 'getDistribution' })
80+
const data = await this.cloudfront.getDistribution(req).promise()
81+
if (this.isDebug) this.logger.info({ data, action: 'getDistribution' })
4982
if (!data || !data.Distribution) throw new Error('No such distribution')
5083
const config = await this.updator.createUpdateDistributionConfig(
5184
data.Distribution.DistributionConfig,
5285
'attachEdge'
5386
)
5487
const params = this.updator.createUpdateDistributionParam(data, config)
55-
return this.cloudfront.updateDistribution(params).promise()
88+
if (this.isDebug) this.logger.info({ data: params, action: 'udateDistribution' })
89+
const result = await this.cloudfront.updateDistribution(params).promise()
90+
if (this.isDebug) this.logger.info({ data: result, action: 'updateDistribution' })
91+
return result
5692
}
5793
}
5894

libs/logger.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
import bunyan from 'bunyan'
3+
import BunyanFormat from 'bunyan-format'
4+
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
5+
6+
export const getLogger = (appName: string): bunyan => {
7+
return bunyan.createLogger({
8+
name: appName,
9+
level: 'debug',
10+
stream: new BunyanFormat({ outputMode: 'bunyan', levelInString: true })
11+
})
12+
}
13+
14+
export class Logger {
15+
private static getLogger (appName: string): bunyan {
16+
return getLogger(appName)
17+
}
18+
19+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20+
public static record (appName: string, level: LogLevel, message: string, ...args: any[]): void {
21+
switch (level) {
22+
case 'trace':
23+
return this.trace(appName, message, args)
24+
case 'debug':
25+
return this.debug(appName, message, args)
26+
case 'info':
27+
return this.info(appName, message, args)
28+
case 'warn':
29+
return this.warn(appName, message, args)
30+
case 'error':
31+
return this.error(appName, message, args)
32+
case 'fatal':
33+
return this.fatal(appName, message, args)
34+
default:
35+
}
36+
}
37+
38+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
39+
public static trace (appName: string, message: string, ...args: any[]): void {
40+
const logger = this.getLogger(appName)
41+
logger.trace(message, args)
42+
}
43+
44+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
45+
public static debug (appName: string, message: string, ...args: any[]): void {
46+
const logger = this.getLogger(appName)
47+
logger.debug(message, args)
48+
}
49+
50+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
51+
public static info (appName: string, message: string, ...args: any[]): void {
52+
const logger = this.getLogger(appName)
53+
logger.info(message, args)
54+
}
55+
56+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
57+
public static warn (appName: string, message: string, ...args: any[]): void {
58+
const logger = this.getLogger(appName)
59+
logger.warn(message, args)
60+
}
61+
62+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
63+
public static error (appName: string, message: string, ...args: any[]): void {
64+
const logger = this.getLogger(appName)
65+
logger.error(message, args)
66+
}
67+
68+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
69+
public static fatal (appName: string, message: string, ...args: any[]): void {
70+
const logger = this.getLogger(appName)
71+
logger.fatal(message, args)
72+
}
73+
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
"devDependencies": {
5959
"@commitlint/cli": "^8.1.0",
6060
"@commitlint/config-conventional": "^8.1.0",
61+
"@types/bunyan": "^1.8.6",
62+
"@types/bunyan-format": "^0.2.0",
6163
"@types/jest": "^24.0.18",
6264
"@types/node": "^12.7.3",
6365
"@typescript-eslint/eslint-plugin": "^2.0.0",
@@ -82,6 +84,8 @@
8284
"typescript": "^3.6.2"
8385
},
8486
"dependencies": {
85-
"aws-sdk": "^2.521.0"
87+
"aws-sdk": "^2.521.0",
88+
"bunyan": "^1.8.12",
89+
"bunyan-format": "^0.2.1"
8690
}
8791
}

yarn.lock

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,20 @@
492492
dependencies:
493493
"@babel/types" "^7.3.0"
494494

495+
"@types/bunyan-format@^0.2.0":
496+
version "0.2.0"
497+
resolved "https://registry.yarnpkg.com/@types/bunyan-format/-/bunyan-format-0.2.0.tgz#de79bf9fe67ea0dcf5416c5f1acc46abb0bb68d8"
498+
integrity sha512-e87MeYM9rZUaQ7UgNU5webttfTlJWhc8oFU/hQBe/GGcf3bspY4ajVHqF1YgbysXnCwZDZgAq1FEe8ufFZZsLw==
499+
dependencies:
500+
"@types/node" "*"
501+
502+
"@types/bunyan@^1.8.6":
503+
version "1.8.6"
504+
resolved "https://registry.yarnpkg.com/@types/bunyan/-/bunyan-1.8.6.tgz#6527641cca30bedec5feb9ab527b7803b8000582"
505+
integrity sha512-YiozPOOsS6bIuz31ilYqR5SlLif4TBWsousN2aCWLi5233nZSX19tFbcQUPdR7xJ8ypPyxkCGNxg0CIV5n9qxQ==
506+
dependencies:
507+
"@types/node" "*"
508+
495509
"@types/eslint-visitor-keys@^1.0.0":
496510
version "1.0.0"
497511
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
@@ -734,6 +748,16 @@ ansi-styles@^3.2.0, ansi-styles@^3.2.1:
734748
dependencies:
735749
color-convert "^1.9.0"
736750

751+
ansicolors@~0.2.1:
752+
version "0.2.1"
753+
resolved "https://registry.yarnpkg.com/ansicolors/-/ansicolors-0.2.1.tgz#be089599097b74a5c9c4a84a0cdbcdb62bd87aef"
754+
integrity sha1-vgiVmQl7dKXJxKhKDNvNtivYeu8=
755+
756+
ansistyles@~0.1.1:
757+
version "0.1.3"
758+
resolved "https://registry.yarnpkg.com/ansistyles/-/ansistyles-0.1.3.tgz#5de60415bda071bb37127854c864f41b23254539"
759+
integrity sha1-XeYEFb2gcbs3EnhUyGT0GyMlRTk=
760+
737761
any-observable@^0.3.0:
738762
version "0.3.0"
739763
resolved "https://registry.yarnpkg.com/any-observable/-/any-observable-0.3.0.tgz#af933475e5806a67d0d7df090dd5e8bef65d119b"
@@ -1045,6 +1069,25 @@ buffer@4.9.1:
10451069
ieee754 "^1.1.4"
10461070
isarray "^1.0.0"
10471071

1072+
bunyan-format@^0.2.1:
1073+
version "0.2.1"
1074+
resolved "https://registry.yarnpkg.com/bunyan-format/-/bunyan-format-0.2.1.tgz#a4b3b0d80070a865279417269e3f00ff02fbcb47"
1075+
integrity sha1-pLOw2ABwqGUnlBcmnj8A/wL7y0c=
1076+
dependencies:
1077+
ansicolors "~0.2.1"
1078+
ansistyles "~0.1.1"
1079+
xtend "~2.1.1"
1080+
1081+
bunyan@^1.8.12:
1082+
version "1.8.12"
1083+
resolved "https://registry.yarnpkg.com/bunyan/-/bunyan-1.8.12.tgz#f150f0f6748abdd72aeae84f04403be2ef113797"
1084+
integrity sha1-8VDw9nSKvdcq6uhPBEA74u8RN5c=
1085+
optionalDependencies:
1086+
dtrace-provider "~0.8"
1087+
moment "^2.10.6"
1088+
mv "~2"
1089+
safe-json-stringify "~1"
1090+
10481091
cache-base@^1.0.1:
10491092
version "1.0.1"
10501093
resolved "https://registry.yarnpkg.com/cache-base/-/cache-base-1.0.1.tgz#0a7f46416831c8b662ee36fe4e7c59d76f666ab2"
@@ -1822,6 +1865,13 @@ dot-prop@^3.0.0:
18221865
dependencies:
18231866
is-obj "^1.0.0"
18241867

1868+
dtrace-provider@~0.8:
1869+
version "0.8.8"
1870+
resolved "https://registry.yarnpkg.com/dtrace-provider/-/dtrace-provider-0.8.8.tgz#2996d5490c37e1347be263b423ed7b297fb0d97e"
1871+
integrity sha512-b7Z7cNtHPhH9EJhNNbbeqTcXB8LGFFZhq1PGgEvpeHlzd36bhbdTWoE/Ba/YguqpBSlAPKnARWhVlhunCMwfxg==
1872+
dependencies:
1873+
nan "^2.14.0"
1874+
18251875
duplexer3@^0.1.4:
18261876
version "0.1.4"
18271877
resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2"
@@ -2555,6 +2605,17 @@ glob-parent@^5.0.0:
25552605
dependencies:
25562606
is-glob "^4.0.1"
25572607

2608+
glob@^6.0.1:
2609+
version "6.0.4"
2610+
resolved "https://registry.yarnpkg.com/glob/-/glob-6.0.4.tgz#0f08860f6a155127b2fadd4f9ce24b1aab6e4d22"
2611+
integrity sha1-DwiGD2oVUSey+t1PnOJLGqtuTSI=
2612+
dependencies:
2613+
inflight "^1.0.4"
2614+
inherits "2"
2615+
minimatch "2 || 3"
2616+
once "^1.3.0"
2617+
path-is-absolute "^1.0.0"
2618+
25582619
glob@^7.0.0, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3:
25592620
version "7.1.4"
25602621
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.4.tgz#aa608a2f6c577ad357e1ae5a5c26d9a8d1969255"
@@ -4154,7 +4215,7 @@ mimic-response@^1.0.0:
41544215
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.1.tgz#4923538878eef42063cb8a3e3b0798781487ab1b"
41554216
integrity sha512-j5EctnkH7amfV/q5Hgmoal1g2QHFJRraOtmx0JpIqkxhBhI/lJSl1nMpQ45hVarwNETOoWEimndZ4QK0RHxuxQ==
41564217

4157-
minimatch@^3.0.0, minimatch@^3.0.4:
4218+
"minimatch@2 || 3", minimatch@^3.0.0, minimatch@^3.0.4:
41584219
version "3.0.4"
41594220
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
41604221
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
@@ -4207,7 +4268,7 @@ mixin-deep@^1.2.0:
42074268
for-in "^1.0.2"
42084269
is-extendable "^1.0.1"
42094270

4210-
mkdirp@0.x, mkdirp@^0.5.0, mkdirp@^0.5.1:
4271+
mkdirp@0.x, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.1:
42114272
version "0.5.1"
42124273
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
42134274
integrity sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=
@@ -4219,6 +4280,11 @@ modify-values@^1.0.0:
42194280
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
42204281
integrity sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw==
42214282

4283+
moment@^2.10.6:
4284+
version "2.24.0"
4285+
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
4286+
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==
4287+
42224288
ms@2.0.0:
42234289
version "2.0.0"
42244290
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
@@ -4234,7 +4300,16 @@ mute-stream@0.0.7:
42344300
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
42354301
integrity sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=
42364302

4237-
nan@^2.12.1:
4303+
mv@~2:
4304+
version "2.1.1"
4305+
resolved "https://registry.yarnpkg.com/mv/-/mv-2.1.1.tgz#ae6ce0d6f6d5e0a4f7d893798d03c1ea9559b6a2"
4306+
integrity sha1-rmzg1vbV4KT32JN5jQPB6pVZtqI=
4307+
dependencies:
4308+
mkdirp "~0.5.1"
4309+
ncp "~2.0.0"
4310+
rimraf "~2.4.0"
4311+
4312+
nan@^2.12.1, nan@^2.14.0:
42384313
version "2.14.0"
42394314
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
42404315
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==
@@ -4261,6 +4336,11 @@ natural-compare@^1.4.0:
42614336
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
42624337
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=
42634338

4339+
ncp@~2.0.0:
4340+
version "2.0.0"
4341+
resolved "https://registry.yarnpkg.com/ncp/-/ncp-2.0.0.tgz#195a21d6c46e361d2fb1281ba38b91e9df7bdbb3"
4342+
integrity sha1-GVoh1sRuNh0vsSgbo4uR6d9727M=
4343+
42644344
needle@^2.2.1:
42654345
version "2.4.0"
42664346
resolved "https://registry.yarnpkg.com/needle/-/needle-2.4.0.tgz#6833e74975c444642590e15a750288c5f939b57c"
@@ -4427,6 +4507,11 @@ object-keys@^1.0.12:
44274507
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.1.1.tgz#1c47f272df277f3b1daf061677d9c82e2322c60e"
44284508
integrity sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==
44294509

4510+
object-keys@~0.4.0:
4511+
version "0.4.0"
4512+
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-0.4.0.tgz#28a6aae7428dd2c3a92f3d95f21335dd204e0336"
4513+
integrity sha1-KKaq50KN0sOpLz2V8hM13SBOAzY=
4514+
44304515
object-visit@^1.0.0:
44314516
version "1.0.1"
44324517
resolved "https://registry.yarnpkg.com/object-visit/-/object-visit-1.0.1.tgz#f79c4493af0c5377b59fe39d395e41042dd045bb"
@@ -5233,6 +5318,13 @@ rimraf@^3.0.0:
52335318
dependencies:
52345319
glob "^7.1.3"
52355320

5321+
rimraf@~2.4.0:
5322+
version "2.4.5"
5323+
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.4.5.tgz#ee710ce5d93a8fdb856fb5ea8ff0e2d75934b2da"
5324+
integrity sha1-7nEM5dk6j9uFb7Xqj/Di11k0sto=
5325+
dependencies:
5326+
glob "^6.0.1"
5327+
52365328
rollup-plugin-typescript2@^0.24.0:
52375329
version "0.24.0"
52385330
resolved "https://registry.yarnpkg.com/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.24.0.tgz#27ce5459d03e095236bdbcda3ca68fe289718d07"
@@ -5299,6 +5391,11 @@ safe-buffer@~5.1.0, safe-buffer@~5.1.1:
52995391
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
53005392
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
53015393

5394+
safe-json-stringify@~1:
5395+
version "1.2.0"
5396+
resolved "https://registry.yarnpkg.com/safe-json-stringify/-/safe-json-stringify-1.2.0.tgz#356e44bc98f1f93ce45df14bcd7c01cda86e0afd"
5397+
integrity sha512-gH8eh2nZudPQO6TytOvbxnuhYBOvDBBLW52tz5q6X58lJcd/tkmqFR+5Z9adS8aJtURSXWThWy/xJtJwixErvg==
5398+
53025399
safe-regex@^1.1.0:
53035400
version "1.1.0"
53045401
resolved "https://registry.yarnpkg.com/safe-regex/-/safe-regex-1.1.0.tgz#40a3669f3b077d1e943d44629e157dd48023bf2e"
@@ -6283,6 +6380,13 @@ xmlbuilder@~9.0.1:
62836380
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-9.0.7.tgz#132ee63d2ec5565c557e20f4c22df9aca686b10d"
62846381
integrity sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=
62856382

6383+
xtend@~2.1.1:
6384+
version "2.1.2"
6385+
resolved "https://registry.yarnpkg.com/xtend/-/xtend-2.1.2.tgz#6efecc2a4dad8e6962c4901b337ce7ba87b5d28b"
6386+
integrity sha1-bv7MKk2tjmlixJAbM3znuoe10os=
6387+
dependencies:
6388+
object-keys "~0.4.0"
6389+
62866390
xtend@~4.0.1:
62876391
version "4.0.2"
62886392
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"

0 commit comments

Comments
 (0)