0

I am initializing an ActorSystem using

implicit val system: ActorSystem = ActorSystem("my-system", config)

and want to be use the FileAppender and ConsoleAppenders I use for logging in my application.

I currently have this following in my reference.conf to use the slf4j logger but I believe I need to set my appenders to the logger to get the logs to be grouped with my other application logs.

akka {

  # Loggers to register at boot time (akka.event.Logging$DefaultLogger logs
  # to STDOUT)
  loggers = ["akka.event.slf4j.Slf4jLogger"]

  # Log level used by the configured loggers (see "loggers") as soon
  # as they have been started; before that, see "stdout-loglevel"
  # Options: OFF, ERROR, WARNING, INFO, DEBUG
  loglevel = "DEBUG"

  # Log level for the very basic logger activated during ActorSystem startup.
  # This logger prints the log messages to stdout (System.out).
  # Options: OFF, ERROR, WARNING, INFO, DEBUG
  stdout-loglevel = "DEBUG"

  # Filter of log events that is used by the LoggingAdapter before
  # publishing log events to the eventStream.
  logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"

  use-slf4j = on
}
benthecarman
  • 125
  • 7
  • Feel free to try the config from official sample repo: https://github.com/akka/akka-samples/blob/2.6/akka-sample-cluster-scala/src/main/resources/logback.xml. Let me know if it doesn't help. – yiksanchan Aug 11 '20 at 04:12
  • @YikSanChan it works for the console logs, but I need a file appender to be able to properly set the log file – benthecarman Aug 11 '20 at 04:18

1 Answers1

1

Try this in logback.xml?

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>logs/application.log</file>
        <encoder>
            <pattern>%date [%level] from %logger in %thread - %.-65535msg%n%xException</pattern>
        </encoder>
    </appender>
yiksanchan
  • 1,890
  • 1
  • 13
  • 37
  • Is there a way to dynamically set the log file? – benthecarman Aug 11 '20 at 14:59
  • @benthecarman What do you want to dynamically set? If you want to switch the log level dynamically, and don't mind an akka-management dependency, you can follow https://doc.akka.io/docs/akka-management/current/loglevels.html - add the akka management dependency, and rely on the loglevel API endpoint to change the log level. Let me know if it answers your question. – yiksanchan Aug 11 '20 at 15:17
  • I want to dynamically set the name of the log file – benthecarman Aug 11 '20 at 15:23
  • @benthecarman May I ask why? It sounds not common to do that - is it because you want to rotate your log? – yiksanchan Aug 11 '20 at 15:51
  • It is just a feature we offer in this application – benthecarman Aug 11 '20 at 16:13
  • Feel free to refer to https://stackoverflow.com/questions/7824620/logback-set-log-file-name-programmatically – yiksanchan Aug 11 '20 at 16:34