@@ -163,9 +163,11 @@ class Metrics extends Utility implements MetricsInterface {
163
163
private functionName ?: string ;
164
164
165
165
/**
166
- * Custom logger object to be used for emitting debug, warning, and error messages.
166
+ * Custom logger object used for emitting debug, warning, and error messages.
167
+ *
168
+ * Note that this logger is not used for emitting metrics which are emitted to standard output using the `Console` object.
167
169
*/
168
- readonly #logger? : GenericLogger ;
170
+ readonly #logger: GenericLogger ;
169
171
170
172
/**
171
173
* Flag indicating if this is a single metric instance
@@ -200,8 +202,8 @@ class Metrics extends Utility implements MetricsInterface {
200
202
super ( ) ;
201
203
202
204
this . dimensions = { } ;
203
- this . #logger = options . logger ;
204
205
this . setOptions ( options ) ;
206
+ this . #logger = options . logger || this . console ;
205
207
}
206
208
207
209
/**
@@ -557,14 +559,10 @@ class Metrics extends Utility implements MetricsInterface {
557
559
public publishStoredMetrics ( ) : void {
558
560
const hasMetrics = this . hasStoredMetrics ( ) ;
559
561
if ( ! this . shouldThrowOnEmptyMetrics && ! hasMetrics ) {
560
- const message =
562
+ this . #logger . warn (
561
563
'No application metrics to publish. The cold-start metric may be published if enabled. ' +
562
- 'If application metrics should never be empty, consider using `throwOnEmptyMetrics`' ;
563
- if ( this . #logger?. warn ) {
564
- this . #logger. warn ( message ) ;
565
- } else {
566
- this . console . warn ( message ) ;
567
- }
564
+ 'If application metrics should never be empty, consider using `throwOnEmptyMetrics`'
565
+ ) ;
568
566
}
569
567
const emfOutput = this . serializeMetrics ( ) ;
570
568
hasMetrics && this . console . log ( JSON . stringify ( emfOutput ) ) ;
@@ -604,9 +602,7 @@ class Metrics extends Utility implements MetricsInterface {
604
602
}
605
603
606
604
if ( ! this . namespace )
607
- ( this . #logger?. warn || this . console . warn ) (
608
- 'Namespace should be defined, default used'
609
- ) ;
605
+ this . #logger. warn ( 'Namespace should be defined, default used' ) ;
610
606
611
607
// We reduce the stored metrics to a single object with the metric
612
608
// name as the key and the value as the value.
0 commit comments