Skip to content

Commit d16be1c

Browse files
committed
chore: simplified logic
1 parent 3473f73 commit d16be1c

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

packages/metrics/src/Metrics.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,11 @@ class Metrics extends Utility implements MetricsInterface {
163163
private functionName?: string;
164164

165165
/**
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.
167169
*/
168-
readonly #logger?: GenericLogger;
170+
readonly #logger: GenericLogger;
169171

170172
/**
171173
* Flag indicating if this is a single metric instance
@@ -200,8 +202,8 @@ class Metrics extends Utility implements MetricsInterface {
200202
super();
201203

202204
this.dimensions = {};
203-
this.#logger = options.logger;
204205
this.setOptions(options);
206+
this.#logger = options.logger || this.console;
205207
}
206208

207209
/**
@@ -557,14 +559,10 @@ class Metrics extends Utility implements MetricsInterface {
557559
public publishStoredMetrics(): void {
558560
const hasMetrics = this.hasStoredMetrics();
559561
if (!this.shouldThrowOnEmptyMetrics && !hasMetrics) {
560-
const message =
562+
this.#logger.warn(
561563
'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+
);
568566
}
569567
const emfOutput = this.serializeMetrics();
570568
hasMetrics && this.console.log(JSON.stringify(emfOutput));
@@ -604,9 +602,7 @@ class Metrics extends Utility implements MetricsInterface {
604602
}
605603

606604
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');
610606

611607
// We reduce the stored metrics to a single object with the metric
612608
// name as the key and the value as the value.

0 commit comments

Comments
 (0)