使用Play Framework是否可以将日志消息发送到其他文件? [英] Is it possible to send log messages to different files with Play Framework?

查看:72
本文介绍了使用Play Framework是否可以将日志消息发送到其他文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PlayFramework 2.2.2,我非常想知道是否可以将我的应用程序记录在不同的文件中以及如何执行.

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.

我已经对其进行了测试,以将conf/application-logger.conf中的多个文件名混合为官方Play的文档说,但是我找不到任何方法.

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.

推荐答案

根据您的播放框架版本,您必须自

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

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

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:

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

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");

像这样:

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

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>

使用此方法,您应该获得这三个文件,并且通过方法调用1的输出:

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

application.log:

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:

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:

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 ;-)

这篇关于使用Play Framework是否可以将日志消息发送到其他文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆