@@ -20,7 +20,9 @@ import { LogItem } from './log';
20
20
21
21
class Logger implements ClassThatLogs {
22
22
23
- public static coldStart ?: boolean ;
23
+ private static coldStart ?: boolean = undefined ;
24
+
25
+ private static coldStartEvaluated : boolean = false ;
24
26
25
27
private static readonly defaultLogLevel : LogLevel = 'INFO' ;
26
28
@@ -47,15 +49,31 @@ class Logger implements ClassThatLogs {
47
49
48
50
private powertoolLogData : PowertoolLogData = < PowertoolLogData > { } ;
49
51
52
+ public static getColdStartEvaluatedValue ( ) : boolean {
53
+ return Logger . coldStartEvaluated ;
54
+ }
55
+
56
+ public static setColdStartEvaluatedValue ( value : boolean ) {
57
+ Logger . coldStartEvaluated = value ;
58
+ }
59
+
60
+ public static getColdStartValue ( ) : boolean | undefined {
61
+ return Logger . coldStart ;
62
+ }
63
+
64
+ public static setColdStartValue ( value : boolean | undefined ) {
65
+ Logger . coldStart = value ;
66
+ }
67
+
50
68
public constructor ( options : LoggerOptions = { } ) {
51
69
this . setOptions ( options ) ;
52
70
}
53
71
54
72
public addContext ( context : Context ) : void {
55
- Logger . evaluateColdStart ( ) ;
73
+ Logger . evaluateColdStartOnce ( ) ;
56
74
const lambdaContext : Partial < LambdaFunctionContext > = {
57
75
invokedFunctionArn : context . invokedFunctionArn ,
58
- coldStart : Logger . coldStart ,
76
+ coldStart : Logger . getColdStartValue ( ) ,
59
77
awsRequestId : context . awsRequestId ,
60
78
memoryLimitInMB : Number ( context . memoryLimitInMB ) ,
61
79
functionName : context . functionName ,
@@ -162,25 +180,24 @@ class Logger implements ClassThatLogs {
162
180
return logItem ;
163
181
}
164
182
165
- private static evaluateColdStart = ( ( ) => {
166
- let evaluated = false ;
167
- return function ( ) {
168
- if ( ! evaluated ) {
169
- evaluated = true ;
170
- if ( typeof Logger . coldStart === 'undefined' ) {
171
- Logger . coldStart = true ;
172
- return ;
173
- }
174
- if ( Logger . coldStart === true ) {
175
- Logger . coldStart = false ;
176
- return ;
177
- }
183
+ public static evaluateColdStartOnce ( ) {
184
+ if ( Logger . getColdStartEvaluatedValue ( ) === false ) {
185
+ Logger . evaluateColdStart ( ) ;
186
+ }
187
+ }
178
188
179
- Logger . coldStart = false ;
189
+ protected static evaluateColdStart ( ) {
190
+ const coldStartValue = Logger . getColdStartValue ( ) ;
191
+ if ( typeof coldStartValue === 'undefined' ) {
192
+ Logger . setColdStartValue ( true ) ;
193
+ } else if ( coldStartValue === true ) {
194
+ Logger . setColdStartValue ( false ) ;
195
+ } else {
196
+ Logger . setColdStartValue ( false ) ;
197
+ }
180
198
181
- }
182
- } ;
183
- } ) ( )
199
+ Logger . setColdStartEvaluatedValue ( true ) ;
200
+ }
184
201
185
202
private getCustomConfigService ( ) : ConfigServiceInterface | undefined {
186
203
return this . customConfigService ;
0 commit comments