Logback logs to STDOUT and not the file

787 views
Skip to first unread message

Aqua Pevi

unread,
Jul 1, 2015, 10:20:54 AM7/1/15
to akka...@googlegroups.com
Hi,

I have a simple Akka application based off of akka-sample-main-java template. I am trying to log into a log file rather than the STDOUT. The pom.xml has dependencies on akka-actor, akka-slf4j and logback-classic. The following are my application.conf and logback.xml. Both are placed in src/main/resources.

application.conf
akka {
  
  loggers = ["akka.event.slf4j.Slf4jLogger"]
stdout-loglevel = "OFF"
}

logback.xml

<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder 
by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.classic.sift.SiftingAppender">
<!-- Set the value via Mapped Diagnostic Context (MDC) in code. -->
<discriminator>
<key>logBasename</key>
<defaultValue>Test</defaultValue>
</discriminator>

<sift>
<appender name="FILE-${LogBasename}"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/application.log</fileNamePattern>
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 5MB -->
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
</encoder>
</appender>
</sift>
</appender>

<root level="INFO">
<appender-ref ref="FILE" />
<!-- <appender-ref ref="STDOUT" /> -->
</root>
</configuration>

Even with STDOUT turned off in application.conf, the logs are shown in the console and not in the logs. In fact,, no logs are created.

Can some one help me with this, please?

Thanks!

Richard Bradley

unread,
Jul 2, 2015, 5:16:03 AM7/2/15
to akka...@googlegroups.com
It looks like it should work.
How are you running the application? "sbt run"?

Are you able to post the code to github?

Aqua Pevi

unread,
Jul 4, 2015, 2:21:36 AM7/4/15
to akka...@googlegroups.com
Hi,

I tried to turn on logback file logger for the sample using the template at http://www.typesafe.com/activator/template/akka-sample-main-java

That does not log to a file as well. Just the console.

I am assuming the UI uses sbt run to run the app.

Richard Bradley

unread,
Jul 6, 2015, 7:39:54 AM7/6/15
to akka...@googlegroups.com
I tried to reproduce your problem, and I think the issue is in your logback config.

I think the "sift" appender is misconfigured somehow. If I replace that appender with a vanilla "file" appender, then things work fine.

If you want to see the code I used, I have pushed it to my GitHub:

You should be able to run:

$ git clone g...@github.com:RichardBradley/akka.git
...
$ cd akka
$ git checkout logback-write-to-file
...
$ cd akka-samples/akka-sample-main-java
$ sbt run
[info] Loading project definition from akka\akka-samples\akka-sample-main-java\project
[info] Set current project to akka-sample-main-java (in build file:akka/akka-samples/akka-sample-main-java/)
[info] Compiling 1 Java source to akka\akka-samples\akka-sample-main-java\target\scala-2.10\classes...

Multiple main classes detected, select one to run:

 [1] sample.hello.Main
 [2] sample.hello.Main2

Enter number: 2

[info] Running sample.hello.Main2
Hello World!
[success] Total time: 3 s, completed 06-Jul-2015 12:33:50
$ cat testFile.log
202  [Hello-akka.actor.default-dispatcher-4] INFO  akka.event.slf4j.Slf4jLogger - Slf4jLogger started
244  [Hello-akka.actor.default-dispatcher-3] INFO  sample.hello.Main2$Terminator - akka://Hello/user/helloWorld has terminated, shutting down system




You could check the Logback docs for instructions on how to use the SiftAppender, or ask on their mailing list?
(It does seem a bit rubbish that the SiftAppender will silently fail if misconfigured in this way.)

Good luck,


Rich
Reply all
Reply to author
Forward
0 new messages