play的logger是基于Log4j,Play 2.0 uses logback as its logging engine.
一、配置
1. 在conf/application.conf中设置logger的级别
// 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
root logger配置影响全局,如果需要追踪某个库,如Spring,加入
logger.org.springframework=TRACE
2. 如果需要全面配置Log4j,创建conf/log4j.properties
Play默认的配置是
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
参考值是:
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
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文件内容是
<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>
如手动改变logback的配置文件
|
|
to be loaded from the classpath |
|
|
to be loaded from the file system |
|
|
to be loaded from an URL |
二、使用logger
1. java类
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);
}
2. 模板文件中
@{play.Logger.info("tasks=" + tasks)}
参考:
- 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