EnriMR
EnriMR

Reputation: 3970

Is it possible to send log messages to different files with Play Framework?

I'm using PlayFramework 2.2.2 and I'm very interested in knowing if is possible to log my app in different files and how to do it.

I would like to have different files for different controllers not for different levels.

I have test it to mix several filenames inside conf/application-logger.conf as official documentation of Play says, but I can't find any way to do it.

Upvotes: 2

Views: 978

Answers (1)

anquegi
anquegi

Reputation: 11522

Depending on your play framework version you must customize your logback from play doc:

If you want to fully customize logback, just define a conf/application-logger.xml or conf/logger.xml configuration file. Here is the default configuration file used by Play:

In play framework 4 is logback.xml. For you I think that the easy way is creating custom loggers like this:

1st create a custom logger in your controller:

private static final Logger.ALogger CustomLogger = Logger.of("custom");

like this:

package controllers;


import play.Logger;
import play.mvc.*;



public class Application extends Controller {

    private static final Logger.ALogger CustomLogger = Logger.of("custom");

    public Result index() {



        String toNormal = "this goes normal way";
        String tolevelInfo = "this goes to info";
        String toCustom = "this goes to custom";

        Logger.info(tolevelInfo);



        Logger.error(toNormal);
        Logger.debug(toNormal);
        Logger.warn(toNormal);

        CustomLogger.info(toCustom);



        return ok("Take a look of your logs files");
    }

}

2) second you should write a configuration file like this, this also has an example for filtering by level, and creates 3 files application.log, customfile.log and filtrolevelinfo.log:

<configuration>

  <conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%coloredLevel - %logger - %message%n%xException</pattern>
    </encoder>
  </appender>

    <appender name="FILTROLEVELINFO" class="ch.qos.logback.core.FileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <file>${application.home}/logs/filtrolevelinfo.log</file>
        <encoder>
            <pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
        </encoder>
    </appender>

    <appender name="CUSTOM" class="ch.qos.logback.core.FileAppender">
        <file>${application.home}/logs/customfile.log</file>
        <encoder>
            <pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
        </encoder>
    </appender>




    <!--
      The logger name is typically the Java/Scala package name.
      This configures the log level to log at for a package and its children packages.
    -->
  <logger name="play" level="DEBUG"/>

  <logger name="application" level="DEBUG"/>

    <logger name="custom" level="DEBUG">
        <appender-ref ref="CUSTOM" />
    </logger>


  <root level="DEBUG">
      <appender-ref ref="STDOUT" />
      <appender-ref ref="FILTROLEVELINFO" />
  </root>



</configuration>

with this you should get this three files and the outputs for 1 call tho the method:

application.log:

2015-06-03 20:02:20,838 [INFO] from play.api.libs.concurrent.ActorSystemProvider in pool-15-thread-2 - Starting application default Akka system: application
2015-06-03 20:02:20,903 [INFO] from play.core.server.NettyServer$ in pool-15-thread-2 - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

customfile.log:

2015-06-03 20:02:30,997 [INFO] from custom in application-akka.actor.default-dispatcher-4 - this goes to custom

filtrolevelinfo.log:

2015-06-03 20:02:30,748 [INFO] from play.api.libs.concurrent.ActorSystemProvider in ForkJoinPool-2-worker-1 - Starting application default Akka system: application
2015-06-03 20:02:30,889 [INFO] from play.api.Play$ in ForkJoinPool-2-worker-1 - Application started (Dev)
2015-06-03 20:02:30,995 [INFO] from application in application-akka.actor.default-dispatcher-4 - this goes to info
2015-06-03 20:02:30,997 [INFO] from custom in application-akka.actor.default-dispatcher-4 - this goes to custom

I hope this helps you ;-)

Upvotes: 5

Related Questions