1

With KotlinLogger and SLF4J, I want to do something like

// In Log.kt
val logger = KotlinLogging.logger {}

// In Main.kt after importing logger
fun main() {
  val loggerMain = KotlinLogging.logger {}
  loggerMain.info("Hello world") // Main: Hello world
  
  // I want logger to have the same name as loggerMain instead. 
  logger.info("Hello world") // Log: Hello world 


}

I want to reduce the amount of unnecessary boiler plate if possible, and having to create a variable containing the logger for every scope that needs it is something I want to avoid.

I.e. inside of Log.kt, I want to give the logger the name of whatever file accessed it as if it were declared in that file.

Danny
  • 244
  • 3
  • 14
  • It’s not clear what you’re asking to be able to do. Your code above looks fine. – Tenfour04 Dec 07 '22 at 00:54
  • It runs fine, but that isn't the question. I want the name to be whatever the name of the file that called it is. – Danny Dec 07 '22 at 03:23
  • There is no way to do that except to write a script that edits your files for you to add code. – Tenfour04 Dec 07 '22 at 03:40
  • @Danny Perhaps you want to adjust the configuration so that the logger doesn't include the class name, but instead the method name? What is the logger that KotlinLogging is using under the hood? If it's Logback, there's [some magic config](https://logback.qos.ch/manual/loggingSeparation.html#tamingStaticRefs) you can try, but I think will just be easier to create new logging instances! – aSemy Dec 07 '22 at 10:34
  • Good point, added the underlying logger (SLF4J) to the original post. I can't seem to figure out how to setup the config to use the file name (I don't want the method or class) if it were possible – Danny Dec 07 '22 at 23:19
  • @Danny slf4j is an abstraction layer. The actual logger implementation will probably be Logback or Log4j2. – aSemy Dec 08 '22 at 00:09

0 Answers0