From ac0e7660c5f608ac9ccabfebef39beee8a2c60e0 Mon Sep 17 00:00:00 2001 From: Andreas Gabor Date: Wed, 18 Dec 2024 08:00:29 +0100 Subject: [PATCH] incorporate review feedback --- logstage-elastic/src/main/scala/Logging.scala | 4 ++-- .../src/main/scala/LogstageCirceElasticRenderingPolicy.scala | 5 ++++- logstage-elastic/src/test/scala/LoggingSpec.scala | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/logstage-elastic/src/main/scala/Logging.scala b/logstage-elastic/src/main/scala/Logging.scala index 39996c9..a60ebc8 100644 --- a/logstage-elastic/src/main/scala/Logging.scala +++ b/logstage-elastic/src/main/scala/Logging.scala @@ -17,9 +17,9 @@ trait Logging extends LoggerTags { implicit lazy val log: IzLogger = { val legacyNameNormalisation = sys.props.get("logger.izumi.legacyNameNormalisation").contains("true") - // set this to 0 to turn it off + // set this to 0 to turn it off. the actual value will be shorter by the length of truncation suffix `[...]` val truncateStringValues = sys.props.get("logger.izumi.truncateStringValuesTo").map(_.toInt) - val defaultTruncateStringValuesTo = 16_384 // 16KB + val defaultTruncateStringValuesTo = 16 * 1024 lazy val jsonPolicy = LogstageCirceElasticRenderingPolicy( loggerClass, diff --git a/logstage-elastic/src/main/scala/LogstageCirceElasticRenderingPolicy.scala b/logstage-elastic/src/main/scala/LogstageCirceElasticRenderingPolicy.scala index c44ba3f..37b9067 100644 --- a/logstage-elastic/src/main/scala/LogstageCirceElasticRenderingPolicy.scala +++ b/logstage-elastic/src/main/scala/LogstageCirceElasticRenderingPolicy.scala @@ -85,12 +85,15 @@ class LogstageCirceElasticRenderingPolicy( dump(maybeTruncatedJson) } + private val truncateIndicationSuffix = "[...]" + private def truncateStringValues(json: Json, maxLength: Int): Json = { + val truncateMaxLengthWithoutSuffix = maxLength - truncateIndicationSuffix.length // this is more efficient than json.fold as it avoids unnecessary un- and re-wrapping json .mapObject(_.mapValues(truncateStringValues(_, maxLength))) .mapArray(_.map(truncateStringValues(_, maxLength))) - .mapString(string => if (string.length > maxLength) string.take(maxLength) else string) + .mapString(string => if (string.length > maxLength) string.take(Math.max(truncateMaxLengthWithoutSuffix, 0)) + truncateIndicationSuffix else string) } // allows indexing of fields with a common name but different types (which elastic rejects) diff --git a/logstage-elastic/src/test/scala/LoggingSpec.scala b/logstage-elastic/src/test/scala/LoggingSpec.scala index c573439..46196b8 100644 --- a/logstage-elastic/src/test/scala/LoggingSpec.scala +++ b/logstage-elastic/src/test/scala/LoggingSpec.scala @@ -72,9 +72,9 @@ class LoggingSpec extends AnyWordSpecLike { elasticLogger.info(s"${true -> "testWithCamelCase"}") } "truncate string values" in { - val truncateTo = 128 + val truncateTo = 26 val elasticLogger = IzLogger(Level.Debug, ConsoleSink(LogstageCirceElasticRenderingPolicy("prefix", truncateStringValues = Some(truncateTo))))() - val longString = "a" * truncateTo + "DROPME" + val longString = "abcdefghijklmnopqrstuvwxyz" * truncateTo + "DROPME" elasticLogger.info(s"this will be a $longString") } }