@@ -5,12 +5,13 @@ import { LogItem } from './log';
5
5
import { cloneDeep , merge } from 'lodash/fp' ;
6
6
import { ConfigServiceInterface , EnvironmentVariablesService } from './config' ;
7
7
import {
8
- DefaultLoggerAttributes ,
9
8
Environment ,
9
+ LoggerData ,
10
10
LogAttributes ,
11
11
LoggerOptions ,
12
12
LogLevel ,
13
- LogLevelThresholds , UnformattedAttributes ,
13
+ LogLevelThresholds ,
14
+ UnformattedAttributes , LambdaFunctionContext ,
14
15
} from '../types' ;
15
16
import { LogFormatterInterface , PowertoolLogFormatter } from './formatter' ;
16
17
@@ -20,10 +21,10 @@ class Logger implements LoggerInterface {
20
21
21
22
private customConfigService ?: ConfigServiceInterface ;
22
23
23
- private defaultLoggerAttributes : DefaultLoggerAttributes = { } ;
24
-
25
24
private static readonly defaultLogLevel : LogLevel = 'INFO' ;
26
25
26
+ private static readonly defaultSampleRate : number = 1 ;
27
+
27
28
private envVarsService ?: EnvironmentVariablesService ;
28
29
29
30
private logFormatter ?: LogFormatterInterface ;
@@ -37,14 +38,30 @@ class Logger implements LoggerInterface {
37
38
'ERROR' : 20
38
39
} ;
39
40
40
- private sampleRateValue ?: number ;
41
+ private loggerData : LoggerData = < LoggerData > { } ;
41
42
42
43
public constructor ( options : LoggerOptions = { } ) {
43
44
this . applyOptions ( options ) ;
44
45
}
45
46
46
47
public addContext ( context : Context ) : void {
47
- this . addToDefaultLoggerAttributes ( context , { isColdStart : Logger . isColdStart ( ) } ) ;
48
+ if ( ! this . isContextEnabled ( ) ) {
49
+ return ;
50
+ }
51
+
52
+ const lambdaContext : LambdaFunctionContext = {
53
+ arn : context . invokedFunctionArn ,
54
+ awsRequestId : context . awsRequestId ,
55
+ coldStart : Logger . isColdStart ( ) ,
56
+ memoryLimitInMB : Number ( context . memoryLimitInMB ) ,
57
+ name : context . functionName ,
58
+ version : context . functionVersion ,
59
+ } ;
60
+
61
+ this . addToLoggerData ( {
62
+ lambdaContext
63
+ } ) ;
64
+
48
65
}
49
66
50
67
public createChild ( options : LoggerOptions = { } ) : Logger {
@@ -77,9 +94,9 @@ class Logger implements LoggerInterface {
77
94
this . printLog ( 'WARN' , this . createLogItem ( 'WARN' , message , attributes ) . getAttributes ( ) ) ;
78
95
}
79
96
80
- private addToDefaultLoggerAttributes ( ...attributesArray : Array < DefaultLoggerAttributes > ) : void {
81
- attributesArray . forEach ( ( attributes : DefaultLoggerAttributes ) => {
82
- this . defaultLoggerAttributes = merge ( this . getDefaultLoggerAttributes ( ) , attributes ) ;
97
+ private addToLoggerData ( ...attributesArray : Array < Partial < LoggerData > > ) : void {
98
+ attributesArray . forEach ( ( attributes : Partial < LoggerData > ) => {
99
+ this . loggerData = merge ( this . getLoggerData ( ) , attributes ) ;
83
100
} ) ;
84
101
}
85
102
@@ -99,26 +116,23 @@ class Logger implements LoggerInterface {
99
116
this . setLogLevel ( logLevel ) ;
100
117
this . setSampleRateValue ( sampleRateValue ) ;
101
118
this . setLogFormatter ( logFormatter ) ;
102
- this . populateDefaultLoggerAttributes ( serviceName , environment , customAttributes ) ;
119
+ this . populateLoggerData ( serviceName , environment , customAttributes ) ;
103
120
104
121
return this ;
105
122
}
106
123
107
124
private createLogItem ( logLevel : LogLevel , message : string , customAttributes : LogAttributes = { } ) : LogItem {
108
- this . addToDefaultLoggerAttributes ( { logLevel, message, timestamp : new Date ( ) } ) ;
125
+ const unformattedAttributes : UnformattedAttributes = merge ( { logLevel, message, timestamp : new Date ( ) } , this . getLoggerData ( ) ) ;
109
126
110
- return new LogItem ( ) . addAttributes ( this . getLogFormatter ( ) . format ( this . getDefaultLoggerAttributes ( ) as UnformattedAttributes ) )
127
+ return new LogItem ( )
128
+ . addAttributes ( this . getLogFormatter ( ) . format ( unformattedAttributes ) )
111
129
. addAttributes ( customAttributes ) ;
112
130
}
113
131
114
132
private getCustomConfigService ( ) : ConfigServiceInterface | undefined {
115
133
return this . customConfigService ;
116
134
}
117
135
118
- private getDefaultLoggerAttributes ( ) : DefaultLoggerAttributes {
119
- return this . defaultLoggerAttributes ;
120
- }
121
-
122
136
private getEnvVarsService ( ) : EnvironmentVariablesService {
123
137
if ( ! this . envVarsService ) {
124
138
this . setEnvVarsService ( ) ;
@@ -136,25 +150,45 @@ class Logger implements LoggerInterface {
136
150
}
137
151
138
152
private getLogLevel ( ) : LogLevel {
139
- if ( ! this . logLevel ) {
153
+ if ( this . loggerData ? .logLevel ) {
140
154
this . setLogLevel ( ) ;
141
155
}
142
156
143
157
return < LogLevel > this . logLevel ;
144
158
}
145
159
146
- private getSampleRateValue ( ) : number | undefined {
147
- return this . sampleRateValue ;
160
+ private getLoggerData ( ) : LoggerData {
161
+ return this . loggerData ;
148
162
}
149
163
150
- private populateDefaultLoggerAttributes ( serviceName ?: string , environment ?: Environment , customAttributes : LogAttributes = { } ) : void {
151
- this . addToDefaultLoggerAttributes ( {
164
+ private getSampleRateValue ( ) : number {
165
+ if ( ! this . loggerData ?. sampleRateValue ) {
166
+ this . setSampleRateValue ( ) ;
167
+ }
168
+
169
+ return < number > this . loggerData ?. sampleRateValue ;
170
+ }
171
+
172
+ private isContextEnabled ( ) : boolean {
173
+ return this . getCustomConfigService ( ) ?. getIsContextEnabled ( ) === true || this . getEnvVarsService ( ) . getIsContextEnabled ( ) === true ;
174
+ }
175
+
176
+ private populateLoggerData ( serviceName ?: string , environment ?: Environment , customAttributes : LogAttributes = { } ) : void {
177
+
178
+ if ( this . isContextEnabled ( ) ) {
179
+ this . addToLoggerData ( {
180
+ lambdaContext : {
181
+ coldStart : Logger . isColdStart ( ) ,
182
+ memoryLimitInMB : this . getEnvVarsService ( ) . getFunctionMemory ( ) ,
183
+ name : this . getEnvVarsService ( ) . getFunctionName ( ) ,
184
+ version :this . getEnvVarsService ( ) . getFunctionVersion ( ) ,
185
+ }
186
+ } ) ;
187
+ }
188
+
189
+ this . addToLoggerData ( {
152
190
awsRegion : this . getEnvVarsService ( ) . getAwsRegion ( ) ,
153
191
environment : environment || this . getCustomConfigService ( ) ?. getCurrentEnvironment ( ) || this . getEnvVarsService ( ) . getCurrentEnvironment ( ) ,
154
- functionName : this . getEnvVarsService ( ) . getFunctionName ( ) ,
155
- functionVersion : this . getEnvVarsService ( ) . getFunctionVersion ( ) ,
156
- logLevel : this . getLogLevel ( ) ,
157
- memoryLimitInMB : this . getEnvVarsService ( ) . getFunctionMemory ( ) ,
158
192
sampleRateValue : this . getSampleRateValue ( ) ,
159
193
serviceName : serviceName || this . getCustomConfigService ( ) ?. getServiceName ( ) || this . getEnvVarsService ( ) . getServiceName ( ) ,
160
194
xRayTraceId : this . getEnvVarsService ( ) . getXrayTraceId ( ) ,
@@ -184,11 +218,13 @@ class Logger implements LoggerInterface {
184
218
}
185
219
186
220
private setLogLevel ( logLevel ?: LogLevel ) : void {
187
- this . logLevel = ( logLevel || this . getCustomConfigService ( ) ?. getLogLevel ( ) || this . getEnvVarsService ( ) . getLogLevel ( ) || Logger . defaultLogLevel ) as LogLevel ;
221
+ this . logLevel = ( logLevel || this . getCustomConfigService ( ) ?. getLogLevel ( ) || this . getEnvVarsService ( ) . getLogLevel ( )
222
+ || Logger . defaultLogLevel ) as LogLevel ;
188
223
}
189
224
190
225
private setSampleRateValue ( sampleRateValue ?: number ) : void {
191
- this . sampleRateValue = sampleRateValue || this . getCustomConfigService ( ) ?. getSampleRateValue ( ) || this . getEnvVarsService ( ) . getSampleRateValue ( ) ;
226
+ this . loggerData . sampleRateValue = sampleRateValue || this . getCustomConfigService ( ) ?. getSampleRateValue ( )
227
+ || this . getEnvVarsService ( ) . getSampleRateValue ( ) || Logger . defaultSampleRate ;
192
228
}
193
229
194
230
private shouldPrint ( logLevel : LogLevel ) : boolean {
0 commit comments