码迷,mamicode.com
首页 > 其他好文 > 详细

Play framework logging设置

时间:2014-05-05 22:59:19      阅读:500      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

play的logger是基于Log4j,Play 2.0 uses logback as its logging engine.

一、配置

1. 在conf/application.conf中设置logger的级别

bubuko.com,布布扣
// Logger
// ~~~~~~~~~~~~~~~~~~~~~~~~~
// You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory 
# Root logger: logger=ERROR # Logger used by the framework: logger.play=INFO # Logger provided to your application: logger.application=DEBUG
bubuko.com,布布扣

root logger配置影响全局,如果需要追踪某个库,如Spring,加入

logger.org.springframework=TRACE

2. 如果需要全面配置Log4j,创建conf/log4j.properties

Play默认的配置是

bubuko.com,布布扣
log4j.rootLogger=ERROR, Console
log4j.logger.play=INFO
# Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m %n
bubuko.com,布布扣

参考值是:

bubuko.com,布布扣
log4j.rootLogger=WARN, Rolling
log4j.logger.play=INFO      
log4j.appender.Rolling=org.apache.log4j.RollingFileAppender
log4j.appender.Rolling.File=${application.path}/logs/application.log
log4j.appender.Rolling.MaxFileSize=1MB
log4j.appender.Rolling.MaxBackupIndex=100
log4j.appender.Rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.Rolling.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m %n
bubuko.com,布布扣

log4j.logger.play的等级必须小于或的等于 "application.log"的等级,如"application.log"的等级是INFO,那么log4j的等级必须是INFO或更低如TRACE, DEBUG。

3、 配置logback——输出有两个

  • the standard out stream
  • logs/application.log

如果要自定义logback,修改conf/logger.xml,默认的logger.xml文件内容是

bubuko.com,布布扣
<configuration>    
  <conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />  
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
     <file>${application.home}/logs/application.log</file>
     <encoder>
       <pattern>%date - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
     </encoder>
   </appender>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%coloredLevel %logger{15} - %message%n%xException{5}</pattern>
    </encoder>
  </appender>  
  <logger name="play" level="INFO" />
  <logger name="application" level="INFO" />
  <root level="ERROR">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
  </root>  
</configuration>
bubuko.com,布布扣

如手动改变logback的配置文件

-Dlogger.resource

$ start -Dlogger.resource=conf/prod-logger.xml
to be loaded from the classpath

-Dlogger.file

$ start -Dlogger.file=/opt/prod/logger.xml
to be loaded from the file system

-Dlogger.url

$ start -Dlogger.url=http://conf.mycompany.com/logger.xml
to be loaded from an URL

二、使用logger

1. java类

bubuko.com,布布扣
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public
class Orders extends Controller { private static Logger logger = LoggerFactory.getLogger(Orders.class); ......
logger.info("Found " + entRef.length + " reference(s) for " + key); }
bubuko.com,布布扣

2. 模板文件中

bubuko.com,布布扣
@{play.Logger.info("tasks=" + tasks)}
bubuko.com,布布扣

 

 

 

参考:

  • http://www.playframework.com/documentation/2.0/SettingsLogger
  • http://www.playframework.com/documentation/1.0/logs
  • http://blog.amyboyd.co.uk/2011/05/how-to-configure-logging-in-play.html

 

Play framework logging设置,布布扣,bubuko.com

Play framework logging设置

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/JoannaQ/p/3704751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!