是否可以使用Play Framework将日志消息发送到不同的文件?

时间:2015-06-03 15:21:47

标签: java playframework

我正在使用PlayFramework 2.2.2而且我非常有兴趣知道是否可以将我的应用程序记录在不同的文件中以及如何操作。

我想为不同的控制器设置不同的文件,而不是不同的级别。

我测试它在conf / application-logger.conf中混合了几个文件名,official documentation of Play说,但我找不到任何方法。

1 个答案:

答案 0 :(得分:5)

根据您的游戏框架版本,您必须自play doc

自定义您的回溯
  

如果要完全自定义logback,只需定义一个   conf / application-logger.xml或conf / logger.xml配置文件。   以下是Play使用的默认配置文件:

在Play框架4中是logback.xml。对我来说,我认为简单的方法是创建这样的自定义记录器:

首先在控制器中创建一个自定义记录器:

private static final Logger.ALogger CustomLogger = Logger.of("custom");
像这样:

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)第二,您应该编写这样的配置文件,这也有一个按级别过滤的示例,并创建3个文件application.log,customfile.log和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>

有了这个,你应该得到这三个文件和1个方法的输出:

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

我希望这可以帮助你; - )