diff --git a/src/logger/logger.ts b/src/logger/logger.ts index 3bc73c569..b61961ab3 100644 --- a/src/logger/logger.ts +++ b/src/logger/logger.ts @@ -53,6 +53,10 @@ export class Logger { return new Logger(child) } + public setBindings = (bindings: { [key: string]: unknown }): void => { + this._logger.setBindings(bindings) + } + public error = ( // NOTE: Giving type as `unknown` here // because when we do a `catch`, diff --git a/support/routes/v2/formsg/formsgSiteLaunch.ts b/support/routes/v2/formsg/formsgSiteLaunch.ts index 0b3c1be1a..ebb4ac064 100644 --- a/support/routes/v2/formsg/formsgSiteLaunch.ts +++ b/support/routes/v2/formsg/formsgSiteLaunch.ts @@ -8,7 +8,7 @@ import { err, ok } from "neverthrow" import { config } from "@config/config" -import logger from "@logger/logger" +import parentLogger from "@logger/logger" import InitializationError from "@errors/InitializationError" @@ -85,9 +85,15 @@ export class FormsgSiteLaunchRouter { }, ] - logger.info( - `Launch site form submission [${submissionId}] (repoName '${repoName}', domain '${primaryDomain}') requested by <${requesterEmail}>` - ) + this.siteLaunchLogger.info({ + message: "Launch site form submission", + meta: { + submissionId, + repoName, + primaryDomain, + requesterEmail, + }, + }) // 2. Check arguments if (!requesterEmail) { @@ -147,6 +153,10 @@ export class FormsgSiteLaunchRouter { private readonly infraService: FormsgSiteLaunchRouterProps["infraService"] + private readonly siteLaunchLogger = parentLogger.child({ + module: "formsgSiteLaunch", + }) + constructor({ usersService, infraService }: FormsgSiteLaunchRouterProps) { this.usersService = usersService this.infraService = infraService @@ -249,14 +259,21 @@ export class FormsgSiteLaunchRouter { })) } catch (e) { if (isErrnoException(e) && e.code === "ENODATA") { - logger.info( - `Domain ${launchResult.primaryDomainSource} does not have any AAAA records.` - ) + this.siteLaunchLogger.info({ + message: `Domain does not have any AAAA records.`, + meta: { + domain: launchResult.primaryDomainSource, + }, + }) return [] // no AAAA records found } - logger.error( - `Error when trying to get AAAA records for domain ${launchResult.primaryDomainSource}: ${e}` - ) + this.siteLaunchLogger.error({ + message: "Error when trying to get AAAA records for domain", + error: e, + meta: { + domain: launchResult.primaryDomainSource, + }, + }) throw e } } @@ -313,9 +330,12 @@ export class FormsgSiteLaunchRouter { return result } catch (e) { if (isErrnoException(e) && e.code === "ENODATA") { - logger.info( - `Domain ${launchResult.primaryDomainSource} does not have any CAA records.` - ) + this.siteLaunchLogger.info({ + message: `Domain does not have any CAA records.`, + meta: { + domain: launchResult.primaryDomainSource, + }, + }) // if no CAA records, no need to add Amazon CAA and letsencrypt.org CAA return { @@ -323,9 +343,14 @@ export class FormsgSiteLaunchRouter { addLetsEncryptCAA: false, } } - logger.error( - `Error when trying to get CAA records for domain ${launchResult.primaryDomainSource}: ${e}` - ) + + this.siteLaunchLogger.error({ + message: "Error when trying to get CAA records for domain", + error: e, + meta: { + domain: launchResult.primaryDomainSource, + }, + }) throw e } } @@ -375,9 +400,13 @@ export class FormsgSiteLaunchRouter { await this.sendLaunchError(submissionId, failureResults) } catch (e) { - logger.error( - `Something unexpected went wrong when launching sites from form submission ${submissionId}. Error: ${e}` - ) + this.siteLaunchLogger.error({ + message: `Something unexpected went wrong when launching sites from form submission`, + error: e, + meta: { + submissionId, + }, + }) } }