Skip to content

Commit eecb370

Browse files
committed
Merge branch 'feat/logging'
2 parents 4fa3e51 + d7e3972 commit eecb370

File tree

3 files changed

+262
-128
lines changed

3 files changed

+262
-128
lines changed

src/cache/storage-cache.ts

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
CacheItemStateType,
55
} from "../models/cache-item-state.model";
66
import { StorageAdapter } from "../models/storage-adapter.model";
7-
import { anonymizeKey } from "../util/anonymize-key";
7+
import { errorLogger, infoLogger } from "../util";
88

9-
const LOG_PREFIX = `[CACHE]`;
9+
const LOG_PREFIX = "CACHE";
1010
const CACHE_STATE_PREFIX = "cache-state:";
1111
const CACHE_STATE_SECONDS_TTL = 1800; // 30 minutes
1212

@@ -24,7 +24,8 @@ export class StorageCache implements ContactCache {
2424
Math.max(Number(CACHE_REFRESH_INTERVAL), 1) * 1000;
2525
}
2626

27-
this.log(
27+
infoLogger(
28+
LOG_PREFIX,
2829
`Initialized storage cache with maximum refresh interval of ${
2930
this.cacheRefreshIntervalMs / 1000
3031
}s.`
@@ -35,10 +36,8 @@ export class StorageCache implements ContactCache {
3536
key: string,
3637
getFreshValue?: (key: string) => Promise<Contact[]>
3738
): Promise<Contact[] | CacheItemState> {
38-
const anonKey = anonymizeKey(key);
39-
4039
try {
41-
this.log(`[${anonKey}] Trying to get contacts from cache`);
40+
infoLogger(LOG_PREFIX, "Trying to get contacts from cache", key);
4241

4342
const cacheItemState = await this.storage.get<CacheItemState>(
4443
this.getCacheItemKey(key)
@@ -47,14 +46,18 @@ export class StorageCache implements ContactCache {
4746
const contacts = await this.storage.get<Contact[]>(key);
4847

4948
if (cacheItemState?.state === CacheItemStateType.FETCHING) {
50-
this.log(
51-
`[${anonKey}] Not refreshing contacts, because cache state is FETCHING`
49+
infoLogger(
50+
LOG_PREFIX,
51+
"Not refreshing contacts, because cache state is FETCHING",
52+
key
5253
);
5354

5455
// if we have old contacts saved in cache we return them instead
5556
if (contacts && contacts?.length > 0) {
56-
this.log(
57-
`[${anonKey}] Returning previously cached contacts (${contacts.length}), because new contacts are still being fetched`
57+
infoLogger(
58+
LOG_PREFIX,
59+
`Returning previously cached contacts (${contacts.length}), because new contacts are still being fetched`,
60+
key
5861
);
5962
return contacts;
6063
}
@@ -63,7 +66,11 @@ export class StorageCache implements ContactCache {
6366
}
6467

6568
if (contacts) {
66-
this.log(`[${anonKey}] Found ${contacts.length} contacts in cache`);
69+
infoLogger(
70+
LOG_PREFIX,
71+
`Found ${contacts.length} contacts in cache`,
72+
key
73+
);
6774

6875
const now: number = new Date().getTime();
6976

@@ -74,40 +81,44 @@ export class StorageCache implements ContactCache {
7481
);
7582

7683
if (getFreshValue && isValueStale) {
77-
this.log(
78-
`[${anonKey}] cached value was stale, fetching fresh contacts`
84+
infoLogger(
85+
LOG_PREFIX,
86+
`Cached value was stale, fetching fresh contacts`,
87+
key
7988
);
89+
8090
// we don't return the fresh value here because we don't want to wait on the result.
8191
// We return the old value instead, the fresh value is returned the next time it is requested
8292
this.getRefreshed(key, getFreshValue).catch((error) => {
83-
this.logErr(`[${anonKey}] Unable to get fresh contacts`, error);
93+
errorLogger(LOG_PREFIX, `Unable to get fresh contacts`, error);
8494
});
8595
}
8696

8797
return contacts;
8898
}
8999
} catch (e) {
90-
this.logErr(`[${anonKey}] Unable to get contacts from cache`, e);
100+
errorLogger(LOG_PREFIX, `Unable to get contacts from cache`, key, e);
91101
}
92102

93103
if (!getFreshValue) {
94-
this.log(
95-
`[${anonKey}] No "getFreshValue" function provided - returning empty array`
104+
infoLogger(
105+
LOG_PREFIX,
106+
`No "getFreshValue" function provided - returning empty array`,
107+
key
96108
);
97109
return [];
98110
}
99111

100-
this.log(`[${anonKey}] Found no match in cache. Getting fresh value.`);
112+
infoLogger(LOG_PREFIX, `Found no match in cache. Getting fresh value`, key);
101113
return this.getRefreshed(key, getFreshValue);
102114
}
103115

104116
public async set(key: string, contacts: Contact[]): Promise<void> {
105-
const anonKey = anonymizeKey(key);
106-
this.log(`[${anonKey}] Saving ${contacts.length} contacts to cache`);
117+
infoLogger(LOG_PREFIX, `Saving ${contacts.length} contacts to cache`, key);
107118
try {
108119
await this.storage.set(key, contacts);
109120
} catch (e) {
110-
this.logErr(`[${anonKey}] Unable to set cache`, e);
121+
errorLogger(LOG_PREFIX, `Unable to set cache`, key, e);
111122
}
112123
}
113124

@@ -116,8 +127,7 @@ export class StorageCache implements ContactCache {
116127
state: CacheItemStateType,
117128
ttl?: number
118129
): Promise<void> {
119-
const anonKey = anonymizeKey(key);
120-
this.log(`[${anonKey}] Setting cache state to ${state}`);
130+
infoLogger(LOG_PREFIX, `Setting cache state to ${state}`, key);
121131
try {
122132
await this.storage.set(
123133
this.getCacheItemKey(key),
@@ -127,28 +137,25 @@ export class StorageCache implements ContactCache {
127137
},
128138
ttl
129139
);
130-
} catch (error) {
131-
this.logErr(`[${anonKey}] Unable to set cache state`, error);
140+
} catch (e) {
141+
errorLogger(LOG_PREFIX, `Unable to set cache state`, key, e);
132142
}
133143
}
134144

135145
public async delete(key: string): Promise<void> {
136-
const anonKey = anonymizeKey(key);
137-
this.log(`[${anonKey}] Removing contacts from cache`);
146+
infoLogger(LOG_PREFIX, `Removing contacts from cache`, key);
138147
try {
139148
await this.storage.delete(key);
140149
} catch (e) {
141-
this.logErr(`[${anonKey}] Unable to remove contacts from cache`, e);
150+
errorLogger(LOG_PREFIX, `Unable to remove contacts from cache`, key, e);
142151
}
143152
}
144153

145154
private async getRefreshed(
146155
key: string,
147156
getFreshValue: (key: string) => Promise<Contact[]>
148157
): Promise<Contact[]> {
149-
const anonKey = anonymizeKey(key);
150-
151-
this.log(`[${anonKey}] Setting cache state to FETCHING`);
158+
infoLogger(LOG_PREFIX, `Setting cache state to FETCHING`, key);
152159

153160
await this.setCacheState(
154161
key,
@@ -160,37 +167,19 @@ export class StorageCache implements ContactCache {
160167
const freshValue = await getFreshValue(key);
161168

162169
await this.set(key, freshValue);
163-
164-
this.log(`[${anonKey}] Setting cache state to CACHED`);
170+
infoLogger(LOG_PREFIX, `Setting cache state to CACHED`, key);
165171

166172
await this.setCacheState(key, CacheItemStateType.CACHED);
167173

168174
return freshValue;
169-
} catch (error) {
170-
this.log(`[${anonKey}] Error while refreshing value`, error);
175+
} catch (e) {
176+
errorLogger(LOG_PREFIX, `Error while refreshing value`, key, e);
171177
await this.storage.delete(this.getCacheItemKey(key));
172-
throw error;
178+
throw e;
173179
}
174180
}
175181

176182
private getCacheItemKey(key: string) {
177183
return `${CACHE_STATE_PREFIX}${key}`;
178184
}
179-
180-
private log(...args: any) {
181-
console.log(this.constructLogMessage(args));
182-
}
183-
184-
private logErr(...args: any) {
185-
console.error(this.constructLogMessage(args));
186-
}
187-
188-
private constructLogMessage(...args: any) {
189-
return `${LOG_PREFIX} ${args
190-
.flat()
191-
.map((item: unknown) =>
192-
typeof item !== "string" ? JSON.stringify(item) : item
193-
)
194-
.join(" ")}`;
195-
}
196185
}

0 commit comments

Comments
 (0)