Skip to content

Commit

Permalink
incorporate review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Gabor authored and an-tex committed Dec 18, 2024
1 parent da51333 commit ac0e766
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions logstage-elastic/src/main/scala/Logging.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions logstage-elastic/src/test/scala/LoggingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down

0 comments on commit ac0e766

Please sign in to comment.