2

New to Scala, trying to compute and log latency for my kafka records using log4j, but running into errors. Tried to look at some SO articles but I think I missing some Scala concept here. Any help is appreciated.

Solution1: Does not give an error.

val currentTimeInMillis = Instant.now.toEpochMilli
val latency = Math.max(0, currentTimeInMillis - record.timestamp())
logger.info("record latency: {}", latency)
logger.info("record KafkaPartition: {}, record Offset: {}", record.kafkaPartition(), record.kafkaOffset())

Solution2: This gives error:

val currentTimeInMillis = Instant.now.toEpochMilli
val latency = Math.max(0, currentTimeInMillis - record.timestamp())
logger.info("record latency: {}, record KafkaPartition: {}, record Offset: {}", latency, record.kafkaPartition(), record.kafkaOffset())

Getting below error for Solution2:

error: overloaded method value info with alternatives

[ERROR]   (x$1: org.slf4j.Marker,x$2: String,x$3: Object*)Unit <and>
[ERROR]   (x$1: org.slf4j.Marker,x$2: String,x$3: Any,x$4: Any)Unit <and>
[ERROR]   (x$1: String,x$2: Object*)Unit
[ERROR]  cannot be applied to (String, Long, Integer, Long)
[ERROR] logger.info("record latency: {}, record KafkaPartition: {}, record Offset: {}", latency, record.kafkaPartition(), record.kafkaOffset())
[ERROR]                  ^
[ERROR] one error found
Yogesh D
  • 1,558
  • 14
  • 29
  • 2
    Maybe it is an autoboxing failure from your primitive types. As a workaround, you could use String interpolation directly (`s"record latency ${latency}..."`). I think it works with two arguments because there is an overload that does not require varargs. You could also get a Scala wrapper for SLF4J, those usually also support lazy log messages which is a nice feature as it can skip generating the message and its inputs depending on log level. – Thilo Oct 25 '19 at 10:31
  • Adding `(s"record latency ${latency}...")` did not help. Looking into Scala wrapper for SLF4J option. – Yogesh D Oct 25 '19 at 10:49
  • 2
    What error do you get with the string interpolation? Remember to remove the now redundant extra arguments. All that is left is `logger.info(s"....")`. – Thilo Oct 25 '19 at 10:50
  • Oh yes, I didn't put it correctly, that worked! `logger.info(s"record latency: ${latency}, record KafkaPartition: ${record.kafkaPartition()}, record Offset: ${record.kafkaOffset()}")` – Yogesh D Oct 25 '19 at 10:58

2 Answers2

0

I've run into this before with Scala. Adding .toString to log arguments that aren't Strings will fix the problem.

D Perkins
  • 123
  • 1
  • 10
0

Alternatively you could do:

logger.info("record latency: {}, record KafkaPartition: {}, record Offset: {}", latency: java.lang.Long, record.kafkaPartition(): Integer, record.kafkaOffset(): java.lang.Long)

to bypass the type restrictions.

Michael-7
  • 1,739
  • 14
  • 17