|
1 | 1 | import { Console } from 'node:console'; |
2 | 2 | import { Utility } from '@aws-lambda-powertools/commons'; |
3 | | -import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types'; |
| 3 | +import type { |
| 4 | + GenericLogger, |
| 5 | + HandlerMethodDecorator, |
| 6 | +} from '@aws-lambda-powertools/commons/types'; |
4 | 7 | import type { Callback, Context, Handler } from 'aws-lambda'; |
5 | 8 | import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js'; |
6 | 9 | import { |
@@ -159,6 +162,13 @@ class Metrics extends Utility implements MetricsInterface { |
159 | 162 | */ |
160 | 163 | private functionName?: string; |
161 | 164 |
|
| 165 | + /** |
| 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. |
| 169 | + */ |
| 170 | + readonly #logger: GenericLogger; |
| 171 | + |
162 | 172 | /** |
163 | 173 | * Flag indicating if this is a single metric instance |
164 | 174 | * @default false |
@@ -193,6 +203,7 @@ class Metrics extends Utility implements MetricsInterface { |
193 | 203 |
|
194 | 204 | this.dimensions = {}; |
195 | 205 | this.setOptions(options); |
| 206 | + this.#logger = options.logger || this.console; |
196 | 207 | } |
197 | 208 |
|
198 | 209 | /** |
@@ -439,6 +450,13 @@ class Metrics extends Utility implements MetricsInterface { |
439 | 450 | this.storedMetrics = {}; |
440 | 451 | } |
441 | 452 |
|
| 453 | + /** |
| 454 | + * Check if there are stored metrics in the buffer. |
| 455 | + */ |
| 456 | + public hasStoredMetrics(): boolean { |
| 457 | + return Object.keys(this.storedMetrics).length > 0; |
| 458 | + } |
| 459 | + |
442 | 460 | /** |
443 | 461 | * A class method decorator to automatically log metrics after the method returns or throws an error. |
444 | 462 | * |
@@ -539,9 +557,9 @@ class Metrics extends Utility implements MetricsInterface { |
539 | 557 | * ``` |
540 | 558 | */ |
541 | 559 | public publishStoredMetrics(): void { |
542 | | - const hasMetrics = Object.keys(this.storedMetrics).length > 0; |
| 560 | + const hasMetrics = this.hasStoredMetrics(); |
543 | 561 | if (!this.shouldThrowOnEmptyMetrics && !hasMetrics) { |
544 | | - console.warn( |
| 562 | + this.#logger.warn( |
545 | 563 | 'No application metrics to publish. The cold-start metric may be published if enabled. ' + |
546 | 564 | 'If application metrics should never be empty, consider using `throwOnEmptyMetrics`' |
547 | 565 | ); |
@@ -584,7 +602,7 @@ class Metrics extends Utility implements MetricsInterface { |
584 | 602 | } |
585 | 603 |
|
586 | 604 | if (!this.namespace) |
587 | | - console.warn('Namespace should be defined, default used'); |
| 605 | + this.#logger.warn('Namespace should be defined, default used'); |
588 | 606 |
|
589 | 607 | // We reduce the stored metrics to a single object with the metric |
590 | 608 | // name as the key and the value as the value. |
@@ -731,6 +749,7 @@ class Metrics extends Utility implements MetricsInterface { |
731 | 749 | serviceName: this.dimensions.service, |
732 | 750 | defaultDimensions: this.defaultDimensions, |
733 | 751 | singleMetric: true, |
| 752 | + logger: this.#logger, |
734 | 753 | }); |
735 | 754 | } |
736 | 755 |
|
|
0 commit comments