Idiomatic way of logging in Kotlin

后端 未结 16 1439
情歌与酒
情歌与酒 2020-12-07 07:34

Kotlin doesn\'t have the same notion of static fields as used in Java. In Java, the generally accepted way of doing logging is:

public class Foo {
    privat         


        
16条回答
  •  攒了一身酷
    2020-12-07 07:45

    Slf4j example, same for others. This even works for creating package level logger

    /**  
      * Get logger by current class name.  
      */ 
    
    fun getLogger(c: () -> Unit): Logger = 
            LoggerFactory.getLogger(c.javaClass.enclosingClass)
    

    Usage:

    val logger = getLogger { }
    

提交回复
热议问题