Skip to content

Commit b8bbb87

Browse files
committed
FLS-1452 - Use server logger in AdapterCacheService to reduce logging noise
Using the request logger as we do at the moment introduces the 'req' object into every log message, which is noisy and duplicative, especially given that we log the request path upstream.
1 parent 729d160 commit b8bbb87

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

runner/src/server/services/AdapterCacheService.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ const createRedisClient = (): Redis | null => {
6060
export class AdapterCacheService extends CacheService {
6161
private apiService: PreAwardApiService;
6262
private formStorage: Redis | any;
63+
private logger: any;
6364

6465
constructor(server: HapiServer) {
6566
//@ts-ignore
6667
super(server);
6768
this.apiService = server.services([]).preAwardApiService;
69+
this.logger = server.logger;
6870
const redisClient = this.getRedisClient();
6971
if (redisClient) {
7072
this.formStorage = redisClient;
@@ -80,13 +82,13 @@ export class AdapterCacheService extends CacheService {
8082
}
8183

8284
async activateSession(jwt, request): Promise<{ redirectPath: string }> {
83-
request.logger.info(`[ACTIVATE-SESSION] jwt ${jwt}`);
85+
this.logger.info(`[ACTIVATE-SESSION] jwt ${jwt}`);
8486
const initialisedSession = await this.cache.get(this.JWTKey(jwt));
85-
request.logger.info(`[ACTIVATE-SESSION] session details ${initialisedSession}`);
87+
this.logger.info(`[ACTIVATE-SESSION] session details ${initialisedSession}`);
8688
const {decoded} = Jwt.token.decode(jwt);
8789
const {payload}: { payload: DecodedSessionToken } = decoded;
8890
const userSessionKey = {segment: partition, id: `${request.yar.id}:${payload.group}`};
89-
request.logger.info(`[ACTIVATE-SESSION] session metadata ${userSessionKey}`);
91+
this.logger.info(`[ACTIVATE-SESSION] session metadata ${userSessionKey}`);
9092
const {redirectPath} = await super.activateSession(jwt, request);
9193
let redirectPathNew = redirectPath
9294
const form_session_identifier = initialisedSession.metadata?.form_session_identifier;
@@ -95,20 +97,20 @@ export class AdapterCacheService extends CacheService {
9597
redirectPathNew = `${redirectPathNew}?form_session_identifier=${form_session_identifier}`;
9698
}
9799
if (config.overwriteInitialisedSession) {
98-
request.logger.info("[ACTIVATE-SESSION] Replacing user session with initialisedSession");
100+
this.logger.info("[ACTIVATE-SESSION] Replacing user session with initialisedSession");
99101
this.cache.set(userSessionKey, initialisedSession, sessionTimeout);
100102
} else {
101103
const currentSession = await this.cache.get(userSessionKey);
102104
const mergedSession = {
103105
...currentSession,
104106
...initialisedSession,
105107
};
106-
request.logger.info("[ACTIVATE-SESSION] Merging user session with initialisedSession");
108+
this.logger.info("[ACTIVATE-SESSION] Merging user session with initialisedSession");
107109
this.cache.set(userSessionKey, mergedSession, sessionTimeout);
108110
}
109-
request.logger.info(`[ACTIVATE-SESSION] redirect ${redirectPathNew}`);
111+
this.logger.info(`[ACTIVATE-SESSION] redirect ${redirectPathNew}`);
110112
const key = this.JWTKey(jwt);
111-
request.logger.info(`[ACTIVATE-SESSION] drop key ${JSON.stringify(key)}`);
113+
this.logger.info(`[ACTIVATE-SESSION] drop key ${JSON.stringify(key)}`);
112114
await this.cache.drop(key);
113115
return {
114116
redirectPath: redirectPathNew,
@@ -129,7 +131,7 @@ export class AdapterCacheService extends CacheService {
129131
if (request.query.form_session_identifier) {
130132
id = `${id}:${request.query.form_session_identifier}`;
131133
}
132-
request.logger.info(`[ACTIVATE-SESSION] session key ${id} and segment is ${partition}`);
134+
this.logger.info(`[ACTIVATE-SESSION] session key ${id} and segment is ${partition}`);
133135
return {
134136
segment: partition,
135137
id: `${id}${additionalIdentifier ?? ""}`,
@@ -145,7 +147,7 @@ export class AdapterCacheService extends CacheService {
145147
return currentHash === cachedHash;
146148
} catch (error) {
147149
// If we can't validate, assume cache is valid
148-
request.logger.warn({
150+
this.logger.warn({
149151
...LOGGER_DATA,
150152
message: `Could not validate cache for form ${formId}, using cached version`
151153
});
@@ -162,13 +164,13 @@ export class AdapterCacheService extends CacheService {
162164
if (!apiResponse) return null;
163165
const formsCacheKey = `${FORMS_KEY_PREFIX}:${formId}`;
164166
await this.formStorage.set(formsCacheKey, JSON.stringify(apiResponse));
165-
request.logger.info({
167+
this.logger.info({
166168
...LOGGER_DATA,
167169
message: `Cached form ${formId} from Pre-Award API`
168170
});
169171
return apiResponse as PublishedFormResponse;
170172
} catch (error) {
171-
request.logger.error({
173+
this.logger.error({
172174
...LOGGER_DATA,
173175
message: `Failed to fetch form ${formId}`,
174176
error: error
@@ -229,20 +231,20 @@ export class AdapterCacheService extends CacheService {
229231
let configObj = null;
230232
if (jsonDataString !== null) {
231233
// Cache hit
232-
request.logger.debug({
234+
this.logger.debug({
233235
...LOGGER_DATA,
234236
message: `Cache hit for form ${formId}`
235237
});
236238
configObj = JSON.parse(jsonDataString);
237239
if (!sessionValidated) {
238240
// Validate cached form once per session
239-
request.logger.debug({
241+
this.logger.debug({
240242
...LOGGER_DATA,
241243
message: `First access of form ${formId} in yar session ${request.yar.id}, validating cache`
242244
});
243245
const isValid = await this.validateCachedForm(formId, configObj.hash, request);
244246
if (!isValid) {
245-
request.logger.info({
247+
this.logger.info({
246248
...LOGGER_DATA,
247249
message: `Cache stale for form ${formId}, fetching fresh version`
248250
});
@@ -251,20 +253,20 @@ export class AdapterCacheService extends CacheService {
251253
configObj = freshConfig;
252254
}
253255
} else {
254-
request.logger.debug({
256+
this.logger.debug({
255257
...LOGGER_DATA,
256258
message: `Cache valid for form ${formId}`
257259
});
258260
}
259261
} else {
260-
request.logger.debug({
262+
this.logger.debug({
261263
...LOGGER_DATA,
262264
message: `Form ${formId} already validated in yar session ${request.yar.id}`
263265
});
264266
}
265267
} else {
266268
// Cache miss - fetch from Pre-Award API
267-
request.logger.info({
269+
this.logger.info({
268270
...LOGGER_DATA,
269271
message: `Cache miss for form ${formId}, fetching from Pre-Award API`
270272
});
@@ -275,7 +277,7 @@ export class AdapterCacheService extends CacheService {
275277
}
276278
if (!sessionValidated) {
277279
// Mark form as validated in this session
278-
request.logger.debug({
280+
this.logger.debug({
279281
...LOGGER_DATA,
280282
message: `Marking form ${formId} as validated in yar session ${request.yar.id}`
281283
});

0 commit comments

Comments
 (0)