Skip to content

Commit

Permalink
DDCE-4973 - Improve the error logging format (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Divya-Manivannan-hmrc authored Oct 16, 2024
1 parent 4c33600 commit 29a02bc
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand All @@ -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 =>
Expand All @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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 ")
}

Expand All @@ -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")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."))
}
Expand All @@ -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."))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand All @@ -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"
)
}

Expand All @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion project/AppDependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 29a02bc

Please sign in to comment.