From 29a02bcd4307c41772555fe89b5ccc59919133ad Mon Sep 17 00:00:00 2001 From: Divya Manivannan <175815017+Divya-Manivannan-hmrc@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:48:58 +0100 Subject: [PATCH] DDCE-4973 - Improve the error logging format (#82) --- .../ConversionRatePeriodJson.scala | 6 +++--- .../repositories/ExchangeRateRepository.scala | 21 ++++++++++++------- .../services/ExchangeRateService.scala | 6 +++--- .../workers/XrsDelayHelper.scala | 2 +- .../XrsExchangeRateRequestWorker.scala | 18 ++++++++-------- project/AppDependencies.scala | 2 +- project/plugins.sbt | 2 +- 7 files changed, 32 insertions(+), 25 deletions(-) diff --git a/app/uk/gov/hmrc/currencyconversion/repositories/ConversionRatePeriodJson.scala b/app/uk/gov/hmrc/currencyconversion/repositories/ConversionRatePeriodJson.scala index 743fad8..5dd212f 100644 --- a/app/uk/gov/hmrc/currencyconversion/repositories/ConversionRatePeriodJson.scala +++ b/app/uk/gov/hmrc/currencyconversion/repositories/ConversionRatePeriodJson.scala @@ -57,7 +57,7 @@ class ConversionRatePeriodJson @Inject() (writeExchangeRateRepository: ExchangeR writeExchangeRateRepository.isDataPresent(targetFileName).map { case true => targetFileName case _ => - logger.info(s"[ConversionRatePeriodJson] [checkFileExists] Tried file: $targetFileName and is not found") + logger.info(s"[ConversionRatePeriodJson][checkFileExists] Tried file: $targetFileName and is not found") "empty" } } @@ -68,7 +68,7 @@ class ConversionRatePeriodJson @Inject() (writeExchangeRateRepository: ExchangeR .map { case response if response.isEmpty => logger.error( - s"[ConversionRatePeriodJson] [getExchangeRatesData] XRS_FILE_NOT_AVAILABLE_ERROR Exchange rate file is not able to read" + "[ConversionRatePeriodJson][getExchangeRatesData] XRS_FILE_NOT_AVAILABLE_ERROR Exchange rate file is not able to read" ) throw new RuntimeException("Exchange rate data is not able to read.") case response => @@ -77,7 +77,7 @@ class ConversionRatePeriodJson @Inject() (writeExchangeRateRepository: ExchangeR seq case _ => logger.error( - s"[ConversionRatePeriodJson] [getExchangeRatesData] XRS_FILE_CANNOT_BE_READ_ERROR Exchange rate data mapping is failed" + "[ConversionRatePeriodJson][getExchangeRatesData] XRS_FILE_CANNOT_BE_READ_ERROR Exchange rate data mapping is failed" ) throw new RuntimeException("Exchange rate data mapping is failed") } diff --git a/app/uk/gov/hmrc/currencyconversion/repositories/ExchangeRateRepository.scala b/app/uk/gov/hmrc/currencyconversion/repositories/ExchangeRateRepository.scala index 17f2ac0..41ffdf3 100644 --- a/app/uk/gov/hmrc/currencyconversion/repositories/ExchangeRateRepository.scala +++ b/app/uk/gov/hmrc/currencyconversion/repositories/ExchangeRateRepository.scala @@ -58,14 +58,16 @@ class DefaultExchangeRateRepository @Inject() (mongoComponent: MongoComponent)(i if (forNextMonth) { // Not really a warning, but this is the only way to generate alerts in Pager Duty without changing PROD log level to INFO logger.warn( - s"XRS_FILE_INSERTED_FOR_NEXT_MONTH [ExchangeRateRepository] writing to mongo is successful ${mongoId(forNextMonth)}" + s"[DefaultExchangeRateRepository][insert] XRS_FILE_INSERTED_FOR_NEXT_MONTH writing to mongo is successful ${mongoId(forNextMonth)}" ) } else { - logger.info(s"[ExchangeRateRepository] writing to mongo is successful ${mongoId(forNextMonth)}") + logger.info(s"[DefaultExchangeRateRepository][insert] writing to mongo is successful ${mongoId(forNextMonth)}") } } .getOrElse { - logger.error(s"[ExchangeRateRepository] XRS_FILE_CANNOT_BE_WRITTEN_ERROR " + s"writing to mongo is failed ") + logger.error( + "[DefaultExchangeRateRepository][insert] XRS_FILE_CANNOT_BE_WRITTEN_ERROR writing to mongo has failed" + ) throw new Exception(s"unable to insert exchangeRateRepository") } @@ -81,10 +83,12 @@ class DefaultExchangeRateRepository @Inject() (mongoComponent: MongoComponent)(i options = FindOneAndUpdateOptions().upsert(false).returnDocument(ReturnDocument.AFTER) ) .toFuture() - logger.info(s"[ExchangeRateRepository] writing to mongo is successful ${mongoId(forNextMonth)}") + logger.info(s"[DefaultExchangeRateRepository][update] writing to mongo is successful ${mongoId(forNextMonth)}") } .getOrElse { - logger.error(s"[ExchangeRateRepository] XRS_FILE_CANNOT_BE_WRITTEN_ERROR " + s"writing to mongo is failed") + logger.error( + "[DefaultExchangeRateRepository][update] XRS_FILE_CANNOT_BE_WRITTEN_ERROR " + s"writing to mongo is failed" + ) throw new Exception(s"unable to insert exchangeRateRepository ") } @@ -93,8 +97,11 @@ class DefaultExchangeRateRepository @Inject() (mongoComponent: MongoComponent)(i val oldFileName = currentFileName(sixMonthOldDate) collection.findOneAndDelete(equal("_id", oldFileName)).toFutureOption() map { - case Some(_) => logger.info(s"[ExchangeRateRepository] deleting older data from mongo is successful $oldFileName") - case _ => logger.warn(s"[ExchangeRateRepository] no older data is available") + case Some(_) => + logger.info( + s"[DefaultExchangeRateRepository][deleteOlderExchangeData] deleting older data from mongo is successful $oldFileName" + ) + case _ => logger.warn("[DefaultExchangeRateRepository][deleteOlderExchangeData] no older data is available") } } diff --git a/app/uk/gov/hmrc/currencyconversion/services/ExchangeRateService.scala b/app/uk/gov/hmrc/currencyconversion/services/ExchangeRateService.scala index 6090a91..375c13f 100644 --- a/app/uk/gov/hmrc/currencyconversion/services/ExchangeRateService.scala +++ b/app/uk/gov/hmrc/currencyconversion/services/ExchangeRateService.scala @@ -56,7 +56,7 @@ class ExchangeRateService @Inject() (exchangeRateRepository: ConversionRatePerio } case None => logger.error( - s"[ExchangeRateService] [getRates] XRS_FILE_NOT_AVAILABLE_ERROR No Exchange rate file is found with in the fallback period." + s"[ExchangeRateService][getRates] XRS_FILE_NOT_AVAILABLE_ERROR No Exchange rate file is found with in the fallback period." ) Future.failed(new RuntimeException("Exchange rate file is not available.")) } @@ -67,11 +67,11 @@ class ExchangeRateService @Inject() (exchangeRateRepository: ConversionRatePerio def getCurrencies(date: LocalDate): Future[CurrencyPeriod] = exchangeRateRepository.getCurrencyPeriod(date).flatMap { case Some(value) => - logger.info(s"[ExchangeRateService] [getCurrencies] getCurrencyPeriod has returned a valid file: $value") + logger.info(s"[ExchangeRateService][getCurrencies] getCurrencyPeriod has returned a valid file: $value") Future.successful(value) case None => logger.error( - s"[ExchangeRateService] [getCurrencies] XRS_FILE_NOT_AVAILABLE_ERROR No Exchange rate file is found with in the fallback period." + s"[ExchangeRateService][getCurrencies] XRS_FILE_NOT_AVAILABLE_ERROR No Exchange rate file is found with in the fallback period." ) Future.failed(new RuntimeException("Exchange rate file is not available.")) } diff --git a/app/uk/gov/hmrc/currencyconversion/workers/XrsDelayHelper.scala b/app/uk/gov/hmrc/currencyconversion/workers/XrsDelayHelper.scala index 8964b7b..5f60ca3 100644 --- a/app/uk/gov/hmrc/currencyconversion/workers/XrsDelayHelper.scala +++ b/app/uk/gov/hmrc/currencyconversion/workers/XrsDelayHelper.scala @@ -31,7 +31,7 @@ class XrsDelayHelper { val refreshTime = now.plusDays(1).withHour(0).withMinute(1).withSecond(0).withNano(0) val refreshDelay = Duration(ChronoUnit.MINUTES.between(now, refreshTime), TimeUnit.MINUTES) logger.info( - s"[XrsExchangeRateRequestWorker] Time of deployment: $now, Checking for rates file in ${refreshDelay.toMinutes} minutes" + s"[XrsExchangeRateRequestWorker][calculateInitialDelay] Time of deployment: $now, Checking for rates file in ${refreshDelay.toMinutes} minutes" ) refreshDelay } else { diff --git a/app/uk/gov/hmrc/currencyconversion/workers/XrsExchangeRateRequestWorker.scala b/app/uk/gov/hmrc/currencyconversion/workers/XrsExchangeRateRequestWorker.scala index ce8bd27..2b19dfc 100644 --- a/app/uk/gov/hmrc/currencyconversion/workers/XrsExchangeRateRequestWorker.scala +++ b/app/uk/gov/hmrc/currencyconversion/workers/XrsExchangeRateRequestWorker.scala @@ -78,7 +78,7 @@ class XrsExchangeRateRequestWorker @Inject() ( successfulResponse(response) case _ => logger.error( - s"[XrsExchangeRateRequestWorker] XRS_BAD_REQUEST_FROM_EIS_ERROR BAD Request is received from DES (EIS)" + s"[XrsExchangeRateRequestWorker][tap] XRS_BAD_REQUEST_FROM_EIS_ERROR BAD Request is received from DES (EIS)" ) Future.successful(HttpResponse(SERVICE_UNAVAILABLE, "Service Unavailable")) } @@ -106,7 +106,7 @@ class XrsExchangeRateRequestWorker @Inject() ( Try { response.json.as[JsObject] } recoverWith { case e: Throwable => - logger.error("[XrsExchangeRateRequestWorker] [getResponseJson] Cannot convert response to JSON") + logger.error("[XrsExchangeRateRequestWorker][getResponseJson] Cannot convert response to JSON") Failure(e) } @@ -134,14 +134,14 @@ trait XrsExchangeRateRequest { if (nextMonthsRates > 0 && totalRates != nextMonthsRates) { logger.error( - s"[XrsExchangeRateRequest] [areRatesForNextMonth] XRS_FILE_HAS_MIXED_MONTHS_ERROR Exchange rates file has a mixture of months. " + + s"[XrsExchangeRateRequest][areRatesForNextMonth] XRS_FILE_HAS_MIXED_MONTHS_ERROR Exchange rates file has a mixture of months. " + s"Total rates=$totalRates, next months rates=$nextMonthsRates, expired rates=$expiredRates." ) } if (totalRates == nextMonthsRates) { logger.error( - "[XrsExchangeRateRequest] [areRatesForNextMonth] XRS_FILE_DETECTED_FOR_NEXT_MONTH Inserting XRS file for next month" + "[XrsExchangeRateRequest][areRatesForNextMonth] XRS_FILE_DETECTED_FOR_NEXT_MONTH Inserting XRS file for next month" ) } totalRates == nextMonthsRates @@ -155,10 +155,10 @@ trait XrsExchangeRateRequest { val isPresent = writeExchangeRateRepository.isDataPresent(fileName) isPresent.map { - case true => logger.info(s"[XrsExchangeRateRequest] [verifyIfNextMonthsFileIsReceived] $fileName exists") + case true => logger.info(s"[XrsExchangeRateRequest][isNextMonthsFileIsReceived] $fileName exists") case false => logger.error( - s"[XrsExchangeRateRequest] [verifyIfNextMonthsFileIsReceived] XRS_FILE_NEXT_MONTH_NOT_RECEIVED $fileName" + s"[XrsExchangeRateRequest][isNextMonthsFileIsReceived] XRS_FILE_NEXT_MONTH_NOT_RECEIVED $fileName" ) } @@ -170,18 +170,18 @@ trait XrsExchangeRateRequest { case Success(exchangeRateData) => if (exchangeRateData.exchangeData.isEmpty) { logger.error( - "[XrsExchangeRateRequestWorker] [verifyExchangeDataIsNotEmpty] XRS_EMPTY_RATES_FILE_ERROR Exchange Data size is 0" + "[XrsExchangeRateRequestWorker][verifyExchangeDataIsNotEmpty] XRS_EMPTY_RATES_FILE_ERROR Exchange Data size is 0" ) } else { logger.info( - s"[XrsExchangeRateRequestWorker] [verifyExchangeDataIsNotEmpty] " + + s"[XrsExchangeRateRequestWorker][verifyExchangeDataIsNotEmpty] " + s"Exchange Data size is ${exchangeRateData.exchangeData.size} with timestamp ${exchangeRateData.timestamp}" ) } exchangeRateData.exchangeData.nonEmpty case Failure(exception) => logger.error( - "[XrsExchangeRateRequestWorker] [verifyExchangeDataIsNotEmpty] XRS_RATES_FILE_INVALID_FORMAT " + + "[XrsExchangeRateRequestWorker][verifyExchangeDataIsNotEmpty] XRS_RATES_FILE_INVALID_FORMAT " + "Cannot convert response JSON to ExchangeRateData", exception ) diff --git a/project/AppDependencies.scala b/project/AppDependencies.scala index f20ca79..39fb50b 100644 --- a/project/AppDependencies.scala +++ b/project/AppDependencies.scala @@ -2,7 +2,7 @@ import sbt.* object AppDependencies { - private lazy val bootstrapPlayVersion = "9.3.0" + private lazy val bootstrapPlayVersion = "9.5.0" private lazy val hmrcMongoVersion = "2.2.0" private lazy val compile: Seq[ModuleID] = Seq( diff --git a/project/plugins.sbt b/project/plugins.sbt index 4e37072..a9d997b 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -6,6 +6,6 @@ resolvers += Resolver.url("HMRC-open-artefacts-ivy2", url("https://open.artefact addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "3.22.0") addSbtPlugin("uk.gov.hmrc" % "sbt-distributables" % "2.5.0") addSbtPlugin("org.playframework" % "sbt-plugin" % "3.0.5") -addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.1.0") +addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.2.1") addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.6.4") addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")