Skip to content

Commit ce39d52

Browse files
committed
improv: types
1 parent 1671055 commit ce39d52

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

packages/logger/src/Logger.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
LoggerInterface,
2727
} from './types/Logger.js';
2828
import type {
29+
LogKeys,
2930
PowertoolsLogData,
3031
UnformattedAttributes,
3132
} from './types/logKeys.js';
@@ -134,7 +135,7 @@ class Logger extends Utility implements LoggerInterface {
134135
/**
135136
* Temporary log attributes that can be appended with `appendKeys()` method.
136137
*/
137-
private temporaryLogAttributes: LogAttributes = {};
138+
private temporaryLogAttributes: LogKeys = {};
138139
/**
139140
* Buffer used to store logs until the logger is initialized.
140141
*
@@ -210,7 +211,7 @@ class Logger extends Utility implements LoggerInterface {
210211
/**
211212
* @deprecated This method is deprecated and will be removed in the future major versions, please use {@link appendPersistentKeys() `appendPersistentKeys()`} instead.
212213
*/
213-
public addPersistentLogAttributes(attributes: LogAttributes): void {
214+
public addPersistentLogAttributes(attributes: LogKeys): void {
214215
this.appendPersistentKeys(attributes);
215216
}
216217

@@ -222,7 +223,7 @@ class Logger extends Utility implements LoggerInterface {
222223
*
223224
* @param attributes - The attributes to add to all log items.
224225
*/
225-
public appendKeys(attributes: LogAttributes): void {
226+
public appendKeys(attributes: LogKeys): void {
226227
this.#appendKeys(attributes, 'temp');
227228
}
228229

@@ -234,7 +235,7 @@ class Logger extends Utility implements LoggerInterface {
234235
*
235236
* @param attributes - The attributes to add to all log items.
236237
*/
237-
public appendPersistentKeys(attributes: LogAttributes): void {
238+
public appendPersistentKeys(attributes: LogKeys): void {
238239
this.#appendKeys(attributes, 'persistent');
239240
}
240241

@@ -519,7 +520,7 @@ class Logger extends Utility implements LoggerInterface {
519520
}
520521

521522
/**
522-
* Remove all temporary log attributes added with `appendKeys()` method.
523+
* Remove all temporary log attributes added with {@link appendKeys() `appendKeys()`} method.
523524
*/
524525
public resetKeys(): void {
525526
for (const key of Object.keys(this.temporaryLogAttributes)) {
@@ -552,7 +553,7 @@ class Logger extends Utility implements LoggerInterface {
552553
/**
553554
* @deprecated This method is deprecated and will be removed in the future major versions, please use {@link appendPersistentKeys() `appendPersistentKeys()`} instead.
554555
*/
555-
public setPersistentLogAttributes(attributes: LogAttributes): void {
556+
public setPersistentLogAttributes(attributes: LogKeys): void {
556557
this.persistentLogAttributes = attributes;
557558
}
558559

@@ -664,7 +665,7 @@ class Logger extends Utility implements LoggerInterface {
664665
* @param attributes - The attributes to add to the log item.
665666
* @param type - The type of the attributes to add.
666667
*/
667-
#appendKeys(attributes: LogAttributes, type: 'temp' | 'persistent'): void {
668+
#appendKeys(attributes: LogKeys, type: 'temp' | 'persistent'): void {
668669
for (const attributeKey of Object.keys(attributes)) {
669670
if (this.#checkReservedKeyAndWarn(attributeKey) === false) {
670671
this.#keys.set(attributeKey, type);

packages/logger/src/types/Logger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
Environment,
99
LogAttributes,
1010
LogAttributesWithMessage,
11+
LogKeys,
1112
} from './logKeys.js';
1213

1314
/**
@@ -187,8 +188,8 @@ type LogItemExtraInput = [Error | string] | LogAttributes[];
187188
*/
188189
type LoggerInterface = {
189190
addContext(context: Context): void;
190-
addPersistentLogAttributes(attributes?: LogAttributes): void;
191-
appendKeys(attributes?: LogAttributes): void;
191+
appendPersistentKeys(attributes?: LogKeys): void;
192+
appendKeys(attributes?: LogKeys): void;
192193
createChild(options?: ConstructorOptions): LoggerInterface;
193194
critical(input: LogItemMessage, ...extraInput: LogItemExtraInput): void;
194195
debug(input: LogItemMessage, ...extraInput: LogItemExtraInput): void;

packages/logger/src/types/logKeys.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,26 @@ type LogAttributes = { [key: string]: unknown };
2424
* This is the most common log attribute object and it's used as first argument in the logger methods.
2525
*/
2626
type LogAttributesWithMessage = LogAttributes & {
27+
/**
28+
* Your log message.
29+
*/
2730
message: string;
31+
/**
32+
* This key is generated by the logger and cannot be set manually.
33+
*/
34+
level?: never;
35+
/**
36+
* This key is generated by the logger and cannot be set manually.
37+
*/
38+
sampling_rate?: never;
39+
/**
40+
* This key is generated by the logger and cannot be set manually.
41+
*/
42+
service?: never;
43+
/**
44+
* This key is generated by the logger and cannot be set manually.
45+
*/
46+
timestamp?: never;
2847
};
2948

3049
/**
@@ -217,7 +236,31 @@ type LogKey =
217236
| keyof PowertoolsLambdaContextKeys
218237
| AutocompletableString;
219238

239+
type LogKeys = { [key: string]: unknown } & {
240+
/**
241+
* This key is passed when emitting a log message and cannot be set manually.
242+
*/
243+
message?: never;
244+
/**
245+
* This key is generated by the logger and cannot be added to the logger.
246+
*/
247+
level?: never;
248+
/**
249+
* This key is generated by the logger and cannot be added to the logger.
250+
*/
251+
sampling_rate?: never;
252+
/**
253+
* This key is generated by the logger and cannot be added to the logger.
254+
*/
255+
service?: never;
256+
/**
257+
* This key is generated by the logger and cannot be added to the logger.
258+
*/
259+
timestamp?: never;
260+
};
261+
220262
export type {
263+
LogKeys,
221264
LogAttributes,
222265
LogAttributesWithMessage,
223266
Environment,

packages/logger/tests/unit/workingWithkeys.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ describe('Working with keys', () => {
627627

628628
// Act
629629
logger.appendKeys({
630+
// @ts-expect-error - testing invalid key at runtime
630631
level: 'Hello, World!',
631632
});
632633
logger.info('foo');
@@ -652,6 +653,7 @@ describe('Working with keys', () => {
652653

653654
// Act
654655
logger.appendPersistentKeys({
656+
// @ts-expect-error - testing invalid key at runtime
655657
timestamp: 'Hello, World!',
656658
});
657659
logger.info('foo');
@@ -707,6 +709,7 @@ describe('Working with keys', () => {
707709
logger.info(
708710
{
709711
message: 'foo',
712+
// @ts-expect-error - testing invalid key at runtime
710713
timestamp: 'Hello, World!',
711714
},
712715
{

0 commit comments

Comments
 (0)