Scala日志打印

基于scala-logginglogback的日志打印模板,其中logback是一个更高效/更优于log4j的日志打印框架,目前正逐渐替代log4j的位置,以下为实现日志打印的几个步骤:

1. 添加依赖

maven为例:


    com.typesafe.scala-logging
    scala-logging_${scala.version}
    3.5.0


    ch.qos.logback
    logback-classic
    1.2.3

2. 添加配置文件

resources目录下添加logback.xml文件,该文件就类似于log4j.properties



    
        
            %d{HH:mm:ss.SSS} TKD [%thread] %-5level %logger{36} - %msg%n
        
    

    
        /tmp/test.log
        true
        
            %d{HH:mm:ss.SSS} TKD [%thread] %-5level %logger{36} - %msg%n
        
    

    
        
        
    
    

具体配置自行修改

3. 代码操作

import com.typesafe.scalalogging.Logger

private[this] val logger = Logger(this.getClass)

logger.info("server ready ...... ")

打印效果:

23:16:05.668 TKD [main] INFO  c.z.p.monitorcenter.CEApplication$ - server ready ...... 
23:16:05.676 TKD [main] INFO  c.z.p.monitorcenter.CEApplication$ - RabbitMq Consumer Thread Start ...... 

你可能感兴趣的:(Scala日志打印)