Akka Logging outside Actor

后端 未结 6 1558
孤街浪徒
孤街浪徒 2021-01-30 09:02

I have an Akka Actor that makes a call to MyObject.foo(). MyObject is not an Actor. How do I setup Logging in it? With an Actor it\'s simple, because

6条回答
  •  忘了有多久
    2021-01-30 09:33

    According to the latest (currently version 2.6.9) logging documentation using a Logger obtained from org.slf4j.LoggerFactory is perfectly fine, and is actually the recommended way to log outside an actor. I copy hereafter the exact wording.

    It’s perfectly fine to use a Logger retrieved via org.slf4j.LoggerFactory, but then the logging events will not include the akkaSource MDC value. This is the recommend way when logging outside of an actor, including logging from Future callbacks.

    I also provide hereafter a snippet based on the example

    val log = LoggerFactory.getLogger("com.mypackage.MyObject")
    
    Future {
      // do something
      "result"
    }.onComplete {
      case Success(result) => log.info("Success!: {}", result)
      case Failure(exc)    => log.error("Failure!", exc)
    }
    

    In order to minimize performance penalties by logging one can configure an asynchronous appender for the SLF4J backend. Logback is the recommended logging backend.

    dependencies {
      compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    }
    

    A starting point for configuration of logback.xml for production:

    
        myapp.log
        false
        
            myapp_%d{yyyy-MM-dd}.log
        
        
            [%date{ISO8601}] [%level] [%logger] [%marker] [%thread] - %msg MDC: {%mdc}%n
        
    
    
    
        8192
        true
        
    
    
    
        
    
    

    Logging generally means IO and locks, which can slow down the operations of your code if it was performed synchronously.

    The configurations shown above are the ones provided by the AKKA logging documentation. The documentation provides more information and can be found here

提交回复
热议问题