How to redirect registration to akka?

I am implementing a distributed database with scala 2.9 and akka 2.0. My current problem is that I want to redirect standard logging to a file instead of stdout. I don't really want to use SLF4J or SLF4S. Is there an easy way to redirect log output?

+4
source share
1 answer

The aka documentation for logging says that you can register handlers in config as follows:

akka { # Event handlers to register at boot time (Logging$DefaultLogger logs to STDOUT) event-handlers = ["akka.event.Logging$DefaultLogger"] # Options: ERROR, WARNING, INFO, DEBUG loglevel = "DEBUG" } 

There is also a SLF4J handler

 akka.event.slf4j.Slf4jEventHandler 

With this, you can add any SLF4J compatible library, such as a journal, to record your journals wherever you want.

edit:

To use logback to enter the file, you need to add the login as a dependency, add Slf4jEventHandler to your configuration:

 akka { # Event handlers to register at boot time (Logging$DefaultLogger logs to STDOUT) event-handlers = ["akka.event.slf4j.Slf4jEventHandler"] # Options: ERROR, WARNING, INFO, DEBUG loglevel = "DEBUG" } 

and add the logback configuration to your project that projects something like this (taken from the log documentation):

 <configuration> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>testFile.log</file> <append>true</append> <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="FILE" /> </root> </configuration> 

Due to asynchronous logging in akka, you cannot use the% thread variable in the log template, use the sourceThread variable from MDC instead. You can read about it at the bottom of this page: http://doc.akka.io/docs/akka/2.0/scala/logging.html

You do not need to explicitly use slf4j or logback in your code, just use normal akka logging, the handler will take care of the rest.

+5
source

Source: https://habr.com/ru/post/1411245/


All Articles